Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

testcode.py # -*- coding: utf-8 -*- a = LinkedList(0) a.append(1) a.append(2) print(7 points if this works) for n in a: print(n) print() print(2 points

testcode.py

# -*- coding: utf-8 -*- """ a = LinkedList(0) a.append(1) a.append(2)

print("7 points if this works") for n in a: print(n)

print("")

print("2 points if this works") for n in a: print(n)

print("")

print("3 points if both of these work") for n in a: if n == 2: break else: print(n)

print("") for n in a: if n == 2: break else: print(n)

print("")

a.append(3) a.append(4) a.append(5) a.append(6) a.append(7) a.append(8)

print("")

print("1 points if this works") print(len(a)) print("")

print("1 points if this works") print(str(a)) print("")

print("1 points if this works") print(repr(a)) print("")

print("1 points each. That is, 2 points if the output of the next line is correct") a[5] = 20 print(a[5])

print("")

print("2 points for correct operation of +") a+9 # doesn't modify a print(a)

print("")

a = a+9 # appends 9 to a print(a)

print("")

print("1 points for raising correct IndexError") try: print(a[999]) except IndexError as e: print(e)

print("")

print("") print("-----") print("") print("Example output:") print(""" 7 points if this works 0 1 2

2 points if this works 0 1 2

3 points if both of these work 0 1

0 1

1 points if this works 9

1 points if this works [0->1->2->3->4->5->6->7->8->]

1 points if this works '[0->1->2->3->4->5->6->7->8->]'

1 points each. That is, 2 points if the output of the next line is correct 20

2 points for correct operation of + [0->1->2->3->4->20->6->7->8->]

[0->1->2->3->4->20->6->7->8->9->]

1 points for raising correct IndexError list index out of range

""")

image text in transcribed

image text in transcribed

