1. Write a recursive function printTriangle() that takes two integers as parameters and prints a triangle of asterisks based on those parameters. The first integer represents the maximum number of asterisks to be printed in the first row of the triangle pattern seen in the examples below. The second integer represents the indentation used for the first line of the pattern. If the first parameter is negative or zero, nothing will be printed. You are allowed to assume that the second paramter will be non-negative. You are allowed to use string multiplication (e.g. c * n for a character c and an integer n) and concatentiona, but no other string functions are allowed. The following shows several sample runs of the function:
2. Write a recursive function recSymPrint() that takes two characters and two integers n and indent as parameters and prints an hour glass pattern using the characters. The first character is used for the top triangle in the hour glass and the second character is used for the bottom triangle in the hour glass. The number of characters in the top line of the pattern and in the bottom line of the pattern (i.e. the biggest lines for each of the characters) is n. The indent parameter represents the indentation used in the first line of the pattern and in the last line of the pattern. The indentation increases in the top triangle (using the first character) and decreases in the bottom triangle (using the second character). If n is 0 or negative or one of the characters is the empty string, the function doesn’t print anything. You should assume that the indent parameter will be non-negative (i.e. >= 0). The following shows several examples of patterns using different characters, values of n, and amount of indentation.
3. Write a recursive function recStrCount() that takes a one-dimensional list as a parameter and returns the number of strings that are found in the list. Recall that you can determine whether an item is a string by writing type(item) == str. The only list functions you are allowed to use are len(), indexing (lst[i] for an integer i), or slicing (lst[i:j] for integers i and j). The following shows several sample runs of the function:
4. Write a recursive function recStrMerge() that takes two strings s1 and s2 as parameters and returns a string that consists of the two strings merged together. The string returned by the function should the first character of s1 followed by the first character of s2 followed by the second character of s1 followed by the second character of s2, etc. If one string runs out of characters before the other, then the remaining string should appear at the end of the merged string. The only string functions you are allowed to use are len(), concatention, indexing (s[i] for an integer i), or slicing (s[i:j] for integers i and j). The following shows several sample runs of the function:
5. Write a recursive function recListSum() that takes an arbitrarily nested list as a parameter and returns the sum of the numbers in the list. You are not allowed to use any list methods in your function other than indexing (lst[i] for some integer i), slicing (lst[i:j] for some integers i and j), or len(). You may assume that the initial value given to the function is a list, and your function does not have to behave in any reasonable way when
given something other than a list as a parameter. The list may contain values of any type
and you must only take the sum of the ones that are integers or floating point values. The
type() function is helpful in determining what values can be found in the list. Numbers
found within other collections (tuples, dictionaries, etc.) should not be included in the
sum. The following shows the behavior of the function on some sample parameters.
Please remember that the examples are just that. Your function must work correctly on all
valid parameters, and you may not make any assumptions about the number of nested
sublists or the depth of the nesting in the list provided as a parameter to the function:
Expert Answer
if n<=0:
return;
else:
print ‘ ‘*m+’*’*n
printTriangle(n-2, m+1)
def recStrCount(list):
global count
if len(list) == 0:
return count
else:
if type(list[0])==str:
count=count+1
return recStrCount(list[1:])
printTriangle(10, 0)
printTriangle(5, 5)
printTriangle(7, 0)
printTriangle(4, 3)
printTriangle(0, 0)
count=0
print recStrCount([‘hello’,’world’])
count=0
print recStrCount([1,2,3,4])
count=0
print recStrCount([1,2,3,4,’hello’,’world’])