Skip to content

Commit 6989fc6

Browse files
author
bianxiaokai
committed
feat:更新
1 parent c6d7496 commit 6989fc6

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

filter.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# filter
2+
3+
4+
def is_odd(n):
5+
return n % 2 == 1
6+
7+
8+
list(filter(is_odd, [1, 2, 34, 4, 5, 6, 7, 8]))
9+
10+
11+
def not_empty(s):
12+
return s and s.strip()
13+
14+
15+
list(filter(not_empty, ['A', 'B', 'C', '', 'D', ' ', 'E']))
16+
17+
18+
# 素数
19+
def _odd_iter():
20+
n = 1
21+
while True:
22+
n += 2
23+
yield n
24+
25+
26+
def _not_divisible(n):
27+
return lambda x: x % n > 0
28+
29+
30+
def primes():
31+
yield 2
32+
it = _odd_iter()
33+
while True:
34+
n = next(it)
35+
yield n
36+
it = filter(_not_divisible(n), it)
37+
38+
39+
# 打印1000以内的素数:
40+
for n in primes():
41+
if n < 10:
42+
print(n)
43+
else:
44+
break
45+
46+
47+
# 回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()筛选出回数:
48+
def is_palindrome(n):
49+
L = [i for i in str(n)]
50+
return L == L[::-1]
51+
52+
53+
def is_palindrome_1(n):
54+
if str(n) == str(n)[::-1]:
55+
return n
56+
57+
58+
# 测试:
59+
60+
output = filter(is_palindrome_1, range(1, 100))
61+
62+
print('1~100:', list(output))

if.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
print(84.5 / (1.75 ** 2))
99

1010
height = 1.75
11-
weight = 83.8
11+
weight = 83.3
1212

1313
bmi = weight / (height ** 2)
1414
print('bmi = %.f' % bmi)

0 commit comments

Comments
 (0)