📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: ZYYYZ
跳转到指定楼层
上一主题 下一主题
收起左侧

Data Scientist Python Coding 题整理(持续更新

   
🔗
CC_hang 2021-11-27 10:26:33 | 只看该作者
全局:
有关machine learning algorithms的问题很有用,要是还有的话可以分享更多一些吗?谢谢啦~
回复

使用道具 举报

全局:
哇!好贴,收藏了
回复

使用道具 举报

🔗
Adelaaa 2022-1-2 06:00:56 | 只看该作者
全局:
感谢lz的分类分享 十分干货了
回复

使用道具 举报

🔗
xiaojean 2022-1-6 03:41:27 | 只看该作者
全局:
谢谢楼主分享
回复

使用道具 举报

🔗
AndersonNAGT 2024-2-7 15:17:34 | 只看该作者
全局:
ZYYYZ 发表于 2021-2-2 22:48
9. Most Frequent Words

Given a file that contains the text of a document and an integer k, retur ...

from collections import heapq, defaultdict. ----
def top_k_freq_words(filename, k):
  with open(filename, "r") as file: # 注意这里
    s = file.read() # 这里注意
  text = s.split()
  word_count = defaultdict(int). 1point3acres
  for word in text:
    word_count[word.lower()] += 1.
  min_heap = []
  for word, count in word_count.items():
    heapq.heappush(min_heap, (count, word))
    if len(min_heap) > k:
      heapq.heappop(min_heap)
  res = []
  while min_heap:
    res.append(heapq.heappop(min_heap)[1]). .и
  return res[::-1]. 1point 3 acres
贴一个自己的解法,谢谢分享!
回复

使用道具 举报

🔗
AndersonNAGT 2024-2-7 16:46:33 | 只看该作者
全局:
ZYYYZ 发表于 2021-2-3 14:42
13 Even Odd Split. 1point3acres.com
-baidu 1point3acres
Given an array, write a function that segregates even and odd numbers. The func ...

def evenOddSplit(nums):
  left = 0
  for i in range(len(nums)):
    if nums[i] % 2 == 0:
      nums[left], nums[i] = nums[i], nums[left]
      left += 1
evenOddSplit(nums)
print(nums)
好题 打卡
回复

使用道具 举报

🔗
AndersonNAGT 2024-2-8 15:07:19 | 只看该作者
全局:
ZYYYZ 发表于 2021-2-6 15:34
17. Binary Search (Leetcode 704)

Given a sorted (in ascending order) integer array nums of n elem ...

优化时间,但是空间效率低 ..
def intersection(nums1, nums2):
  return list(set(nums1) & set(nums2))
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2025-7-16 07:10:36 来自APP | 只看该作者
全局:
本帖最后由 ZYYYZ 于 2025-7-15 16:12 编辑

36: Term Frequency

Say you are given a text document in the form of a string with the following sentence: ..
Example:
Input:
  1. sentence = "I have a nice car with a nice tires"
复制代码
Output:
  1. {
  2. "I":0.11,
  3. "have":0.11,
  4. "a":0.22,
  5. "nice":0.22,
  6. "car": 0.11,
  7. "with":0.11,
  8. "tires":0.11
  9. }
复制代码
Write a program in python to determine the TF (term frequency. check 1point3acres for more.
) values for each term of this sentence.
Note: Round the term frequency to 2 decimal points.

Solution:
  1. def term_frequency(sentence):
  2.     words = sentence.split(' ')
  3.     out_dict = {}.1point3acres
  4.     if words == [""]:
  5.         return out_dict
  6.     count = len(words)
  7.     for word in words:
  8.         if word in out_dict:
  9.             out_dict[word] += 1
  10.         else:
  11.             out_dict[word] = 1. 1point 3acres
  12.     for key in out_dict.keys():
  13.         out_dict[key] = round(out_dict[key]/count, 2)
  14.     return out_dict
  15.    
  16.     . From 1point 3acres bbs
  17. sentence = "I have a nice car with a nice tires"
  18. print(term_frequency(sentence))
  19. {'I': 0.11, 'have': 0.11, 'a': 0.22, 'nice': 0.22, 'car': 0.11, 'with': 0.11, 'tires': 0.11}
复制代码
回复

使用道具 举报

🔗
Aimee20126 2025-7-25 03:47:38 | 只看该作者
全局:
谢谢分享
回复

使用道具 举报

全局:
感谢分享,求职季非常有用!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表