-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebLog.py
More file actions
91 lines (68 loc) · 2.87 KB
/
Copy pathWebLog.py
File metadata and controls
91 lines (68 loc) · 2.87 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
# DailyCoding
##
##We use a Linked Data Structure
class Node:
'''Node Object'''
def __init__(self, val, nextVal):
self.val = val
self.nextVal = nextVal
class LinkedList:
"""Simple LinkedList Data Structure"""
def __init__(self):
self.previous, self.current = None, None # instantiate current and previous Nodes
self.head, self.tail = None, None ##instantiate head and tail as null Values
def add(self, val):
"""Add val to List"""
if self.head == None: # if linked List is empty
self.previous = Node(val, self.tail) # append first element in list
self.current = self.previous # set current equal to first element; previous
self.head = Node(None, self.previous) # redefine Val of Head Variable to reference previous Node
##Otherwise if list is not empty
elif self.current.nextVal == self.tail:
self.current.nextVal = Node(val, self.tail) # add new value to end of Linked DataSet
else: ##else, update the value of current to the last nonempty Node
while self.current.nextVal != self.tail: self.current = self.current.nextVal
self.current.nextVal = Node(val, self.tail) ##append new val to end of list
def isEmpty(self):
"""Evaluate if List is Empty or Not"""
if self.head is None: return True
return False
def get(self, ith_val):
"""Return ith val in list"""
if self.isEmpty(): return None # return None Object if list is Empty
self.current, ithVal = self.previous, 1 ##set current to first value in list. So ithVal is at 1
while ithVal != ith_val:
self.current = self.current.nextVal
ithVal += 1
return self.current.val
class WebLog:
"""Log for Web order"""
def __init__(self):
self.__log = LinkedList() # instantiate the log dataSet
def record(self, order_id):
'''add order id to log'''
self.__log.add(order_id)
def get_Last(self, ithOrder):
"""return the ith order in List"""
return self.__log.get(ithOrder)
# ###Test
# orders = WebLog()
# for num in range(10,21): orders.record(num)
# print(orders.get_Last(4))
# print(orders.get_Last(10))
#################################################################################################################
"""This could be simply implemented with a List"""
class WebLog2:
def __init__(self):
self.log = [] ##instantiate list to record data
def record(self, order_id):
self.log.append(order_id)
def get_last(self, ithOrder):
return self.log[ithOrder]
###Test WebLog2
logData = WebLog2
for n in range(50, 101):
logData.record(n)
print(logData.get_last(4))
print(logData.get_last(8))
print(logData.get_last(3))