Question: Recursive Number Patterns ***IN PYTHON PLEASE*** Complete the pattern() function, which takes a single positive (non-zero) integer as its argument. This function returns a new
Recursive Number Patterns ***IN PYTHON PLEASE***
Complete the pattern() function, which takes a single positive (non-zero) integer as its argument. This function returns a new string containing a specific pattern of 1 or more integers, each separated by a single space: -If the user enters 1, the program should return "1"
-If the user enters 2, the program should return "1 2 1"
-If the user enters 3, the program should return "1 2 1 3 1 2 1"
etc. If the pattern isnt obvious from the examples above, the base case occurs when the argument is 1, which returns "1". The recursive case (when the argument n is greater than 1) surrounds the current value of n with the results of calling the function with the value (n - 1). For example, pattern(2) ultimately returns "1 2 1" by calling pattern(1), followed by "2", followed by calling pattern(1) again.
Dont worry if your functions return value contains one or more extra leading or trailing spaces, as long as there are no double spaces between any of the integers.
| Function Call | Return Value |
| pattern(1) | 1 |
| pattern(3) | 1 2 1 3 1 2 1 |
| pattern(5) | 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
