Merge branch 'master' into master
This commit is contained in:
@@ -189,6 +189,7 @@ P7:9*9 乘法表
|
||||
P8:couple 情侣
|
||||
|
||||
实现函数 `couple`,它接受两个列表并返回一个列表,其中包含两个序列的第 i 个元素耦合在一起的列表。您可以假设两个序列的长度相同。
|
||||
tips:zip(list1,list2)
|
||||
|
||||
```python
|
||||
def couple(lst1, lst2):
|
||||
|
||||
@@ -76,7 +76,7 @@ ______
|
||||
|
||||
# 任务
|
||||
|
||||
P9:我们发现以下两个函数看起来实现的非常相似
|
||||
P9:我们发现以下两个函数看起来实现的非常相似,是否可以进行改进,将其整合?
|
||||
|
||||
```python
|
||||
def count_factors(n):
|
||||
@@ -111,9 +111,13 @@ def is_prime(n):
|
||||
return count_factors(n) == 2 # only factors are 1 and n
|
||||
```
|
||||
|
||||
你可以通过一个函数 count_cond 来接受一个含有两个参数的函数 condition(n, i)
|
||||
需求:
|
||||
|
||||
`count_cond` 返回一个单参数函数,它在调用时 `n` 计算从 1 到 `n` 满足的所有数字 `condition`。
|
||||
你需要通过自己写一个函数: `count_cond` ,来接受一个含有两个参数的函数 `condition(n, i)`(使用lambda表达式),
|
||||
|
||||
且`condition`函数应该满足第一个参数为N,而第二个参数将会在`condition`函数中遍历 1 to N。
|
||||
|
||||
`count_cond` 将返回一个单参数函数(ps:一个匿名函数),此单参数函数将会在被调用时返回 1 to N 中所有满足`condition`的数字的个数(如:1到n中素数的个数)。
|
||||
|
||||
```python
|
||||
def count_cond(condition):
|
||||
|
||||
Reference in New Issue
Block a user