-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (88 loc) · 2.25 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
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
import random
def intInput(text: str = "number: "):
a = input(text)
num = None
while num == None:
try:
num = int(a)
except:
a = input(text)
return num
def ask_for_new_num(arr: list[int]):
new = intInput()
arr.append(new)
def remove_last(arr: list[int]):
if len(arr) == 0:
print("im empty dumbass")
return
arr.pop()
def remove_first(arr: list[int]):
if len(arr) == 0:
print("im empty dumbass")
return
for i in range(1, len(arr)):
arr[i - 1] = arr[i]
arr.pop()
def add_first(arr: list[int]):
# duplicate last element
arr.append(arr[-1])
# loop backwards from penultimate element to start (exclusive)
for i in range(len(arr) - 2, 0, -1):
arr[i] = arr[i - 1]
new = intInput()
arr[0] = new
def remove_anywhere(arr: list[int]):
idx = intInput("index: ")
# make sure idx is valid according to the list
while idx < 0 or idx > len(arr) - 1:
idx = intInput()
# Loop from index (inclusive) to penultimate element
for i in range(idx, len(arr) - 1):
arr[i] = arr[i + 1]
arr.pop()
def add_anywhere(arr: list[int]):
idx = intInput("index: ")
# make sure idx is valid according to the list
while idx < 0 or idx > len(arr) - 1:
idx = intInput()
new_val = intInput()
# duplicate last element
arr.append(arr[-1])
# loop backwards from penultimate to idx (exlusive)
for i in range(len(arr) - 2, idx, -1):
arr[i] = arr[i - 1]
arr[idx] = new_val
arr = [random.randint(1, 100) for _ in range(10)]
print(
"""Help:
x = exit
n = new
p = print
o = remove last
z = remove first
a = add first
r = remove anywhere
h = add anywhere"""
)
command: str = ""
while command != "x":
print(arr)
command = input()
if command == "n":
ask_for_new_num(arr)
if command == "p":
print(arr)
if command == "o":
remove_last(arr)
if command == "z":
remove_first(arr)
if command == "a":
add_first(arr)
if command == "r":
remove_anywhere(arr)
if command == "h":
add_anywhere(arr)
# Add to the beginning ✅
# Remove from the beginning ✅
# Add anywhere ✅
# Remove from anywhere ✅