Skip to content
Advertisement

Calculating min, max without using a list

I am trying to solve a problem from a python textbook:

Write a program that asks the user to enter the number of times that they have run around a racetrack, and then uses a loop to prompt them to enter the lap time for each of their laps. When the loop finishes, the program should display the time of their fastest lap, the time of their slowest lap, and their average lap time.

So far the concept of lists haven’t been introduced but I can’t think of a way to calculate the min, max lap time without using min(),max() on a list.

Here is my code:

JavaScript

Advertisement

Answer

well i think you can actually do this fairly simply, without a list. just keep track of the largest and smallest numbers seen so far.. right? not sure if this works but a simple example is something along the lines of:

JavaScript

there’s probably a bug in there somewhere, and edge cases to handle but the idea remains and you should be able to make it work correctly using the concept.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement