-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (133 loc) · 4.16 KB
/
main.py
File metadata and controls
152 lines (133 loc) · 4.16 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
from collections import Counter
def bubble_sort_by_length(words):
n = len(words)
for i in range(n, 0, -1):
swapped = False
for j in range(i - 1):
if len(words[j]) > len(words[j + 1]):
words[j], words[j + 1] = words[j + 1], words[j]
swapped = True
if not swapped:
break
return words
def merge(left, right):
result = []
i, j = 0 , 0
while i < len(left) and j < len(right):
if len(left[i]) < len(right[j]):
result.append(left[i])
i +=1
else:
result.append(right[j])
j +=1
result += left[i:]
result += right[j:]
return result
def mergesort(list):
if len(list) <= 1:
return list
mid = len(list) //2
left = mergesort(list[mid:])
right = mergesort(list[:mid])
return merge(left, right)
def binary_search(_arr, x):
arr = sorted(_arr,key=len)
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if len(arr[mid]) < x:
low = mid + 1
elif len(arr[mid]) > x:
high = mid - 1
else:
return mid
return -1
def readFile():
with open('data.txt', "r") as file:
words = [word.rstrip() for word in file.readlines()]
#bubble_sort_by_length(words)
return sorted(words)
def addWord(words_to_add : list):
words = readFile()
added_words = []
word_exists = []
for word in words_to_add:
if word in words or word.isdigit():
word_exists.append(word)
continue
with open('data.txt', 'a') as file:
file.write(f'{word}\n')
added_words.append(word)
return [added_words, word_exists]
def removeWord(word_to_remove : list):
words = readFile()
removed_words = []
failed_to_remove = []
for word in word_to_remove:
if not word in words:
failed_to_remove.append(word)
continue
words.remove(word)
with open('data.txt', 'w') as file:
for item in words:
file.write(f'{item}\n')
removed_words.append(word)
return [removed_words, failed_to_remove]
def matchLetters(given):
words = readFile()
valids = []
for word in words:
count = Counter(given)
for letters in word:
if not count[letters]:
break
count[letters] -=1
else:
valids.append(word)
return valids
def create_string(words, perline = 30):
result = ''
total = perline
for word in words:
if len(result) + len(word) + 1 > total:
result += '\n'
total += perline
else:
result+= ' ' + word
return result
if __name__ == "__main__":
displayed = []
while True:
command = input("Enter [search/ add / remove / all / sort / quit]: ").split()
if command[0] == "all":
lst = readFile()
print(f"Total words:{len(lst)}")
displayed = lst.copy()
print(create_string(lst, 80))
elif command[0] == 'sort':
print(create_string(bubble_sort_by_length(displayed), 80))
elif command[0] == "quit":
print("BYEE!!!!")
break
if len(command) <= 1:
continue
if command[0] == "search":
lst = matchLetters(command[1])
displayed = lst.copy()
print(create_string(lst, 80))
elif command[0] == "add":
val = addWord(command[1:])
if val[0]:
print(f'Added: {", ".join(val[0])}')
if val[1]:
print(f'Failed to add: {", ".join(val[1])}')
elif command[0] == "remove":
val = removeWord(command[1:])
if val[0]:
print(f'Removed: {", ".join(val[0])}')
if val[1]:
print(f'Failed to remove: {", ".join(val[1])}')
else:
print("Invalid command. Please enter a valid command.")