楼主: 李浩泉
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] LC Python 刷题笔记 有志者事竟成

   
全局:
李浩泉 发表于 2020-8-5 09:11
年薪12,绿卡,30+,普通硕士,中等身材相貌,是找不到老婆的,no kidding

年薪20,2个孩子,40+,双 ...

LZ是在弯曲吗
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-5 09:42:25 | 只看该作者
全局:

虾图 看我的头像
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-5 09:48:03 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-8-5 09:49 编辑

上面那道乱伦题,是FB5级DE的水平,做一道3-4级的简单题缓缓,这种难度的题,一般是电面的第一道,30分钟做5道PYTHON题的那个电面。

You are given an array with positive numbers and a number N. You should find the N-th power of the element in the array with the index N. If N is outside of the array, then return -1. Don't forget that the first element has the index 0.

Let's look at a few examples:
- array = [1, 2, 3, 4] and N = 2, then the result is 32 == 9;
- array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.

Input: Two arguments. An array as a list of integers and a number as a integer.

Output: The result as an integer.

Example:

index_power([1, 2, 3, 4], 2) == 9
index_power([1, 3, 10, 100], 3) == 1000000
index_power([0, 1], 0) == 1
index_power([1, 2], 3) == -1

  1. def index_power(array: list, n: int) -> int:
  2.     if n >= len(array): return -1
  3.     return array[n]**n
  4.    
  5.    
  6. if __name__ == '__main__':
  7.     print('Example:')
  8.     print(index_power([1, 2, 3, 4], 2))
  9.    
  10.     #These "asserts" using only for self-checking and not necessary for auto-testing
  11.     assert index_power([1, 2, 3, 4], 2) == 9, "Square"
  12.     assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"
  13.     assert index_power([0, 1], 0) == 1, "Zero power"
  14.     assert index_power([1, 2], 3) == -1, "IndexError"
  15.     print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
复制代码

        

回复

使用道具 举报

全局:
加油,欣赏你,不要怂就是干!!
回复

使用道具 举报

全局:
有热情毅力很好推一个 但是技巧还是很重要的 分类别刷事半功倍
回复

使用道具 举报

全局:
李浩泉 发表于 2020-8-5 09:26
不瞒你说,我GMAT考了700,IR是满分8,AWA 5.5,但是看看MBA毕业后的薪资,M7S16的平均post MBA,仅仅16W ...

嗯嗯。就是说现在专业不适合读,但是CS更适合?
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-5 23:37:06 | 只看该作者
全局:
这种DE3-4级的简单题,我觉得30分钟可以做30道。

Not all of the elements are important. What you need to do here is to remove all of the elements after the given one from list.

example

For illustration, we have an list [1, 2, 3, 4, 5] and we need to remove all the elements that go after 3 - which is 4 and 5.

We have two edge cases here: (1) if a cutting element cannot be found, then the list shoudn't be changed; (2) if the list is empty, then it should remain empty.

Input: List and the border element.

Output: Iterable (tuple, list, iterator ...).


  1. from typing import Iterable


  2. def remove_all_after(items: list, border: int) -> Iterable:
  3.     list1 = []
  4.     for num in items:
  5.         if num != border:
  6.             list1.append(num)
  7.         else:
  8.             list1.append(num)
  9.             break
  10.     return list1
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-5 23:57:37 | 只看该作者
全局:
We have a List of booleans. Let's check if the majority of elements are true. Some cases worth mentioning: 1) an empty list should return false; 2) if trues and falses have an equal amount, function should return false.
Input: A List of booleans.

Output: A Boolean.

Example:

is_majority([True, True, False, True, False]) == True
is_majority([True, True, False]) == True

  1. def is_majority(items: list) -> bool:
  2.     num1 = 0
  3.     num2 = 0
  4.     for c in items:
  5.         if c == True:
  6.             num1 += 1
  7.         elif c == False:
  8.             num2 += 1
  9.     if num1 > num2:
  10.         return True
  11.     else:
  12.         return False


  13. if __name__ == '__main__':
  14.     print("Example:")
  15.     print(is_majority([True, True, False, True, False]))

  16.     # These "asserts" are used for self-checking and not for an auto-testing
  17.     assert is_majority([True, True, False, True, False]) == True
  18.     assert is_majority([True, True, False]) == True
  19.     assert is_majority([True, True, False, False]) == False
  20.     assert is_majority([True, True, False, False, False]) == False
  21.     assert is_majority([False]) == False
  22.     assert is_majority([True]) == True
  23.     assert is_majority([]) == False
  24.     print("Coding complete? Click 'Check' to earn cool rewards!")
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-6 00:25:01 | 只看该作者
全局:
分别用 Python 和 SQL 计算 Median


PYTHON

A median is a numerical value separating the upper half of a sorted array of numbers from the lower half. In a list where there are an odd number of entities, the median is the number found in the middle of the array. If the array contains an even number of entities, then there is no single middle value, instead the median becomes the average of the two numbers found in the middle. For this mission, you are given a non-empty array of natural numbers (X). With it, you must separate the upper half of the numbers from the lower half and find the median.

