In: Computer Science
The following SinglyLinkedList class is available:
class SinglyLinkedList: class _Node: """Lightweight, nonpublic class for storing a singly linked node.""" __slots__ = 'element', 'next' # streamline memory usage def __init__(self, element, next): # initialize node's fields self.element = element # reference to user's element self.next = next # reference to next node def __init__(self): # initialize list's fields self._head = None # head references to None def printList(self, label): print(label, end=' ') curr = self._head while curr != None: print(curr.element, end=" -> ") curr = curr.next print("/") ######################################################### # Only this method is required for your solution def computeStats(self): # Your code goes here ######################################################### def main(): # Create a list object for testing purpose sList = SinglyLinkedList() # Create list 2 -> 4 -> -1 -> 8 -> -5 sList._head = sList._Node(2, sList._Node(4, sList._Node(-1, sList._Node(8, sList._Node(-5, None))))) sList.printList("before:") # Call computeStats method sList.computeStats() # And see if it worked! sList.printList("after :") if __name__=="__main__": main()
Assume you have a singly-linked list of integers, some positive and some negative. Write a Python method that traverses this list to calculate both the average and the count (ie, number) of the odd values only. Once calculated, add the average to a new node at the front of the list, and the count to a new node at the end of the list. You may assume that the list will always contain at least one odd value.
Your method will be have the following method signature:
def computeStats(self):
You may use only the SinglyLinkedList class; no other methods (like size(), etc) are available.
For example, if the list initially contains:
2 → 4 → -1 → 8 → -5
the resulting list will contain:
-3.0 → 2 → 4 → -1 → 8 → -5 → 2
class SinglyLinkedList: class _Node: """Lightweight, nonpublic class for storing a singly linked node.""" __slots__ = 'element', 'next' # streamline memory usage def __init__(self, element, next): # initialize node's fields self.element = element # reference to user's element self.next = next # reference to next node def __init__(self): # initialize list's fields self._head = None # head references to None def printList(self, label): print(label, end=' ') curr = self._head while curr != None: print(curr.element, end=" -> ") curr = curr.next print("/") ######################################################### # Only this method is required for your solution def computeStats(self): # Your code goes here sum = 0 count = 0 curr = self._head pre = None while curr != None: if curr.element % 2 == 1: count += 1 sum += curr.element pre = curr curr = curr.next pre.next = self._Node(count, None) newnode = self._Node(sum/count, self._head) self._head = newnode ######################################################### def main(): # Create a list object for testing purpose sList = SinglyLinkedList() # Create list 2 -> 4 -> -1 -> 8 -> -5 sList._head = sList._Node(2, sList._Node(4, sList._Node(-1, sList._Node(8, sList._Node(-5, None))))) sList.printList("before:") # Call computeStats method sList.computeStats() # And see if it worked! sList.printList("after :") if __name__=="__main__": main() Output
before: 2 -> 4 -> -1 -> 8 -> -5 -> /
after : -3.0 -> 2 -> 4 -> -1 -> 8 -> -5 -> 2
-> /
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Thank you.