-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleList.py
More file actions
172 lines (154 loc) · 5.93 KB
/
SimpleList.py
File metadata and controls
172 lines (154 loc) · 5.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Reader as r
import Parser as p
# list global variables
ListStorage = ["->"]
ListIndexes = [100]
def IndexOnList(Index):
ToReturn = ""
for n in ListIndexes:
if Index == n:
print(Index)
ToReturn = " Already Stored"
break
return ToReturn
# sorts and stores values on list, ordering them on the process
def StoreOnList(valueToStore, Pointer, ListSize):
for x in range(ListSize):
if ListSize > 1 and (x + 1) < ListSize and Pointer > ListIndexes[x] and Pointer < ListIndexes[x + 1]:
# adds new value on middle of positions that arent bigger or smaller than the value itself
ListIndexes.insert((x + 1), Pointer)
ListStorage.insert((x + 1), valueToStore)
break
elif Pointer < ListIndexes[x] and (x + 1) == ListSize:
# if the new value is smaller than all others stored, then it's going to be saved at the beggining of the list
ListIndexes.insert((x + 1), Pointer)
ListStorage.insert((x + 1), valueToStore)
elif Pointer > ListIndexes[x] and (x + 1) == ListSize:
# if the new value is greater than the ones already stored, then it's going to be stored at the end of the list
ListIndexes.insert((x + 1), Pointer)
ListStorage.insert((x + 1), valueToStore)
# fix issues
def StoreChars(IndexToStore, ListSize):
while True:
ItemToStore = input("CARACTERES A GUARDAR EN LISTA (MAX. 2): ")
if len(ItemToStore) > 0 and len(ItemToStore) < 3:
if(len(ItemToStore) == 1):
ItemToStore += " "
MsgToReturn = StoreOnList(ItemToStore, IndexToStore, ListSize)
break
elif ItemToStore == "":
print("NO SE PUEDE GUARDAR UN ELEMENTO VACIO!")
else:
print("INGRESE SOLO 2 CARACTERES!")
# adds elements to List
def AddToList(ListSize):
MsgToReturn = ""
#reads ID where new element is going to be saved
while True:
IndexToStore = r.intReader("ID DE ELEMENTO A AGREGAR (101 a 200): ", "ID INVALIDO")
if IndexToStore > 100 and IndexToStore <= 200:
break
else:
print("VALOR DE ID FUERA DE RANGO")
# checks if index has already been stored
MsgToReturn = IndexOnList(IndexToStore)
# if index is clear then saves elements to that position
if(MsgToReturn == ""):
StoreChars(IndexToStore, ListSize)
# Returns string
return MsgToReturn
def RemoveFromList(ListSize):
MsgToReturn = ""
if ListSize > 1:
IndexToRemove = 0
while True:
ItemToRemove = r.intReader("POSICION DE ELEMENTO A REMOVER: ", "VALOR NO VALIDO")
for x in range(ListSize):
if ItemToRemove == ListIndexes[x] and ItemToRemove > 100 and ItemToRemove <= 200:
if (x + 1) < ListSize and ItemToRemove == ListIndexes[x + 1]:
IndexToRemove = x + 1
break
else:
IndexToRemove = x
break
if IndexToRemove > 0:
break
else:
print("ID DE POSICION FUERA DE RANGO")
# deletes items requested on IndexToRemove at lists
ListStorage.pop(IndexToRemove)
ListIndexes.pop(IndexToRemove)
else:
MsgToReturn = " UNDERFLOW ERROR"
# Returns string
return MsgToReturn
def CleanList(ListSize):
MsgToReturn = ""
if ListSize > 1:
print("LIMPIANDO LISTA...")
for x in range(ListSize):
if x > 0:
ListStorage.pop(1)
ListIndexes.pop(1)
else:
MsgToReturn = " UNDERFLOW ERROR"
# Returns error string
return MsgToReturn
#Simple List
def ListaSimple():
IndexToRemove = 0
ListString = ""
UpperMiddleLine = ""
LowerMiddleLine = ""
ListSize = 1
x = 0
# Program loops until user chooses exit
while True:
ListSize = len(ListStorage)
if ListString == "":
for x in range(ListSize):
if x == 0:
ListString += str(ListIndexes[0]) + ListStorage[0]
else:
ListString += "[" + ListStorage[x] + "]:" + str(ListIndexes[x]) + "->"
# defines end of list
ListString += "NULL"
if ListSize < 3:
UpperMiddleLine = "┤"
LowerMiddleLine = "┤"
if (25 - len(ListString)) > 0:
ListString += (25 - len(ListString)) * " "
else:
UpperMiddleLine = "┴" + "─" * (len(ListString) - 26) + "┐"
LowerMiddleLine = "┬" + "─" * (len(ListString) - 26) + "┘"
if (25 - len(ListString)) > 0:
ListString += ((25 + 1) - len(ListString)) * " "
print(f'''
┌─────────────────────────┐
│ LISTA SIMPLE │
├─────────────────────────{UpperMiddleLine}
│{ListString}│
├─────────────────────────{LowerMiddleLine}
│ 1] VER LISTA SIMPLE │
│ 2] AGREGAR ELEMENTO │
│ 3] QUITAR ELEMENTO │
│ 4] LIMPIAR LISTA │
│ 0] SALIR │
└─────────────────────────┘''')
choice = r.ReadInt(0, 4)
ListString = ""
if (choice == 1):
continue
elif (choice == 2):
if ListSize < 7:
# stores position pointer
ListString = AddToList(ListSize)
else:
ListString = " OVERFLOW ERROR"
elif (choice == 3):
ListString = RemoveFromList(ListSize)
elif choice == 4:
ListString = CleanList(ListSize)
elif choice == 0:
print("VOLVIENDO A MENU...")
break