Project 6 of 15, for Neil
Linked List
A linked list from scratch. The kart track chalked an arrow on every crate of its old kart, each one pointing at the next: build it from two classes, add crates, take them off, and turn the round round.
Sign up free to open itLog in
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilThe kart track's crates came with chalk on them, and each one says which crate comes next.
- NeilStart at the door and follow the chalk and you get axle, brakes, chassis and drive chain, a whole kart in order.
- NeilI want to put one in, take one out and go round backwards, all without lifting a single crate.
- PaulThen each crate only needs to know which crate comes after it, and you can build the rest from that.
In chalk_round.py, two classes.
Crate(part) is one crate on the round: part is what's in it, and next is the next Crate, or None at the end of the round.
Round() is the round of crates, which starts empty. head is its first crate, or None.
append(part)puts a new crate at the end of the round.parts()returns a list of the parts, in round order.len(round)is how many crates are on it.insert_after(existing, part)puts a new crate straight after the first crate holdingexisting, and raisesValueErrorif there isn't one.remove(part)takes the first crate holdingpartoff the round, and raisesValueErrorif there isn't one.reverse()turns the round round in place, by changing where the crates point. No new crates.
The crates are linked to each other, not kept in a Python list.
Round: ['axle', 'brakes', 'chassis', 'drive-chain'] After the brakes, a fuel tank: ['axle', 'brakes', 'fuel-tank', 'chassis', 'drive-chain'] Neil took the axle: ['brakes', 'fuel-tank', 'chassis', 'drive-chain'] From the far end: ['drive-chain', 'chassis', 'fuel-tank', 'brakes'] 4 crates
What you learn
What's in it
- Each crate knows the next
- Follow the chalk
- Changing two arrows
- A fuel tank after the axle
- Turning the round round
- One turn
- How Paul would chain it
- Make the round of crates
- Ship it