0

I'm timing different sorting algorithms with multiple arrays of numbers that I've generated. I have to show the time it takes to sort each array. How can I turn code like this

    print(f'bubble sort for test_array1 took {time_difference1} seconds')
    print(f'bubble sort for test_array2 took {time_difference2} seconds')
    print(f'bubble sort for test_array3 took {time_difference3} seconds')
    print(f'bubble sort for test_array4 took {time_difference4} seconds')
    print(f'bubble sort for test_array5 took {time_difference5} seconds')

into something like this?

for i in range(1,6):
    print("bubble sort for", test_array(i),"took", time_difference(i),"seconds")

where the "i" would be added to the end of the variable name to show each test.

Beta Chad
  • 5
  • 3
  • 1
    Use a list instead of a bunch of numbered variables. – Michael Butscher Oct 07 '21 at 01:48
  • Alternatively, print at the end of the loop that makes the data in the first place – Mad Physicist Oct 07 '21 at 01:49
  • use list with values `time_difference = [3, 2, 5, ...]` and later use index `time_difference[i]`. And then you can even use `enumerate()` in `for i, diff in enumerate(time_difference, 1): print(f"... test_array{i} took {diff} seconds")` – furas Oct 07 '21 at 05:57

0 Answers0