-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveListSorter.py
More file actions
109 lines (99 loc) · 4.44 KB
/
Copy pathInteractiveListSorter.py
File metadata and controls
109 lines (99 loc) · 4.44 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
import itertools as it
from random import shuffle
from os import system, get_terminal_size
def printProgressBar (iteration: int, total: int, prefix: str = '', suffix: str = '', decimals: int = 1, length: int = 100, fill: str = '█', printEnd: str = "\r") -> None:
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
newline = f'{percent}%{suffix}'.center(get_terminal_size().columns)
print(f'\r{prefix}|{bar}|\n{newline}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
def interactiveListSorter(dictionary: dict[str,int]) -> dict[str,int]:
comp_list = list(it.combinations(dictionary, 2))
shuffle(comp_list)
list_len = len(comp_list)
for index in range(list_len):
system("clear")
printProgressBar(iteration=index, total=list_len, suffix=" Complete", length=((get_terminal_size().columns)-2), printEnd="\n")
while True:
try:
pref = int(input(f'Type "1" if you prefer {comp_list[index][0]} or "2" if you prefer {comp_list[index][1]}\n'))
if pref != 1 and pref != 2:
print('Please enter a valid response')
continue
break
except ValueError:
print('Please enter a valid response')
continue
score = dictionary.get(comp_list[index][pref - 1])
score += 1
dictionary.update({comp_list[index][pref - 1]: score})
return dict(sorted(dictionary.items(), key=lambda item: item[1], reverse=True))
def manual_list() -> dict[str,int]:
things = {}
user_input = input('Type what you want to put in the list; after one entry, press enter; when you\'re done, press enter without having typed anything:\n')
while user_input != '':
things.update({user_input : 0})
user_input = input()
return things
def auto_list(filename: str) -> dict[str,int]:
with open(filename, "r") as file:
text = file.read()
keys = text.splitlines()
values = [0] * len(keys)
return dict(zip(keys, values))
if __name__ == '__main__':
while True:
try:
choice = int(input(f'Type "1" if you want to manually enter a list of items or "2" if you want to get the list of items from an external file\n'))
if choice != 1 and choice != 2:
print('Please enter a valid response')
continue
break
except ValueError:
print('Please enter a valid response')
continue
if choice == 1:
things = manual_list()
elif choice == 2:
while True:
try:
filename = str(input('Type the name of the file here (if the file is not in the same directory as this script, then please type the full path):\n')).strip()
things = auto_list(filename)
break
except FileNotFoundError:
print('Either the path was typed incorrectly or the file cannot be found. Please check the path name and try again.')
continue
sorted_things = interactiveListSorter(things)
sorted_things_as_list = list(sorted_things)
#print(sorted_things)
#print(sorted_things_as_list)
system("clear")
print('Here is how your list of items rank according to your preferences:')
rank = 1
rankskip = 1
for j in range(len(sorted_things_as_list)):
print(f'{rank}. {sorted_things_as_list[j]}')
try:
if sorted_things.get(sorted_things_as_list[j]) > sorted_things.get(sorted_things_as_list[j + 1]):
rank += rankskip
rankskip = 1
else:
rankskip += 1
except IndexError:
break
#