-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.py
294 lines (244 loc) · 7.62 KB
/
Day5.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from QueueADT import Queue
import random
from DequeADT import Deque
# class Printer:
#
# def __init__(self, ppm):
# self.page_rate = ppm
#
# self.current_task = None
#
# self.time_remaining = 0
#
# def tick(self):
# if self.current_task != None:
# self.time_remaining = self.time_remaining - 1
# if self.time_remaining <= 0:
# self.current_task = None
#
# def busy(self):
# if self.current_task != None:
# return True
# else:
# return False
#
# def start_next(self, new_task):
# self.current_task = new_task
# self.time_remaining = new_task.get_pages() * 60 / self.page_rate
#
#
# class Task:
#
# def __init__(self, time):
# self.timestamp = time
#
# self.pages = random.randrange(1, 21)
#
# def get_stamp(self):
# return self.timestamp
#
# def get_pages(self):
# return self.pages
#
# def wait_time(self, current_time):
# return current_time - self.timestamp
#
#
# def simulation(num_seconds, pages_per_minute):
#
# lab_printer = Printer(pages_per_minute)
#
# print_queue = Queue()
#
# waiting_times = []
#
# for current_second in range(num_seconds):
# if new_print_task():
# task = Task(current_second)
#
# print_queue.enqueue(task)
#
# if (not lab_printer.busy()) and (not print_queue.is_empty()):
# next_task = print_queue.dequeue()
#
# waiting_times.append(next_task.wait_time(current_second))
#
# lab_printer.start_next(next_task)
#
# lab_printer.tick()
# average_wait = sum(waiting_times) / len(waiting_times)
#
# print("Average Wait %6.2f secs %3d tasks remaining." % (average_wait, print_queue.size()))
#
# def new_print_task():
# num = random.randrange(1, 181)
# if num == 180:
# return True
# else:
# return False
#
# for i in range(10):
# simulation(3600,5)
# deque is an ordered collection of items where items can be added and removed at either rear or front
# items remained ordered in collection, provides both capabilities of stack and queue
# add_front(item) adds item to front, add_rear adds it to rar
# remove_front() and remove_rear() returns and removes item
# is_empty returns true or false
# size() returns items in deque
# palindrome is string that reads same forward and backwards
def pal_checker(a_string):
char_deque = Deque()
for ch in a_string:
char_deque.add_rear(ch)
still_equal = True
while char_deque.size() > 1 and still_equal:
first = char_deque.remove_front()
last = char_deque.remove_rear()
if first != last:
still_equal = False
return still_equal
# list or unordered list is a collection of tems where each items holds an index
# fr simplicity, assume that lists cant contain duplicate items
# append(item) adds to end, index(item) gives you index, remove(item) removes it from list, search(item) returns True or False
# size(), add(item) adds it to a random position, pop() removes that item in list, pop(pos) removes and returns item in pos
class Node:
def __init__(self, init_data):
self.data = init_data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = new_data
def set_next(self, new_next):
self.next = new_next
class UnorderedList:
def __init__(self):
self.head = None
self.firstItem = self.head
self.lastItem = None
def is_empty(self):
return self.head == None
def add(self, item):
temp = Node(item)
temp.set_next(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
print(current.get_data)
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.get_data() == item:
found = True
else:
print(current.get_data)
current = current.get_next()
return found
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
def Insert(self, data):
temp = Node(data)
temp.set_next(self.head)
if (temp == None):
self.head = Node(data)
else:
current = self.head
while (current.next != None):
current = current.next
current.next = Node(data)
myList = UnorderedList()
myList.add(31)
myList.add(85)
myList.add(93)
print(myList.search(25))
myList.size()
# (myList.Insert(100))
# size search and remove are based on linked list traversal
# traversal refers to process of systemically visiting every node
# size method traverses list and counts number of nodes visited
# ordered lists is where items hold a position based on a characteristic
# usually ascending or descending order
class OrderedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
print(current.get_data)
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.get_data() == item:
found = True
else:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
return found
def add(self, item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next()
temp = Node(item)
if previous == None:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
list1 = OrderedList()
list1.add(99)
list1.add(90)
list1.add(11)
list1.add(45)
print(list1.is_empty())
print(list1.size())