Can you find the longest Galway–Athenry train journey without mixing up the records?
You’ve logged three train journeys from Galway to Athenry. You want to find the one that took longest and see how many passengers were on it. If you sort just the durations, can you still trust the passenger count beside the longest one?
You’ve already used lists and dates. Here, each row starts with a departure timestamp: the date and time together. Read down the table and you’ll see one departure per row. In Python, you can use an outer list to hold those rows.
Count the passengers. You might get forty-two or forty-three, never half a passenger. That makes the count discrete. Time the journey. It could take one thousand and eighty seconds, one thousand and eighty point four seconds, or a time in between. Duration is continuous, even when you round the reading.
Your timer measures one thousand two hundred and fifty-nine point six seconds. You round it and store one thousand two hundred and sixty seconds. How should you classify the journey duration itself? Pick the option with the right reason.
Read each row from left to right: the departure timestamp is a string, the passenger count is an integer, and the rounded duration is an integer in seconds. Append the third row when its readings arrive. You’re using Python lists here. In many languages, an array holds elements of one type.
If you keep the unrounded timer reading, use a real number, represented by a float in Python. Use a date for the departure day. A char can hold a single letter, such as A for a route code. A Boolean can record whether the train was on time: true or false.
Read the durations in their original order. Then use them to order the whole rows from longest to shortest. Eight thirty-five comes first with thirty-five passengers, then nine o’clock with fifty-one, then eight ten with forty-two. The first row gives the longest recorded duration: one thousand two hundred and sixty seconds.
Here are two new records. The ten o’clock train had forty-eight passengers and took one thousand one hundred and forty seconds. The ten twenty train had thirty-one and took one thousand two hundred seconds. Pick the full record that comes first when you sort longest first.
Compare the first row after each sort. If you sort the duration column alone, the eight ten train appears to have taken one thousand two hundred and sixty seconds. That reading belongs to the eight thirty-five train. Sorting whole rows keeps its departure time and passenger count with it.