PYTH-P5

Build a train

Practice 5 of 5Python for DSA7 tests

The problem

The starter code defines a Node class. Write make_train(values): make one node for each value, in the same order, and link each node to the next through its next attribute. The last node’s next stays None. Return the first node (the head), or None if values is empty.

head = make_train([3, 8, 5])
head.value             # 3
head.next.value        # 8
head.next.next.value   # 5
head.next.next.next    # None

The tests walk along your nodes from the head, following next, and compare the values they find. Keep the class name and the attribute names value and next.

Limits

  • Up to 100,000 values.
  • Do not change the list values.

Builds on PYTH-5 Classes for nodes.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

7 tests, not run yet
Test results
#TestResultReads · time
1Three coachesNot run
2Returns the first nodeNot run
3One coachNot run
4No coachesNot run
5Each node is a new objectNot run
6The input is not changedNot run
7100,000 coachesNot run