-
Notifications
You must be signed in to change notification settings - Fork 0
/
del.py
324 lines (254 loc) · 6.07 KB
/
del.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# %%
def perfect_number(n: int) -> bool:
sum_ = 0
for i in range(1, n):
# print(i+1)
if n % (i) == 0:
sum_ += i
# print(i+1)
# print(sum_)
return sum_ == n
# %%
iteration = 2
fibonace = ( '0 1' if iteration > 1 else '0' if iteration == 1 else ['err'] )
iteration -=2
numCur = 0
numNext = 1
while iteration > 0:
sum = numCur + numNext
numCur = numNext
numNext = sum
fibonace += ' ' + str(sum)
iteration -= 1
print (fibonace)
# %%
number = input('Enter numbers separating by comma: ').split(',')
def get_square(ls: list) -> list:
square = []
for i in ls:
square.append(float(i)**2)
return square
try:
square = get_square(number)
except Exception as e:
print(e)
def test_square():
assert get_square([1, 2, 3]) == [1, 4, 9]
assert get_square([1.2, 2, 3]) == [1.44, 4, 9]
# %%
def filter_list(ls: list) -> list:
only_numbers = []
for i in ls:
if not isinstance(i, str):
only_numbers.append(i)
return only_numbers
def filter_list(ls: list) -> list:
return [i for i in ls if not isinstance(i, str)]
# def filter_list(ls: list) -> list:
# only_numbers = []
# for i in ls:
# if i.isnumeric():
# only_numbers.append(i)
# return only_numbers
def test():
assert filter_list([1,2,'a','b']) == [1,2]
assert filter_list([1,'a','b', 0, 15]) == [1, 0, 15]
assert filter_list([1,2,'asd','1', '123', 123]) == [1, 2, 123]
test()
# %%
def count_odd_in_range(a, b):
return (b + 1 - a) // 2
# return [i for i in range(a, b+1) if i % 2 == 0]
def calc_salary(x: list) -> float:
return sum([i for i in x[1:-1]]) / len(x[1:-1])
# task 3
def get_duplicate(x: list):
res = [i for i in x if x.count(i) > 1]
return res[0] if len(res) > 0 else None
# task 4
def get_common(a: list, b: list):
return len(set(a) & set(b))
# %%
# key = find
# task 5 ►
def rle(s: str) -> str:
uniques = ''
[uniques:= uniques+i for i in s if i not in uniques ]
# print(uniques)
counts = [s.count(i) for i in uniques]
res = ''
e = 0
for i in uniques:
res += i
res += str(counts[e])
e += 1
return res
# pop прореживание
# task 5-1 ►
def rle2(s: str) -> str:
uniques = set(s)
uniques = list(uniques)
uniques.sort(key=s.find)
counts = [s.count(i) for i in uniques]
res = ''
for a, b in zip(uniques, counts):
res += (a+str(b))
return res
def rle3(s: str) -> str:
uniques = set(s)
uniques = list(uniques)
uniques.sort(key=s.find)
res = ''
for a in uniques:D
res += (a+str(s.count(a)))
return res
# %%
# [i if (i % 2 == 0) else c.append(i) for i in x]
x = [int(i) for i in '1 2 4 5 3 5 6'.split()]
c = []
for i in x:
if i % 2 == 0:
c.append(i)
else:
c.insert(-1,i)
print(c)
# %%
x = 'lksdfeiIHH'
c = [i.islower() for i in x]
low = (sum(c)/len(c))
upper = 1 - low
print(f'Малих {low*100:.2f}%, великих {upper*100:.2f}%')
# %%
arr = [1,2,5,10]
trg = 3
def func(trg, arr):
if trg in arr:
return arr.index(trg)
elif trg > arr[-1]:
return len(arr)
else:
return [i>trg for i in arr].index(1)
# %%
def check(candidate:str) -> bool:
'''
check if it has a permutation which is a palindrome
'''
x = set(candidate)
cumcount = 0
for i in x:
cumcount += candidate.count(i) % 2
return cumcount <=1
check('civci')
# %%
def check(candidate:str) -> bool:
'''
check if it has a permutation which is a palindrome
'''
return sum([candidate.count(i) % 2 for i in set(candidate)]) <=1
check('civci')
def test_check():
assert check('civic') == True
assert check('civicc') == False
assert check('civci') == True
assert check('c') == True
assert check('cc') == True
test_check()
# %%
x = 'aassbbc'
c= x[0]
[c == (c:=i) for i in x[1:]][::2]
# %%
with open('countries.csv', 'w') as file:
writer = csv.writer(file, delimiter = ',')
writer.writerow("Country,Population,Square,Country")
for country in countries:
writer.writerow(country)
csv.writer(file) csv.reader(file)
writer.wtiterOW
@dataclass
class Book:
# %%
from pydantic import BaseModel
class City(BaseModel):
id: int
name: str
population: int
class City(BaseModel):
id: int
name: str
pop: int
class City(BaseModel):
id: int
name: str = Field(alias='nama')
pop: int
city = City.parse_raw(jsn)
city = City.parse_raw(jsn)
city = City.parse_raw(jsn)
city = City.parse_raw(jsn)
from typing import Optional, List
import json
class Book(BaseModel):
title: str
author: str
price: float
with open() as file:
data = json.load(file)
books: List[Book] = [Book(**item) for item in data]
books = [Book(**item) for item in data]
# %%
def divmod(x,y):
Divmod = namedtuple('Divmod', 'a b')
return Divmod(x, x//y)
DivMod = namedtuple('DivMod', 'a b')
Car = namedtuple('Car', 'a b')
return Car(**)
# %%
from enum import Enum, auto
from dataclasses import dataclass
class Day(Enum):
ONE = 1
TWO = 2
class Note(Enum):
DO = auto()
RE = auto()
class Car(Enum):
MUS = 1
TA = 2
class Role(Enum):
PRES = auto()
VICE = auto()
MAN = auto()
SUBST = auto()
SECRET = auto()
LEAD = auto()
WORKER = auto()
@dataclass
class Emp:
name: str
id: int
role: Role
# %%
def memo(func):
cache = {}
def wrapper(*args, **kwargs):
if str(args)+str(kwargs) not in cache:
result = func(*args, **kwargs)
cache[str(args)+str(kwargs)] = result
else:
print('memoized')
result = cache[str(args)+str(kwargs)]
return result
return wrapper
def memoize(func):
cache = dict()
def wrapper(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = func(*args, **kwargs)
else:
print('memoized...')
return cache[key]
return wrapper
@memo
def hii():
return 23