-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
162 lines (123 loc) · 2.89 KB
/
demo.py
File metadata and controls
162 lines (123 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from collections.abc import Iterator
from functools import reduce
from decimal import Decimal
from io import StringIO
import json
# with open('test.txt', 'r', encoding='utf-8') as f:
# print(f.read(10))
#
# exit()
class Chain(object):
test = 123
def __init__(self, path=''):
self._path = path
def __getattr__(self, path):
print(path)
return Chain('%s/%s' % (self._path, path))
def __str__(self):
return self._path
__repr__ = __str__
s = Chain().z.x
print(s)
j = json.dumps(s, default=lambda obj: obj.__dict__)
print(j)
obj = json.loads(j)
print(type(obj))
exit()
def createCounter():
_list = []
def counter():
last = len(_list) + 1
_list.append(last)
return last
return counter
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA())
exit(123)
def is_palindrome_yield(n):
s = str(n)
if len(s) == 1:
yield n
else:
while len(s) > 1:
if s[0] == s[-1]:
s = s[1:-1]
if len(s) < 2:
yield n
else:
break
for i in range(1, 100):
palindrome = is_palindrome_yield(i)
print(palindrome)
exit(123)
palindrome = [n for n in map(is_palindrome_yield, range(1, 100))]
print(palindrome)
def is_palindrome(n):
s = str(n)
if len(s) == 1:
return True
else:
while len(s) > 1:
if s[0] == s[-1]:
s = s[1:-1]
if len(s) < 2:
return True
else:
return False
output = filter(is_palindrome, range(1, 100))
print('1~1000:', list(output))
exit(1)
def _odd_iter():
n = 1
while True:
n = n + 2
yield n
def primes():
yield 2
it = _odd_iter() # 初始序列
while True:
n = next(it) # 返回序列的第一个数
yield n
it = filter(_not_divisible(n), it) # 构造新序列
def _not_divisible(n):
return lambda x: x % n > 0
# 打印1000以内的素数:
for n in primes():
if n < 100:
print(n)
else:
break
exit(1)
def fn(x, y):
return x * y
res = reduce(fn, [1, 2, 4])
print(res)
def _map(z):
return z + 1
_m = map(_map, [1, 2, 3])
if isinstance(_m, Iterator):
print(list(_m))
exit(1)
_list = ['Michael', 'Bob', 'Tracy']
_tuple = (1,)
if _tuple:
print(_tuple)
_input = input('aaa:')
print(_input)
if not isinstance(_input, (int, float)):
raise TypeError('Bad operand type')
print('输入参数类型不合法,%s' % _input)
else:
print('输入参数正确,%s' % _input)
nums = list(range(100))
print(len(nums))
# for _v in nums:
# print(_v)
# 格式化整数和浮点数还可以指定是否补0和整数与小数的位数:
print('%06d-%04d' % (3, 10))
# 000003-0010
print('%06.2f' % 3.1415926)
# 003.14
_dict = {}
_dict['a'] = 1
print(_dict.get('b'))