Input: An array as a list of integers.

Output: The median as a float or an integer.

Example:

checkio([1, 2, 3, 4, 5]) == 3
checkio([3, 1, 2, 5, 3]) == 3
checkio([1, 300, 2, 200, 1]) == 2
checkio([3, 6, 20, 99, 10, 15]) == 12.5
  1. from typing import List

  2. def checkio(data: List[int]) -> [int, float]:
  3.     data.sort()
  4.     if len(data) % 2 == 0:
  5.         return (data[len(data)//2] + data[len(data)//2 - 1]) / 2
  6.     else:
  7.         return data[len(data)//2]


  8. #These "asserts" using only for self-checking and not necessary for auto-testing
  9. if __name__ == '__main__':
  10.     print("Example:")
  11.     print(checkio([3, 6, 20, 99, 10, 15]))

  12.     assert checkio([1, 2, 3, 4, 5]) == 3, "Sorted list"
  13.     assert checkio([3, 1, 2, 5, 3]) == 3, "Not sorted list"
  14.     assert checkio([1, 300, 2, 200, 1]) == 2, "It's not an average"
  15.     assert checkio([3, 6, 20, 99, 10, 15]) == 12.5, "Even length"
  16.     print("Start the long test")
  17.     assert checkio(list(range(1000000))) == 499999.5, "Long."
  18.     print("Coding complete? Click 'Check' to earn cool rewards!")
复制代码



SQL

# 569 Median Employee Salary

The Employee table holds all employees. The employee table has three columns: Employee Id, Company Name, and Salary.

+-----+------------+--------+
|Id   | Company    | Salary |
+-----+------------+--------+
|1    | A          | 2341   |
|2    | A          | 341    |
|3    | A          | 15     |
|4    | A          | 15314  |
|5    | A          | 451    |
|6    | A          | 513    |
|7    | B          | 15     |
|8    | B          | 13     |
|9    | B          | 1154   |
|10   | B          | 1345   |
|11   | B          | 1221   |
|12   | B          | 234    |
|13   | C          | 2345   |
|14   | C          | 2645   |
|15   | C          | 2645   |
|16   | C          | 2652   |
|17   | C          | 65     |
+-----+------------+--------+
Write a SQL query to find the median salary of each company. Bonus points if you can solve it without using any built-in SQL functions.

+-----+------------+--------+
|Id   | Company    | Salary |
+-----+------------+--------+
|5    | A          | 451    |
|6    | A          | 513    |
|12   | B          | 234    |
|9    | B          | 1154   |
|14   | C          | 2645   |
+-----+------------+--------+

  1. SELECT Id,Company,Salary
  2. FROM (
  3. SELECT
  4. Id,Company,Salary,
  5. ROW_NUMBER() OVER(PARTITION BY Company ORDER BY Salary) AS RN,
  6. COUNT(Id) OVER(PARTITION BY Company) AS COUNTS
  7. FROM Employee ) T
  8. WHERE RN BETWEEN COUNTS/2.0 AND COUNTS/2.0 + 1
  9. ORDER BY 2,3 ASC
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-8-6 02:16:30 | 只看该作者
全局:


这也是一道3-4级DE常考题,用python实现SQL order by 1 desc, 2 asc。

Your mission is to sort the list by the frequency of numbers included in it. If a few numbers have an equal frequency - they should be sorted according to their natural order. For example: [5, 2, 4, 1, 1, 1, 3] ==> [1, 1, 1, 2, 3, 4, 5]


Input: Chaotic list of numbers.

Output: The list of numbers in which they are sorted by their frequency.

Example: frequency_sorting([5, 3, 8, 11, 5, 6, 6, 5]) == [5, 5, 5, 6, 6, 3, 8, 11]

如果是5级DE面试,会提高难道,要求modify order in place without making new list,难度一下子就进马里亚纳海沟了。

SQL:order by 1 desc, 2 asc
PYTHON:sort(key=lambda x: (-x[0], x[1]))

  1. def frequency_sorting(numbers):
  2.     list1 = []
  3.     for num in numbers:
  4.         list1.append([numbers.count(num),num])
  5.     list1.sort(key=lambda x: (-x[0], x[1]))
  6.     list2 = []
  7.     for c in list1:
  8.         list2.append(c[1])
  9.     return list2

  10. if __name__ == '__main__':
  11.     print("Example:")
  12.     print(frequency_sorting([3, 4, 11, 13, 11, 4, 4, 7, 3]))

  13.     #These "asserts" using only for self-checking and not necessary for auto-testing
  14.     assert frequency_sorting([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5], "Already sorted"
  15.     assert frequency_sorting([3, 4, 11, 13, 11, 4, 4, 7, 3]) == [4, 4, 4, 3, 3, 11, 11, 7, 13], "Not sorted"
  16.     assert frequency_sorting([99, 99, 55, 55, 21, 21, 10, 10]) == [10, 10, 21, 21, 55, 55, 99, 99], "Reversed"
  17.     print("Coding complete? Click 'Check' to earn cool rewards!")
复制代码


回复

使用道具 举报

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

本版积分规则

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