-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2WithMergeSortAttempt.py
More file actions
172 lines (150 loc) · 5.74 KB
/
lab2WithMergeSortAttempt.py
File metadata and controls
172 lines (150 loc) · 5.74 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
"""
Course: CS2302 Data Structures
Author: Javier Navarro
Assignment: Option B
Instructor: Diego Aguirre
TA: Manoj Saha
Last modified: 10/17/18
Purpose: Find 20 most common passwords in a given file
"""
def main():
dict = {} #Dictionary. Holds
validInput = False #used to exit loops if it's a valid input
head = None #used to hold head of list
sortedHead = None #used to hold head of sorted list
while(validInput == False):
print("Would you like to create a linked list using solution A or B?")
userInput = input("Enter 'A' for without a dictionary or 'B' for with a dictionary: ")
userInput = userInput.lower()
if(userInput != 'a' and userInput != 'b'):
validInput = False
print("Error. The only options are 'a' and 'b'. Try again.")
else:
validInput = True
validInput = False
while(validInput == False):
fileName = input("Enter the name of your file: ")
try:
if(userInput == 'a'):
head = createListSolA(fileName)
else:
head = createListSolB(fileName, dict)
validInput = True
except FileNotFoundError:
print("File not found. Try again.")
validInput = False
while(validInput == False):
print("Would you like to sort the list using solution A or B?")
userInput = input("Enter 'A' for Bubble Sort or 'B' for Merge Sort: ")
userInput = userInput.lower()
if(userInput != 'a' and userInput != 'b'):
validInput = False
print("Error. The only options are 'a' and 'b'. Try again.")
else:
validInput = True
if(userInput == 'a'):
sortedHead = bubbleSort(head)
else:
sortedHead = mergeSort(head) #Incomplete
printTop20(sortedHead)
def createListSolA(fileName):
head = None #used to hold the head of the list
temp = None #used to traverse list
found = False #used if password is found in list
with open(fileName, "r") as file: #opens file with given fileName
for i in file: #i holds each line in the file
found = False
line = i.split() #splits i into an array
try:
while(temp != None and found != True):
if(temp.password == line[1]): #if password already in list
temp.count += 1
found = True
temp = temp.next
if(found == False):
head = Node(line[1], 1, head)
temp = head
except IndexError:
pass
return head
def createListSolB(fileName, dict):
head = None #used to create list
with open(fileName, "r") as file: #opens file with given fileName
for item in file: #item holds each line in the file
line = item.split() #splits item into an array
try:
if(line[1] in dict): #if password is already in dictionary
dict[line[1]].count += 1 #increment value for password in dictionary
else:
head = Node(line[1], 1, head)
dict[line[1]] = head #dictionary stores new node
except IndexError:
pass
return head
def bubbleSort(head):
swap = True #holds whether there was a swap or not
current = head #used to traverse list
while(swap == True): #loops until there's no swaps left
swap = False
while(current != None and current.next != None):
if(current.count < current.next.count):
temp = current.count #next 3 statements swap the values
current.count = current.next.count
current.next.count = temp
swap = True
current = current.next
current = head #set to head to traverse again
return current
def mergeSort(head):
previousHead = head
mid = getSize(head) // 2
if(head == None or head.next == None):
return head
while mid - 1 > 0:
previousHead = previousHead.next
mid -= 1
newHead = previousHead.next
previousHead.next = None
previousHead = head
t1 = mergeSort(previousHead)
t2 = mergeSort(newHead)
return mergeList(t1, t2)
def mergeList(nodeA, nodeB):
result = None
if nodeA == None:
return nodeB
if nodeB == None:
return nodeA
if(nodeA.count > nodeB.count):
result = nodeB
result.next = mergeList(nodeA, nodeB.next)
else:
result = nodeA
result.next = mergeList(nodeA.next, nodeB)
return result
def getSize(head):
counter = 0
temp = head
while temp != None:
counter += 1
temp = temp.next
return counter
def printTop20(head):
temp = head #used to traverse list
counter = 0 #used to keep track of top 20
while(temp != None and counter < 20):
counter += 1
print("Password ", counter, ": ", temp.password)
print("Number of times repeated: ", temp.count)
print("-------------------------------")
temp = temp.next
#Class given through the pdf
class Node(object):
password = ""
count = -1
next = None
def __init__(self, password, count, next):
self.password = password
self.count = count
self.next = next
main()