## Traversing a list

This is the place for queries that don't fit in any of the other categories.

### Traversing a list

Hi everyone!

I am new here and I am in general a beginner in programming and Python. I hope my questions will not become too trivial.

So, the exercise is:

You are supposed to learn to transverse (look at all the elements) in a simple list. In the given framework the code for creating a chained list is written The important thing to remember in this exercise is how to link elements together. This is very common in object-oriented programming. The trick when making a chained list, is to connect several objects of the same class, so that every object has a reference (link) to another object (the next object'). If you so, at any place, save a referance to the first object in the chain (the next object that has not been linked to any other objects), you have a starting point to transverse the whole list of objectsr.

Input consists of whole numbers separated by a break line. Your program is supposed to put these in a chained list, transverse the chained list, and print the highest number.

Input example:

54
37
100
123
1
54

Output should be:
123

Framework:

Code: Select all
`class cube:    weight = None    next = None    def __init__(self, vekt):        self.weight = vekt         self.next = None def trace(cube):    # WRITE YOUR CODE HERE# Creating a chained listfirst = Nonelast = Nonefor line in stdin:    previous_last = last    last = cube(int(line))    if first == None:        first = last    else:        previous_last.next = last# Calling the solution function and printing the resultprint trace(first)`

We don't have to use the framework to find the solution.

Ok, first of all, I don't really understand the framework. I think I understand the the class "Cube" is creating a cube and initializing the values of weight and next. What I don't understand is how the chained list is getting created. Especially after the part previous_last = last.

I was wondering if perhaps someone could give me a few hints as to how to understand the framework? I would also appreciate, in case nobody wants to baby me through too many steps, if someone could recommend to me what to learn in order to understand this. For example, I figured that I should learn about object oriented programming in Python, so I checked out a few tutorials on YouTube, It made me understand what classes and objects are, but I still have a hard time understanding the framework even after knowing that.

I appreciate your time and help
Norasaur

Posts: 1
Joined: Thu Aug 29, 2013 3:56 pm

### Re: Transverseing a list

I'm not sure how I feel about making students implement linked lists in python...

Anyway, here are some comments for the part of the code you said you don't understand:
Code: Select all
`# Creating a chained listfirst = Nonelast = Nonefor line in stdin:    previous_last = last    # create a cube (calls the cube class' __init__() with the number for the vekt argument)    last = cube(int(line))    # if there are no cubes yet, make this the first one (as well as last)    if first == None:        first = last    # if there are already cubes, place this one after the one that was last    else:        previous_last.next = last`

So for going through all of the objects, you're obviously going to start from first.
The one after that will be first.next.
After that comes the new one's next.
When you get to an object who's next is None, you stop.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1474
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Transverseing a list

stranac wrote:I'm not sure how I feel about making students implement linked lists in python...

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

micseydel

Posts: 1916
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Transverseing a list

micseydel wrote:
stranac wrote:I'm not sure how I feel about making students implement linked lists in python...

Yeah. They love to take python because it is so "easy," and then teach about data structures and sorting algorithms that are completely irrelevant to it.

If you want to teach about ADT in a statically typed non-garbage-collected language, then use a statically typed non-garbage-collected language. I've also recently seen a thread where the student was asked to make all his class attributes private with "properly coded setters and getters"... in python. :headdesk:

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1362
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Transverseing a list

Mekire wrote:class attributes private with "properly coded setters and getters"... in python.
- I want this teacher's blood!

Also, what this post is about is called "traverse" not "transverse".
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm