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

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

   
全局:
YZDH 发表于 2021-2-2 13:42
2. String Compression

Implement a method to perform basic string compression using the counts of  ...

谢谢分享,第12行是不是应该是   result += s[-1] + str(cnt)  ?
回复

使用道具 举报

🔗
qq1909 2021-2-3 15:55:06 | 只看该作者
本楼:
全局:
谢谢分享!!
回复

使用道具 举报

🔗
letttti 2021-2-4 02:53:05 | 只看该作者
本楼:
全局:
感谢分享!
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-4 03:20:16 | 只看该作者
全局:
12. Longest Palindromic Substring (leetcode 5)

Given a string s, return the longest palindromic substring in s.. check 1point3acres for more.

Example:
Input: s = "cbabd"
Output: "bab"
. Χ
  1. class Solution:
  2.     # T: O(n^2)
  3.     # S: O(1)
  4.     def longestPalindrome(self, s):
  5.         cur_longest = [0, 1]
  6.         for i in range(1, len(s)):. 1point3acres.com
  7.             odd = self.helper(s, i - 1, i + 1). 1point3acres
  8.             even = self.helper(s, i - 1, i)
  9.             longest = max(odd, even, key = lambda x: x[1] - x[0])
  10.             cur_longest = max(longest, cur_longest, key = lambda x: x[1]  - x[0])
  11.             
  12.         return s[cur_longest[0]:cur_longest[1]]
  13.             
  14.     def helper(self, s, left, right):
  15.         while left >= 0  and right < len(s):
  16.             if s[left] != s[right]:
  17.                 break
  18.             left -= 1
  19.             right += 1-baidu 1point3acres
  20. .
  21.         return [left + 1, right]. 1point 3acres
  22.     . 1point 3 acres
  23.     . Waral dи,
  24. if __name__ == '__main__':
  25.     assert Solution().longestPalindrome("cbabd") == "bab"
复制代码


回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-4 03:42:25 | 只看该作者
全局:
13 Even Odd Split

Given an array, write a function that segregates even and odd numbers. The functions should put all even numbers first, and then odd numbers.
. 1point3acres.com
Input = [12, 34, 45, 9, 8, 90, 3]
Output = [12, 34, 8, 90, 45, 9, 3]
. Χ
Followup:​ ​Do​ ​it​ ​in-place

  1. def even_odd_split(arr):
  2.     # T: O(n), S:O(n)
  3.     if len(arr) <= 1:
  4.         return arr
  5.     . 1point 3 acres
  6.     even_list, odd_list = [], []
  7.    
  8.     for x in arr:
  9.         if x % 2 == 0:
  10.             even_list.append(x)
  11.         else:
  12.             odd_list.append(x)
  13.             
  14.     return even_list + odd_list. 1point 3acres
  15. . .и
  16. . 1point 3acres
  17. if __name__ == '__main__':. From 1point 3acres bbs
  18.     assert even_odd_split([12, 34, 45, 9, 8, 90, 3]) == [12, 34, 8, 90, 45, 9, 3]
  19.    
  20. . From 1point 3acres bbs
  21. ### followup: do it in-place
  22. def even_odd_split_in_place(arr):. Χ
  23.     # T: O(n), S:O(1). 1point 3acres
  24.     if len(arr) <= 1:
  25.         return arr
  26.    
  27.     left = 0
  28.     right = len(arr) - 1
  29.    
  30.     while left < right:
  31.         while arr[left] % 2 == 0 and left < right:
  32.             left += 1
  33.         . .и
  34.         while arr[right] % 2 ==1 and left < right:
  35.             right -= 1
  36.             
  37.         if left < right:
  38.             arr[left], arr[right] = arr[right], arr[left]
  39.             left += 1.
  40.             right -= 1
  41.             
  42.     return arr . ----


  43. if __name__ == '__main__':
  44.     assert even_odd_split_in_place([12, 34, 45, 9, 8, 90, 3]) == [12, 34, 90, 8, 9, 45, 3]
复制代码


回复

使用道具 举报

🔗
FinalLi 2021-2-4 10:52:35 | 只看该作者
全局:
感谢楼主分享。可以解释一下第一题 interval_idx = bisect.bisect(cumulative_probs, random.random()) - 1 这段code的数学含义么?谢谢
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-5 08:32:18 | 只看该作者
全局:
14. Merge two sorted arrays. 1point 3 acres
这道题有两个变体

Situation 1, i.e. leetcode 88
.
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2.

.google  и
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]

.google  и
T: O(m + n), S: O(1). 1point3acres

  1. class Solution:
  2.     def merge_sorted_array_in_place(self, nums1, m, nums2, n):
  3.         """
  4.         Do not return anything, modify nums1 in-place instead.
  5.         """
  6.         i, j, k = m - 1, n - 1, m + n - 1
  7.         while i >= 0 and j >= 0:
  8.             if nums2[j] >= nums1[i]:
  9.                 nums1[k] = nums2[j]
  10.                 j -= 1
  11.                 k -= 1. ----
  12.             else:
  13.                 nums1[k] = nums1[i]
  14.                 i -= 1
  15.                 k -= 1
  16.                
  17.         while j >= 0:
  18.             nums1[k] = nums2[j]
  19.             j -= 1
  20.             k -= 1
  21.             .1point3acres
  22.             
  23. if __name__ == '__main__':
  24.     nums1, m, nums2, n = [1,2,3,0,0,0], 3, [2,5,6], 3
  25.     Solution().merge_sorted_array_in_place(nums1, m, nums2, n)
  26.     assert nums1 == [1,2,2,3,5,6]
复制代码



Situation 2:

input: arr1: [1, 3, 4, 5], arr2 = [2, 4, 6, 8]
output: result = [1, 2, 3, 4, 4, 5, 6, 8]

T:O(m + n), S: O(m + n)

  1. class Solution:
  2.     def merge_sorted_array(self, arr1, arr2):
  3.         m, n = len(arr1), len(arr2)
  4.         result = [None]*(m + n)
  5.         i = j = k = 0
  6.         
  7.         while i < m and j < n:
  8.             if arr1[i] < arr2[j]:
  9.                 result[k] = arr1[i]
  10.                 k += 1
  11.                 i += 1
  12.             else:
  13.                 result[k] = arr2[j]
  14.                 k += 1
  15.                 j += 1
  16.                 . 1point 3 acres
  17.         while i < m:
  18.             result[k] = arr1[i].
  19.             k += 1
  20.             i += 1
  21.             
  22.         while j < n:
  23.             result[k] = arr2[j]
  24.             k += 1
  25.             j += 1
  26.             
  27.         return result
  28.    

  29. if __name__ == '__main__':
  30.     assert Solution().merge_sorted_array([1, 3, 4, 5], [2, 4, 6, 8]) == [1, 2, 3, 4, 4, 5, 6, 8]

复制代码
.google  и


回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-5 10:41:52 | 只看该作者
全局:
15. Best Time to Buy and Sell Stock (leetcode 121)

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Input: [7, 1, 5, 3, 6, 4] Output: 5. check 1point3acres for more.

max. difference = 6 - 1 = 5 (not 7 - 1 = 6, as selling price needs to be larger than buying price)

T: O(n)
S: O(1)

  1. class Solution:
  2.     def maxProfit(self, prices):.google  и
  3.         if not prices or len(prices) < 2:. check 1point3acres for more.
  4.             return 0
  5.         
  6.         max_profit = 0
    .
  7.         min_price = prices[0]
  8.         
  9.         for price in prices:
  10.             min_price = min(min_price, price)
    . check 1point3acres for more.
  11.             potential_profit = price - min_price
  12.             max_profit = max(potential_profit, max_profit)
  13.             
  14.         return max_profit
  15.    
  16.    
  17. if __name__ == '__main__':
  18.     assert Solution().maxProfit([6, 13, 2, 10, 3, 5]) == 8
  19.     assert Solution().maxProfit([7, 1, 5, 3, 6, 4]) == 5
复制代码


回复

使用道具 举报

全局:
6. Smallest difference of two arrays的另一种更解答。以测试可以得到正确答案:

  1. def find_smallest_diff(arr1, arr2):-baidu 1point3acres
  2.     smallest = max(arr1[0] - arr2[0],arr2[0] - arr1[0])
  3.     for i in arr1:. 1point3acres.com
  4.         for j in arr2:. 1point3acres
  5.             diff = max(i-j, j-i)
  6.             if diff < smallest:
  7.                 smallest = diff
  8.     return smallest
复制代码
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-5 14:51:04 | 只看该作者
全局:
zqf1616zqf1616 发表于 2021-2-5 14:11
6. Smallest difference of two arrays的另一种更解答。以测试可以得到正确答案:
. 1point3acres
[mw_shl_code=bash,t ...

.. double loop的解法是比较容易想到的,不过复杂度比较高,

到后面面试官可能还是会想看你sort后怎么解。
回复

使用道具 举报

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

本版积分规则

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