Instructions: In this homework, you will create a class named Linkedlist to implement a linked list data structure that can be iterated over with a for loop. You'll need to create your own classes, raise exceptions, overload operators and built-in functions, and implement an iterator, and write a generator. Name your file hw3.py and submit on CCLE. Test code: The provided test code lists point values associated with various pieces of functionality. For credit, the output of your code should match the included "Example output" and should work equally well for similar test commands. Tasks: : Create a class Node. - The initializer should accept one argument - the data to be stored in the node. Store the data in an instance variable data and the reference to the next node in an instance variable next. What should next be initialized to, considering there are no other nodes in the list? Hint: search online for the Python equivalent to the "null" of C++ and Java. - Write magic methods such that the built-in str and repr functions can be used to return a string representation of the node, e.g. the following code: n = Node (10) print (str(n)) print (repr(n)) print(n) should print 10 three times. Hint: remember that str and repr already work on whatever data is stored in the node. Leverage this to make them work on your Node objects. Create a class Linkedlist. The constructor should accept one argument: the data to be stored in the first node. Create a new node to store that data, and set two instance variables of the LinkedList - first and last - to that node. A third instance variable n should track the number of elements. - Write a method append that accepts one argument: the data to be stored in a new node at the end of the list. Store that data in a new node, update the next field of the current last node of the Linked List, and update the last node. Remember to increment n. - Write methods _iter_ and _next, so that the data structure can be iterated over with a for loop. Do not use a generator (yet). When you are done, the code: a = LinkedList (0) a.append(1) a.append(2) for n in a: print (n) should print the numbers 0 through 2 to the console. Hint: next_ is going to need to both return the value stored in a node and keep track of which node will come next. When the end of the list is reached (how do you know when that happens?) it needs to raise a Stop Iteration exception (review 8.4). If you try to run the same for loop (for n in a: print (n)) again on the same LinkedList object, I'll bet nothing will print out (unless you have great foresight). Figure out why and fix it so that you can loop over the same LinkedList object as many times as you wish. Don't use a generator to fix the problem (yet). If you run the following code: for n in a: if n == 2: break else: print(n) once, it will print out the numbers 0 and 1. If you run it a second time (without chang- ing the object referred to by a), it probably won't work. Fix this using a generator, which can replace some of the code you've already written. Hint: this is why there needs to be an _iter, method and collections don't just define their own _next_method. By the way, writing this generator is very easy once you understand how generators work. It took me five lines, and afterward I could comment out the original _next_method. Implement magic methods such that built-in functions len, str, and repr will work. The string representations should be like [0->1->2->] (yes, the end arrow is OK). Implement magic methods such that you can set and get (single) nodes in your list via bracket notation, e.g. print(a[0]), a[1] = 10. If the index is out of bounds, raise the appropriate exception. Implement the appropriate magic method so that the + operator concatenates the second operand to a copy of your list. That is, running a + 1 should not modify a; a = a + 1 should effectively append 1 to the list (by replacing the original). Instructions: In this homework, you will create a class named Linkedlist to implement a linked list data structure that can be iterated over with a for loop. You'll need to create your own classes, raise exceptions, overload operators and built-in functions, and implement an iterator, and write a generator. Name your file hw3.py and submit on CCLE. Test code: The provided test code lists point values associated with various pieces of functionality. For credit, the output of your code should match the included "Example output" and should work equally well for similar test commands. Tasks: : Create a class Node. - The initializer should accept one argument - the data to be stored in the node. Store the data in an instance variable data and the reference to the next node in an instance variable next. What should next be initialized to, considering there are no other nodes in the list? Hint: search online for the Python equivalent to the "null" of C++ and Java. - Write magic methods such that the built-in str and repr functions can be used to return a string representation of the node, e.g. the following code: n = Node (10) print (str(n)) print (repr(n)) print(n) should print 10 three times. Hint: remember that str and repr already work on whatever data is stored in the node. Leverage this to make them work on your Node objects. Create a class Linkedlist. The constructor should accept one argument: the data to be stored in the first node. Create a new node to store that data, and set two instance variables of the LinkedList - first and last - to that node. A third instance variable n should track the number of elements. - Write a method append that accepts one argument: the data to be stored in a new node at the end of the list. Store that data in a new node, update the next field of the current last node of the Linked List, and update the last node. Remember to increment n. - Write methods _iter_ and _next, so that the data structure can be iterated over with a for loop. Do not use a generator (yet). When you are done, the code: a = LinkedList (0) a.append(1) a.append(2) for n in a: print (n) should print the numbers 0 through 2 to the console. Hint: next_ is going to need to both return the value stored in a node and keep track of which node will come next. When the end of the list is reached (how do you know when that happens?) it needs to raise a Stop Iteration exception (review 8.4). If you try to run the same for loop (for n in a: print (n)) again on the same LinkedList object, I'll bet nothing will print out (unless you have great foresight). Figure out why and fix it so that you can loop over the same LinkedList object as many times as you wish. Don't use a generator to fix the problem (yet). If you run the following code: for n in a: if n == 2: break else: print(n) once, it will print out the numbers 0 and 1. If you run it a second time (without chang- ing the object referred to by a), it probably won't work. Fix this using a generator, which can replace some of the code you've already written. Hint: this is why there needs to be an _iter, method and collections don't just define their own _next_method. By the way, writing this generator is very easy once you understand how generators work. It took me five lines, and afterward I could comment out the original _next_method. Implement magic methods such that built-in functions len, str, and repr will work. The string representations should be like [0->1->2->] (yes, the end arrow is OK). Implement magic methods such that you can set and get (single) nodes in your list via bracket notation, e.g. print(a[0]), a[1] = 10. If the index is out of bounds, raise the appropriate exception. Implement the appropriate magic method so that the + operator concatenates the second operand to a copy of your list. That is, running a + 1 should not modify a; a = a + 1 should effectively append 1 to the list (by replacing the original)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions