-
Notifications
You must be signed in to change notification settings - Fork 0
/
trail_1.py
286 lines (210 loc) · 6.78 KB
/
trail_1.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
def sort_odd_and_even(a):
if not isinstance(a, list):
return None
if not all(isinstance(n, int) for n in a):
return None
odd = []
even = []
output = []
if (isinstance(n, int) for n in a):
for n in a:
if n % 2 == 0:
even.append(n)
else:
odd.append(n)
even.sort()
odd.sort(reverse=True)
return odd + even
if len(a) == 0:
return a
def password_check(a):
if isinstance(a, list):
return False
if len(str(a)) < 6 or len(str(a)) > 12:
return False
count_num = 0
count_lower = 0
count_upper = 0
for i in a:
if i.isdigit():
count_num += 1
if i.islower():
count_lower += 1
if i.islower():
count_upper += 1
if count_num < 1 or count_lower < 1 or count_upper < 1:
return False
special = "$#@"
count_s = 0
for i in a:
if i in special:
count_s += 1
if count_s < 1:
return False
return True
def recursive_digit_sum(a):
# check the type of input
if not isinstance(a, int):
return False
else:
while a > 9:
b = 0
alist = list(str(a))
for i in alist:
b += int(i)
a = b
return a
print(recursive_digit_sum(32))
print(recursive_digit_sum(678))
print(recursive_digit_sum(88888888888888))
print(recursive_digit_sum("a"))
def reverse_words(a):
word_list = a.split(" ")
rev_list =[]
while len(word_list) > 0:
word = word_list.pop(0)
rev_list.append(word[::-1])
return " ".join(rev_list)
print(reverse_words('Hello World'))
print(reverse_words('python package'))
print(reverse_words('Renmin University of China'))
def is_divisible(a):
# check the type of the input
if not isinstance(a, int):
return None
elif isinstance(a, int):
a_list = list(str(a))
b = 0
for i in a_list:
try:
b += int(i)
except:
pass
if a % b == 0:
return 'Yes'
else:
return 'No'
print(is_divisible(123))
print(is_divisible("-1.0f"))
print(is_divisible(-102))
print(is_divisible(10))
def max_in_list(data):
max_value = None
for item in data:
if isinstance(item, (int, float)):
if max_value is None or item > max_value:
max_value = item
elif isinstance(item, (tuple, list, set)):
item_max = max_in_list(item)
if item_max is not None and (max_value is None or item_max > max_value):
max_value = item_max
return max_value
print(max_in_list([10,-0.2]))
print(max_in_list(["15.5", 7, [(9,2),-2]]))
print(max_in_list(['a',('b','c')]))
print(max_in_list([5, {16, 2}]))
def min_in_list_2(data):
num_list = [item for item in data if isinstance(item, (int,float))]
if len(num_list) < 2:
return None
num_list.sort()
sec_min = num_list[1]
if sec_min > num_list[0]:
return (sec_min, data.index(sec_min))
elif sec_min == num_list[0]:
data.copy().remove(sec_min)
loc = data.index(num_list[0]) + 1
return (sec_min, loc)
print(min_in_list_2([10,-0.2]))
print(min_in_list_2(["15.5", 7, 9, 2, -2, 2]))
print(min_in_list_2(['a','b','c']))
print(min_in_list_2([5, 2, 2]))
# 动态规划——这想法也太妙了!!!
def word_cut(string, dictionary):
if not string:
return ([], 0)
n = len(string)
paths = {i: [] for i in range(n+1)}
dp = [0] * (n + 1)
for i in range(1, n+1):
for j in range(i):
word = string[j: i]
if word in dictionary:
score = dp[j] + dictionary[word]
if score > dp[i]:
dp[i] = score
paths[i] = paths[j].copy() + [word]
if not paths[n]:
return(None, 0)
return (paths[n], dp[n])
def sorted_with_weird_order(string_list, order):
if not 0 <= len(string_list) <= 100000:
return None
for string in string_list:
if not 0 <= len(string) <=100000:
return None
# create an idex for the order(you should set the character as key and the index as value)
order_index = {char: index for index, char in enumerate(order)}
# define a function for comparison
# compare the orders in the order_index
# use subtraction
def compare(s1, s2):
for c1, c2 in zip(s1, s2):
if c1 != c2:
return order_index[c1] - order_index[c2]
return len(s1) - len(s2)
def sort_key(s):
return compare(s, string_list[0])
return sorted(string_list, key=sort_key)
# print(sorted_with_weird_order(['', 'ab', 'a'], 'acbdefghijklmnopqrstuvwxyz'))
print(sorted_with_weird_order(['c', 'b', 'a'], 'acbdefghijklmnopqrstuvwxyz'))
class Animal(object):
def __init__(self, name):
self.__name = name
def speak(self):
print("I am an animal.")
class Dog(Animal):
def __init__(self, name):
super(Dog, self).__init__(name)
self.__species = 'Dog'
def speak(self):
print("I am a Dog.")
def play(self):
print(f"Dog {self._Animal__name} is playing.")
class Cat(Animal):
def __init__(self, name):
super(Cat, self).__init__(name)
self.__species = 'Cat'
def speak(self):
print("I am a Cat.")
def play(self):
print(f"Cat {self._Animal__name} is playing.")
class PetShop(object):
def __init__(self):
self.__pets = []
def add_pet(self, pet):
self.__pets.append(pet)
def show_pets(self):
for pet in self._PetShop__pets:
if isinstance(pet, Dog):
print(f"{pet._Animal__name} is Dog.")
elif isinstance(pet, Cat):
print(f"{pet._Animal__name} is Cat.")
def testing_PetShop(pet_dict):
pet_shop = PetShop()
for species, name in pet_dict.items():
species = species.split("_")
if species[0] == "Dog":
dog = Dog(name)
dog.speak()
dog.play()
pet_shop.add_pet(dog)
elif species[0] == "Cat":
cat = Cat(name)
cat.speak()
cat.play()
pet_shop.add_pet(cat)
else:
print("Incorrect dict keys!")
pet_shop.show_pets()
testing_PetShop({"Dog_1":"Tom", "Dog_2":"Bob", "Cat":"Lucy"})