In: Computer Science
Python
class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self, value, next=None): """ DO NOT EDIT Initialize a node :param value: value of the node :param next: pointer to the next node in the LinkedList, default is None """ self.value = value # element at the node self.next = next # reference to next node in the LinkedList def __repr__(self): """ DO NOT EDIT String representation of a node :return: string of value """ return str(self.value) __str__ = __repr__ # IMPLEMENT THESE FUNCTIONS - DO NOT MODIFY FUNCTION SIGNATURES # def insert(value, node=None): pass def to_string(node): pass def remove(value, node): pass def remove_all(value, node): pass def search(value, node): pass def length(node): pass def sum_list(node): pass def count(value, node): pass def reverse(node): pass def remove_fake_requests(head): pass
Testcases:
import unittest from LinkedList import insert, remove, remove_all, to_string, search, sum_list, \ count, reverse, length, remove_fake_requests class TestProject2(unittest.TestCase): def test_insert(self): linked_list = insert(0) insert(1, linked_list) insert(2, linked_list) for i in range(0, 3): assert linked_list.value == i linked_list = linked_list.next def test_to_string(self): list1 = insert(0) insert(1, list1) insert(2, list1) insert(3, list1) assert to_string(list1) == "0, 1, 2, 3" def test_length(self): list1 = insert(1) insert(2, list1) insert(3, list1) assert length(list1) == 3 def test_search(self): list1 = insert(0) insert(1, list1) insert(2, list1) assert search(2, list1) assert not search(3, list1) def test_count(self): list1 = insert(0) insert(1, list1) insert(2, list1) assert count(0, list1) == 1 assert count(1, list1) == 1 assert count(2, list1) == 1 def test_sum_list(self): list1 = insert(0) insert(1, list1) insert(2, list1) insert(3, list1) assert sum_list(list1) == 6 def test_remove(self): list1 = insert(0) insert(1, list1) insert(2, list1) insert(3, list1) list1 = remove(1, list1) for i in [0, 2, 3]: assert list1.value == i list1 = list1.next assert list1 == None def test_remove_all(self): list1 = insert(0) insert(1, list1) insert(0, list1) insert(2, list1) insert(3, list1) insert(0, list1) list1 = remove_all(0, list1) test_list = list1 for i in [1, 2, 3]: assert test_list.value == i test_list = test_list.next assert test_list == None def test_reverse(self): list1 = insert(0) insert(1, list1) insert(2, list1) insert(3, list1) list1 = reverse(list1) for i in [3, 2, 1, 0]: assert list1.value == i list1 = list1.next def test_fake_requests(self): requests = insert(170144) insert(567384, requests) insert(604853, requests) insert(783456, requests) insert(783456, requests) insert(903421, requests) real_requests = remove_fake_requests(requests) for i in [170144, 567384, 604853, 903421]: assert real_requests.value == i real_requests = real_requests.next if __name__ == "__main__": unittest.main()
Thanks!
That's a lot of methods. Please note that we will be given 2 hours of time to solve a problem. In 2 hours I have done all the methods except two. reverse() fake_request(). I have taken full 2 hours.
So, please post another question asking for only these two.
class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self, value, next=None): """ DO NOT EDIT Initialize a node :param value: value of the node :param next: pointer to the next node in the LinkedList, default is None """ self.value = value # element at the node self.next = next # reference to next node in the LinkedList def __repr__(self): """ DO NOT EDIT String representation of a node :return: string of value """ return str(self.value) __str__ = __repr__ # IMPLEMENT THESE FUNCTIONS - DO NOT MODIFY FUNCTION SIGNATURES # def insert(value, node=None): # Is it an empty linked list?? if node == None: # Create a new LinkedList head = LinkedNode(value) return head else: # Append a node n = LinkedNode(value) temp = node while temp.next != None: temp = temp.next temp.next = n def to_string(node): # Take all the values in a list values = [] temp = node while temp != None: values.append(temp.value) temp = temp.next # Return the string representation of the values return ', '.join(str(i) for i in values) def remove(value, node): # Add the first node new_head = insert(node.value) temp = node while temp.next != None: # Found the value?? ignore it to add to list if temp.next.value != value: new_head.next = temp temp = temp.next else: temp.next=temp.next.next return new_head def remove_all(value, node): if node.value==value: new_head = insert(node.next.value) else: new_head = insert(node.value) temp = node while temp.next!= None: if temp.next.value != value: new_head.next = temp temp = temp.next else: temp.next = temp.next.next return new_head def search(value, node): temp = node while temp != None: if temp.value == value: # FOUND!!! return True temp = temp.next return False #NOT FOUND def length(node): count = 0 temp = node while temp != None: temp = temp.next count += 1 return count #RETURN THE NUMBER OF VALUES def sum_list(node): sum_ = 0 temp = node while temp != None: sum_ += temp.value temp = temp.next# ADD THE VALUES return sum_ def count(value, node): count = 0 temp = node while temp != None: if temp.value == value: count += 1 temp = temp.next return count def reverse(node): pass def remove_fake_requests(head): pass
======================================
SCREENSHOT:
OUTPUT:
SOURCE CODE: