代写四个小函数,所有的函数只能用recursion实现,练习基础的recursive的用法。
Purpose
The purpose of this assignment is to assess your understanding of
- Recursive functions
- Recursive thinking
Next week, we will continue with topics in Chapter 10.
Submission
- Include your full name as a comment on the first line of your Python program.
- Submit (upload) to the dropbox one file labeled as YourName_HW8.py
Problem
All of the following problems use recursion. So, you are cannot use any types
of loops, string processing functions other than slicing, global variables,
remove, replace, etc.
Problem 1
Write a recursive function stars(n)
that takes an input a nonnegative
integer and generates the pattern of stars shown below:
>>> stars(4)
****
***
**
*
*
**
***
****
>>>
Your function should produce a pattern for any nonnegative integer.
Problem 2
Write a recursive function alt(s,t)
that takes as input two strings and
mixes string s and t with alternating characters from each string.. Check that
s and t have the same length; if not, return immediately.
>>> alt(‘abcde’, ‘vwxyz’)
avbwcxdyez
>>>
>>> alt(‘hello’, ‘world’)
hweolrllod
>>>
Problem 3
Write a recursive function check(s)
that take a string representing a
password as input and returns all the characters that are digits ( 0 -9)
>>> check(‘abc’)
>>> check(‘1abc2efg’)
2 1
>>> check(‘a0b8nd79’)
9 7 8 0
Problem 4
Write a recursive function prompt()
that asks the user to enter a
password. If the user simply hits return without entering anything, the
function should prompt again, until the user enters a string. The string is
then returned (not printed). Do not use any type of loop.
>>> prompt()
Enter password:
Enter password:
Enter password:
Enter password:
Enter password:abc1ghki9$%funny
‘abc1ghki9$%funny’