-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
49 lines (37 loc) · 1.49 KB
/
main.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
"""
License: Apache
Organization: UNIR
"""
import os
import sys
DEFAULT_FILENAME = "words.txt"
DEFAULT_DUPLICATES = False
def sort_list(items, ascending=True):
if not isinstance(items, list):
raise RuntimeError(f"No puede ordenar {type(items)} / It's not possible to order {type(items)}")
return sorted(items, reverse=(not ascending))
def remove_duplicates_from_list(items):
return list(set(items))
if __name__ == "__main__":
filename = DEFAULT_FILENAME
remove_duplicates = DEFAULT_DUPLICATES
if len(sys.argv) == 3:
filename = sys.argv[1]
remove_duplicates = sys.argv[2].lower() == "yes"
else:
print("Se debe indicar el fichero como primer argumento / You should indicate the file as first argument")
print("El segundo argumento indica si se quieren eliminar duplicados / Second argument indicates if you want to delete duplicates")
sys.exit(1)
print(f"Se leerán las palabras del fichero {filename} / File to read {filename}")
file_path = os.path.join(".", filename)
if os.path.isfile(file_path):
word_list = []
with open(file_path, "r") as file:
for line in file:
word_list.append(line.strip())
else:
print(f"El fichero {filename} no existe / File {filename} doesn't exist")
word_list = ["ravenclaw", "gryffindor", "slytherin", "hufflepuff"]
if remove_duplicates:
word_list = remove_duplicates_from_list(word_list)
print(sort_list(word_list))