PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand
LinkedList ADT:
class myLinkedList:
def __init__(self):
self.__head =
None
self.__tail =
None
self.__size =
0
def insert(self, i,
data):
if
self.isEmpty():
self.__head = listNode(data)
self.__tail = self.__head
elif
i <= 0:
self.__head = listNode(data, self.__head)
elif
i >= self.__size:
self.__tail.setNext(listNode(data))
self.__tail = self.__tail.getNext()
else:
current = self.__getIthNode(i - 1)
current.setNext(listNode(data, current.getNext()))
if
current == self.__tail:
self.__tail = self.__tail.getNext()
self.__size += 1
def __getIthNode(self,
i):
current =
self.__head
count = 0
while
current is not None and count < i:
count = count + 1
current = current.getNext()
return
current
def __str__(self):
current =
self.__head
result =
""
while current is not None:
result = result + " " +
str(current.getData())
current = current.getNext()
return
result
#returns True if list is empty, else
False
def isEmpty(self):
return
self.__head==None
#returns the length
def __len__(self):
'''count =
0
current=self.__head
#counting number of
nodes
while current is not
None:
count = count + 1
current = current.getNext()
#returning count
return count'''
return self.__size
#adds to the front
def prepend(self,
item):
#adding the
item at index 0
self.insert(0,item)
#adds to the rear
def
append(self,item):
#adding the
item at the end
self.insert(len(self),item)
#removes and returns the element at
index i
def pop(self, i):
#validating
index
if
i<0 or i>=len(self):
return
None #invalid index
#finding ith node
node=self.__getIthNode(i)
#if it is head,
updating head node
if
node==self.__head:
self.__head=self.__head.getNext()
self.__size-=1
return
node.getData() #returning removed data
#finding previous
node
prev=self.__getIthNode(i-1)
#linking node
at i+1 as next node of node at i-1
prev.setNext(node.getNext())
self.__size -= 1
return
node.getData() #returning removed data
#helps to access data by index (using [])
def __getitem__(self,
index):
#returning data
of node at index
return self.__getIthNode(index).getData()
#helps to set data by index (using
[])
def __setitem__(self,
index, value):
#updates the
data of node at index
self.__getIthNode(index).setData(value)
#returns the index of an item, if
exists
def find(self,item):
#looping and
comparing each item with target
for i in range(len(self)):
if
self.__getitem__(i)==item:
#found
return i
return
-1 #not found
student class ADT:
class Student:
def __init__(self, id=None, name=""):
self.id = id
self.name = name
def getID(self):
return self.id
def getName(self):
return self.name
def setID(self, id):
self.id = id
def setName(self, name):
self.name = name
def __str__(self):
#print out
print(str(self.id) + "(" + self.name+")")
def __cmp__(self,other_id):
ot_id = other_id.getID()
if (self.id < ot_id):
p = -1
elif (self.id > ot_id):
p = 1
elif (self.id == ot_id):
p = 0
return p
def __eq__(self,other_id):
x = self.__cmp__(other_id)
if x == 0:
print(True)
else:
print(False)
def __ne__(self,other_id):
x = self.__cmp__(other_id)
if x == 0:
print(False)
else:
print(True)
def __lt__(self,other_id):
x = self.__cmp__(other_id)
if x == -1:
print(True)
else:
print(False)
def __le__(self,other_id):
x = self.__cmp__(other_id)
if x == 0 or x==-1:
print(True)
else:
print(False)
def __gt__(self,other_id):
k = self.__cmp__(other_id)
if (k == 1):
print(True)
else:
print(False)
def __ge__(self,other_id):
k = self.__cmp__(other_id)
if (k == 0 or k==1):
print(True)
else:
print(False)
LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below)
here is the answer ............
class myLinkedList: #this is for the creation
of linked list..
def __init__(self):
self.__head =
None
self.__tail =
None
self.__size =
0
def insert(self, i, data):
#this function is used for the inserting the data in the linked
list...
if
self.isEmpty():
self.__head = listNode(data)
self.__tail = self.__head
elif
i <= 0:
self.__head = listNode(data, self.__head)
elif
i >= self.__size:
self.__tail.setNext(listNode(data))
self.__tail = self.__tail.getNext()
else:
current = self.__getIthNode(i - 1)
current.setNext(listNode(data, current.getNext()))
if
current == self.__tail:
self.__tail = self.__tail.getNext()
self.__size += 1
def __getIthNode(self, i):
#this is for the getting requested node..
current =
self.__head
count = 0
while
current is not None and count < i:
count = count + 1
current = current.getNext()
return
current
def __str__(self): #this
is for the current head node
current =
self.__head
result =
""
while current is not None:
result = result + " " +
str(current.getData())
current = current.getNext()
return
result
#returns True if list is empty, else
False
def isEmpty(self):
return
self.__head==None
#returns the length
def __len__(self):
'''count =
0
current=self.__head
#counting number of
nodes
while current is not
None:
count = count + 1
current = current.getNext()
#returning count
return count'''
return self.__size
#adds to the front
def prepend(self,
item):
#adding the
item at index 0
self.insert(0,item)
#adds to the rear
def
append(self,item):
#adding the
item at the end
self.insert(len(self),item)
#removes and returns the element at
index i
def pop(self, i):
#validating
index
if
i<0 or i>=len(self):
return
None #invalid index
#finding ith node
node=self.__getIthNode(i)
#if it is head,
updating head node
if
node==self.__head:
self.__head=self.__head.getNext()
self.__size-=1
return
node.getData() #returning removed data
#finding previous
node
prev=self.__getIthNode(i-1)
#linking node
at i+1 as next node of node at i-1
prev.setNext(node.getNext())
self.__size -= 1
return
node.getData() #returning removed data
#helps to access data by index (using [])
def __getitem__(self,
index):
#returning data
of node at index
return self.__getIthNode(index).getData()
#helps to set data by index (using
[])
def __setitem__(self,
index, value):
#updates the
data of node at index
self.__getIthNode(index).setData(value)
#returns the index of an item, if
exists
def find(self,item):
#looping and
comparing each item with target
for i in range(len(self)):
if
self.__getitem__(i)==item:
#found
return i
return
-1 #not found
student class ADT:
class Student:
def __init__(self, id=None, name=""):
self.id = id
self.name = name
def getID(self):
return self.id
def getName(self):
return self.name
def setID(self, id):
self.id = id
def setName(self, name):
self.name = name
def __str__(self):
#print out
print(str(self.id) + "(" + self.name+")")
def __cmp__(self,other_id):
ot_id = other_id.getID()
if (self.id < ot_id):
p = -1
elif (self.id > ot_id):
p = 1
elif (self.id == ot_id):
p = 0
return p
def __eq__(self,other_id):
x = self.__cmp__(other_id)
if x == 0:
print(True)
else:
print(False)
def __ne__(self,other_id):
x = self.__cmp__(other_id)
if x == 0:
print(False)
else:
print(True)
def __lt__(self,other_id):
x = self.__cmp__(other_id)
if x == -1:
print(True)
else:
print(False)
def __le__(self,other_id):
x = self.__cmp__(other_id)
if x == 0 or x==-1:
print(True)
else:
print(False)
def __gt__(self,other_id):
k = self.__cmp__(other_id)
if (k == 1):
print(True)
else:
print(False)
def __ge__(self,other_id):
k = self.__cmp__(other_id)
if (k == 0 or k==1):
print(True)
else:
print(False)
PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...
PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...
Task 2: SecretWord class:
Download and save a copy of LinkedLists.py (found at the bottom
of this assignment page on eClass). In this file, you have been
given the complete code for a LinkedList class. Familiarize
yourself with this class, noticing that it uses the Node class from
Task 1 and is almost identical to the SLinkedList class given in
the lectures. However, it also has a complete insert(pos, item)
method (which should be similar to the insert method you...
I am having trouble with my Python code in this dictionary and
pickle program. Here is my code but it is not running as i am
receiving synthax error on the second line.
class Student:
def__init__(self,id,name,midterm,final):
self.id=id
self.name=name
self.midterm=midterm
self.final=final
def calculate_grade(self):
self.avg=(self.midterm+self.final)/2
if self.avg>=60 and self.avg<=80:
self.g='A'
elif self.avg>80 and self.avg<=100:
self.g='A+'
elif self.avg<60 and self.avg>=40:
self.g='B'
else:
self.g='C'
def getdata(self):
return self.id,self.name.self.midterm,self.final,self.g
CIT101 = {}
CIT101["123"] = Student("123", "smith, john", 78, 86)
CIT101["124"] = Student("124", "tom, alter", 50,...
Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...
Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...
Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...
class Book: def __init__(self,id,bookName,authorName,nextNode=None): self.id = id self.bookName = bookName self.authorName = authorName self.nextNode = nextNode def getId(self): return self.id def getBookName(self): return self.bookName def getAuthorName(self): return self.authorName def getNextNode(self): return self.nextNode def setNextNode(self,val): self.nextNode = val class LinkedList: def __init__(self,head = None): self.head = head self.size = 0 def getSize(self): return self.size def AddBookToFront(self,newBook): newBook.setNextNode(self.head) self.head = newBook self.size+=1 def DisplayBook(self): curr = self.head while curr: print(curr.getId(),curr.getBookName(),curr.getAuthorName()) curr = curr.getNextNode() def RemoveBookAtPosition(self,n): prev = None curr = self.head curPos = 0 while curr: if curPos == n: if prev: prev.setNextNode(curr.getNextNode()) else: self.head = curr.getNextNode() self.size = self.size - 1 return True prev = curr curr = curr.getNextNode() curPos = curPos + 1 return False def AddBookAtPosition(self,newBook,n): curPos = 1 if n == 0: newBook.setNextNode(self.head) self.head = newBook self.size+=1 return else: currentNode = self.head while currentNode.getNextNode() is not None: if curPos == n: newBook.setNextNode(currentNode.getNextNode()) currentNode.setNextNode(newBook) self.size+=1 return currentNode = currentNode.getNextNode() curPos = curPos + 1 if curPos == n: newBook.setNextNode(None) currentNode.setNextNode(newBook) self.size+=1 else: print("cannot add",newBook.getId(),newBook.getBookName(),"at that position") def SortByAuthorName(self): for i in range(1,self.size): node1 = self.head node2 = node1.getNextNode() while node2 is not None: if node1.authorName > node2.authorName: temp = node1.id temp2 = node1.bookName temp3 = node1.authorName node1.id = node2.id node1.bookName = node2.bookName node1.authorName = node2.authorName node2.id = temp node2.bookName = temp2 node2.authorName = temp3 node1 = node1.getNextNode() node2 = node2.getNextNode() myLinkedList = LinkedList() nodeA = Book("#1","cool","Isaac") nodeB = Book("#2","amazing","Alfred") nodeC = Book("#3","hello","John") nodeD = Book("#4","why","Chase") nodeE = Book("#5","good","Mary") nodeF = Book("#6","hahaha","Radin") myLinkedList.AddBookToFront(nodeA) myLinkedList.AddBookToFront(nodeB) myLinkedList.AddBookToFront(nodeC) myLinkedList.AddBookAtPosition(nodeD,1) myLinkedList.AddBookAtPosition(nodeE,1) myLinkedList.AddBookAtPosition(nodeF,1) myLinkedList.RemoveBookAtPosition(2) myLinkedList.RemoveBookAtPosition(2) myLinkedList.DisplayBook() myLinkedList.SortByAuthorName()print(myLinkedList.getSize())...
I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...