-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs_lab2_solutions.py
More file actions
195 lines (175 loc) · 5.07 KB
/
cs_lab2_solutions.py
File metadata and controls
195 lines (175 loc) · 5.07 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Govinda KC
# CS 2302 lab2
# import sys to increase the recursion limit.
import sys
sys.setrecursionlimit(6500)
# Define class to create nodes
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Creating the class for linked list
class LinkedList:
def __init__(self):
self.head = None
# method to print the linked list
def printList(self,n=-1):
temp = self.head
count = 0
while temp:
if count == n:
break
print(temp.data)
temp = temp.next
count+=1
# Defining the method to create a node at the end of the linked list
def append(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
# reading files
def read_file(filename):
text_file = open(filename, 'r')
l_list = text_file.read().splitlines()
return l_list
# Creating the linked list of the one single file
def get_linked_list(a_list):
list1 = LinkedList()
for i in range(len(a_list)):
list1.append(int(a_list[i])) # use float if you have floating num
return list1
# Defining function which will merge two linked lists
def mergeLists(list1, list2):
temp = None
if list1 is None:
return list2
if list2 is None:
return list1
if list1.data <= list2.data:
temp = list1
temp.next = mergeLists(list1.next, list2)
else:
temp = list2
temp.next = mergeLists(list1, list2.next)
return temp
# Solution 3
# Defining function which will sort the linked list using mergeSort
def mergeSort(head):
if head is None or head.next is None:
return head
list1, list2 = divideLists(head)
list1 = mergeSort(list1)
list2 = mergeSort(list2)
head = mergeLists(list1, list2)
return head
# Defining function which will divide a linked list into two equal linked lists
def divideLists(head):
slow = head # slow is a pointer to reach the mid of linked list
fast = head # fast is a pointer to reach the end of the linked list
if fast:
fast = fast.next
while fast:
fast = fast.next # fast is increased by 2 times than slow.
if fast:
fast = fast.next
slow = slow.next
mid = slow.next
slow.next = None
return head, mid
# Solution 2
def bubbleSort(head):
if head is None or head.next is None:
return head
else:
sorted = False
while not sorted:
sorted = True
prev = head
current = head.next
while current!=None:
if prev.data > current.data:
sorted = False
temp = current.data
current.data = prev.data
prev.data = temp
prev = current
current = current.next
return head
def count_sorted(head):
if head is None:
return 0
elif head.next is None:
return 1
duplicates = 0
temp = []
current = head
while current.next != None:
next = current.next
if next.data == current.data:
temp.append(current.data)
duplicates += 1
current = current.next
temp.sort()
print("Number of duplicates = "+str(duplicates))
# Solution 1
def count_nested(head):
if head is None:
return 0
elif head.next is None:
return 1
duplicates = 0
temp = []
current1 = head
while current1 !=None:
#data = head.data
data = current1.data
current2 = current1.next
while current2!=None:
if current2.data == data:
temp.append(data)
duplicates += 1
current2 = current2.next
current1 = current1.next
temp.sort()
print(temp)
print("Number of duplicates = " + str(duplicates))
# Solution 4
def seen(head):
numDuplicates = 0
numComparisions = 0
seenBefore = [False]*6001
temp = head
list1=[]
while temp is not None:
numComparisions +=1
if seenBefore[temp.data] ==True:
numDuplicates +=1
list1.append(True)
else:
list1.append(False)
seenBefore[temp.data] = True
temp = temp.next
print(list1)
return {numComparisions, numDuplicates}
# Main logic
if __name__ == '__main__':
a_list = read_file('activision.txt')
v_list = read_file('vivendi.txt')
a_list.extend(v_list)
list = get_linked_list(a_list)
#print("Linked list before sorting")
#list.printList()
#list.head = bubbleSort(list.head) # Applying bubbleSort to linked list
#list.head = mergeSort(list.head) # Applying mergeSort to linked list
#print("Linked list after sorting")
#list.printList()
#print('-----------')
#count_sorted(list.head)
count_nested(list.head)
#out1, out2=seen(list.head)
#print(out1, out2)