logo CodeStepByStep logo

write_sequence

Language/Type: Python recursion recursive programming

Write a function write_sequence that accepts an integer n as a parameter and prints a symmetric sequence of n numbers with descending integers ending in 1 followed by ascending integers beginning with 1, as in the table below:

Call Output
write_sequence(1) 1
write_sequence(2) 1 1
write_sequence(3) 2 1 2
write_sequence(4) 2 1 1 2
write_sequence(5) 3 2 1 2 3
write_sequence(6) 3 2 1 1 2 3
write_sequence(7) 4 3 2 1 2 3 4
write_sequence(8) 4 3 2 1 1 2 3 4
write_sequence(9) 5 4 3 2 1 2 3 4 5
write_sequence(10) 5 4 3 2 1 1 2 3 4 5

Notice that for odd numbers the sequence has a single 1 in the middle while for even values it has two 1s in the middle.

Your function should raise a ValueError if passed a value less than 1.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.