Question: I just need help fixing the Node class and the Insert function. I believe that self.prev and self.next keeps resetting so the node isn't working
I just need help fixing the Node class and the Insert function. I believe that self.prev and self.next keeps resetting so the node isn't working properly for this ordered linked list. The only value that's printed out is the last value in the data.txt file There are 3 modules which I put in order: courselist.py course.py main.py. All of these work in conjunction with each other. main and course don't need any fixing. courselist is the first one and is the main issue.
(data.txt:
"1400,Introduction to Programming,4,3.6
1410,C++ Programming,4,2.6
2810,Computer Architecture,3,3.8
2420,Introduction to Data Structures,4,3.2
1030,Introduction to Computers,2,3.2")
class Node:
"""I'm not sure if I need this. course might work as a node by itself"""
def __init__(self, course):
#issue where .prev and .next keep resetting. Need to find a workaround
self.prev = None
self.next = None
self.value = course.number()
self.course = course
def __str__(self):
"""returns the result"""
return str(self.course)
class CourseList:
"""Takes the values from Course and puts it all in an ordered linked list"""
def __init__(self):
"""initialize the variables we need"""
self.first = None
self.last = None
self.count = 0
self.value = 0
def insert(self, course):
"""inserts the value and node from course for the linked list"""
course = Node(course)
if self.first is None:
self.first = course
self.last = course
elif self.last.value < course.value:
self.last = self.fix_connection(self.last, course, None)
elif self.first.value >= course.value:
self.first = self.fix_connection(None, course, self.first)
else:
current = self.first
current.value = self.first.value
while current.value < course.value:
current = current.next
self.fix_connection(current.prev, course, current)
self.count += 1
def fix_connection(self, previous, current, after):
"""restores the links between nodes to make sure the list isn't broken"""
if current is None:
if previous is not None:
previous.next = after
if after is not None:
after.prev = previous
else:
if previous is not None:
previous.next = current
elif after is not None:
after.prev = current
return current
def remove(self, number):
"""removes the specific number (first 4 digits of the object) from the list"""
if number <= self.last.value and number >= self.first.value:
current = self.first
while current.value != number and current.next is not None:
current = current.next
if current.value == number:
self.eradicate(current)
def remove_all(self, number):
"""removes all instances of the number from the list"""
current = self.first
while current.next is not None:
if current.value == number:
self.eradicate(current)
current = current.next
def find(self, number):
"""finds the number in the linked list and returns the object reference"""
if number <= self.last.value and number >= self.first.value:
current = self.first
while current.value != number and current.next is not None:
current = current.next
if current.value == number:
return current
return -1
def size(self):
"""returns the size of the list"""
return self.count
def calculate_gpa(self):
"""calculates the GPA based on the last numbers of the object"""
current = self.first
gpa = 0
temp = str(current)
while current.next is not None:
temp = str(current)
gpa += float(temp[-3:])
current = current.next
return gpa/self.count
def is_sorted(self):
"""makes sure the list is actually sorted"""
current = self.first
while current.next is not None:
temp = current.next
if current.value > temp.value:
return False
return True
def eradicate(self, current):
"""removes the item (current) from the ordered linked list """
if current == self.last:
self.last == current.prev
if current == self.first:
self.first = current.next
self.fix_connection(current.prev, None, current.next)
self.count = self.count - 1
def __str__(self):
"""prints the linked list"""
#needs fixing in the f-string
finished = ""
current = self.first
while current is not None:
finished += str(current)
current = current.next
return finished
def __iter__(self):
"""can iterate through the list"""
self.currentiteration = self.first
return self
def __next__(self):
"""next moves one forward in the list"""
if self.currentiteration is not None:
self.currentiteration = self.currentiteration.next
if self.currentiteration is None:
raise StopIteration()
return self.currentiteration()
else:
raise StopIteration()
Course is the class that obtains the info from each class mentioned
in data.txt
"""
class Course:
"""gets the values from data.txt"""
def __init__(self, num=0, nam="", crd=0.0, grd=0.0):
"""initialize the class"""
self.next = None
self.previous = None
self.num = num
self.nam = nam
self.crd = crd
self.grd = grd
self.number_value = self.number()
self.name_value = self.name()
self.credit_value = self.credit_hr()
self.grade_value = self.grade()
self.results = self.__str__()
def number(self):
"""gets the number from the txt file"""
try:
if self.num == float:
raise ValueError
self.num = int(self.num)
except (TypeError, ValueError, IndexError):
raise ValueError
return self.num
def name(self):
"""makes sure it's a string"""
try:
self.nam = str(self.nam)
except (TypeError, ValueError, IndexError):
raise ValueError
return self.nam
def credit_hr(self):
"""makes sure credit is a number and returns the value"""
try:
self.crd = float(self.crd)
except (TypeError, ValueError, IndexError):
raise ValueError
return self.crd
def grade(self):
"""obtains the grade if it's less than 4.0 and greater than 0
(or equal to)"""
try:
if float(self.grd) <= 4.0 and float(self.grd) >= 0:
self.grd = float(self.grd)
else:
raise ValueError
except (TypeError, ValueError, IndexError):
raise ValueError
return self.grd
def __str__(self):
"""returns all values as a string"""
self.done = (f"{self.number_value} {self.name_value} Grade: {self.credit_value} Credit Hours: {self.grade_value}")
return str(self.done)
from courselist import CourseList as cl
from course import Course as c
def main():
"""driver function"""
data = open("data.txt").read()
data = data.split(" ")
chad = cl()
for i in data:
i = i.split(",")
try:
results = c(i[0], i[1], i[2], i[3])
except IndexError:
pass
chad.insert(results)
print(f"Current List:{chad.size()}")
print(chad.__str__())
print(chad.calculate_gpa())
if __name__ == "__main__":
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
