This repository was archived by the owner on Jan 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit_9part_1.py
More file actions
84 lines (71 loc) · 2.26 KB
/
Unit_9part_1.py
File metadata and controls
84 lines (71 loc) · 2.26 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
from string import punctuation
def create_file_with_numbers(n: int) -> None:
file_name = s = "range_" + str(n) + ".txt"
s = ''
for i in range(n):
s = s + str(i + 1) + '\n'
my_file = open(file_name, 'w', encoding='utf-8')
my_file.write(s)
my_file.close()
# create_file_with_numbers(8)
def longest_word_in_file(file_name: str) -> str:
#work with the file
my_file = open(file_name, 'r', encoding='utf-8')
sy = my_file.read() #read the file
my_file.close()
# prepare text
clean_srt = ''
for w in sy:
if w not in punctuation:
clean_srt = clean_srt + w
# print(clean_srt)
sy = clean_srt.split()
# find the longest word
m, pos = 0, 0
for i in range(len(sy)):
if len(sy[i]) >= m:
m = len(sy[i])
pos = i
return sy[pos]
# longest_word_in_file('range_8.txt')
def count_hundreds_and_dozens(file_name: str) -> str:
my_file = open(file_name, 'r', encoding='utf-8')
sy = my_file.readlines() #read the file
my_file.close()
cnt = 0
sm = 0
for num in sy:
n = len((num.strip()))
if n == 3:
# print('tr=',n)
cnt += 1
elif n == 2:
sm += int(num)
# print('tr=', num)
# my_file.seek(0)
# print(f'{cnt=},{sm=}')
return str(cnt)+','+str(sm)
# file_name = 'numbers.txt'
# print(count_hundreds_and_dozens(file_name))
# file_name_ = 'lorem.txt'
def count_uniq(file_name: str) -> int:
with open(file_name, 'r', encoding='utf-8') as my_file:
sy = my_file.read() #read the file
uni = set(sy.lower().split())
return len(uni)
def create_dict(file_name: str) -> dict:
my_dict = {}
with open(file_name, 'r', encoding='utf-8') as my_file:
sy = my_file.read() #read the file
for word in sy.upper().split():
my_dict[word] = my_dict.get(word,0) + 1
return my_dict
def find_str(file_name: str, substr: str) -> list:
with open(file_name, 'r', encoding='utf-8') as my_file:
sy = my_file.read() #read the file
my_dict = set(sy.upper().split())
res = [word for word in my_dict if word.endswith(substr)]
res.sort(key=lambda x: (len(x), x))
return res
file_name_ = 'words.txt'
print(*find_str(file_name_, 'ЕЯ'), sep='\n')