,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:
gt;gt;gt; stars(4) **** *** ** * * ** *** **** gt;gt;gt;
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.
gt;gt;gt; alt(apos;abcdeapos;, apos;vwxyzapos;) avbwcxdyez gt;gt;gt; gt;gt;gt; alt(apos;helloapos;, apos;worldapos;) hweolrllod gt;gt;gt;
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)
gt;gt;gt; check(apos;abcapos;) gt;gt;gt; check(apos;1abc2efgapos;) 2 1 gt;gt;gt; check(apos;a0b8nd79apos;) 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.
gt;gt;gt; prompt() Enter password: Enter password: Enter password: Enter password: Enter password:abc1ghki9$%funny apos;abc1ghki9$%funnyapos;