-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathl2.py
More file actions
86 lines (64 loc) · 1.93 KB
/
l2.py
File metadata and controls
86 lines (64 loc) · 1.93 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
from curses.ascii import isalnum, isalpha
import random
import re
# Zadanie 2
def pierwiastek(n: int) -> int:
res, sum = 0, 0
while (sum + (2*res + 1)) <= n:
res += 1
sum += (2*res - 1)
return res
assert pierwiastek(16) == 4
assert pierwiastek(18) == 4
assert pierwiastek(25) == 5
assert pierwiastek(30) == 5
assert pierwiastek(81) == 9
assert pierwiastek(99) == 9
assert pierwiastek(100) == 10
# Zadanie 4
def uprosc_zdanie(tekst: str, dl_slowa: int, liczba_slow: int) -> str:
words = tekst.split(' ')
words_filtered = [w for w in words if len(w) <= dl_slowa]
while len(words_filtered) > liczba_slow:
words_filtered.pop(random.randrange(len(words_filtered)))
return ' '.join(words_filtered)
tekst = "Podział peryklinalny inicjałów wrzecionowatych \
kambium charakteryzuje się ścianą podziałową inicjowaną \
w płaszczyźnie maksymalnej."
print(uprosc_zdanie(tekst, 10, 5))
print(uprosc_zdanie(tekst, 10, 5))
print(uprosc_zdanie(tekst, 10, 5))
print(uprosc_zdanie(tekst, 10, 5))
# Zadanie 5
def kompresja(tekst: str) -> str:
res = tekst[0]
count = 1
for i in range(len(tekst)-1):
if(tekst[i] == tekst[i+1]):
count += 1
else:
if count > 1:
res += f'{count}'
res += tekst[i+1]
count = 1
if count > 1:
res += f'{count}'
return res
test = kompresja('aaa"aaa')
def dekompresja(tekst: str) -> str:
res = ''
for (char, num, single_nondigit) in re.findall(r'([a-zA-Z])([0-9]+)|(\D)', tekst):
if num:
res += char * int(num)
else:
res += single_nondigit
return res
print(test)
print(dekompresja(test))
print('a4bxc3')
print(dekompresja('a4bxc3'))
with open('test.txt', 'r') as f:
compressed = kompresja(f.read())
print('Compressed: \n', compressed)
decompressed = dekompresja(compressed)
print('Decompressed: \n', decompressed)