Decreasing sequence

The function in Listing 1 builds a string from the values visited by a for loop.

Listing 1: A for loop with a step of -2 (Question 1).
def mysterious_loop(n: int) -> str:
    result = ""  # Text to return after the loop.

    for i in range(n, 0, -2):
        result = result + str(i) + " "

    return result

What does mysterious_loop return for n = 0, n = 1, n = 5, and n = 6? Each visited number is followed by one space.

  1. n = 0: "" / n = 1: "1 " / n = 5: "5 3 1 " / n = 6: "6 4 2 ".
  2. n = 0: "0 " / n = 1: "1 " / n = 5: "5 3 1 -1 " / n = 6: "6 4 2 0 ".
  3. n = 0: "" / n = 1: "1 " / n = 5: "5 3 1 -1 " / n = 6: "6 4 2 0 ".
  4. n = 0: "" / n = 1: "1 " / n = 5: "5 4 3 2 1" / n = 6: "6 4 2 0 ".
  • range(start, stop, step) supplies integers from start toward, but excluding, stop.
    • step defaults to 1, and range(stop) starts at 0.
    • list(range(2, 7, 2)) is [2, 4, 6]; list(range(5, 1, -2)) is [5, 3].
    • A range is empty when the step cannot reach the stop; a zero step raises ValueError.
  • list collects the supplied values in a new list.
  • str(value) returns the value as text, without changing it. str(8) is "8" and str(2.5) is "2.5"; an existing string remains the same text.