Homework 5
CS1210 Computer Science 1 - Fall 2021
Due Friday, October 15, by 8:00pm
6 points

1. Write recursive function, q1(n), that takes a positive integer as input and returns a list of numbers as described below.

For 1 through 5, q1 produces:
>>> q1(1)
[1]
>>> q1(2)
[1, 4, 1]
>>> q1(3)
[1, 4, 1, 9, 1, 4, 1]
>>> q1(4)
[1, 4, 1, 9, 1, 4, 1, 16, 1, 4, 1, 9, 1, 4, 1]
>>> q1(5)
[1, 4, 1, 9, 1, 4, 1, 16, 1, 4, 1, 9, 1, 4, 1, 25, 1, 4, 1, 9, 1, 4, 1, 16, 1, 4, 1, 9, 1, 4, 1]
How do you do this? Study the above examples. Then write (on paper or in a text file - don't code right away) a recursive equation or formula that expresses how the list for q1(n) can be built by combining smaller pieces: numbers or small lists of numbers, and one or more lists produced by "smaller" uses of the q1 function. Of course you'll also need a base case that is not defined in terms of itself.

Your function should be quite short. If should contain exactly one if/else statement with the "if" part handling the base case and the else part handling the recursive case. No loops are needed nor allowed.

2. Implement recursive function q2(n, listOfStrings) that takes a positive number, n, and a list of strings as input and returns the number of strings in the list with length less than n. The function must be recursive and must contain no loops.
For example,
>>> q2(4, ['hi', 'bye', 'goodbye', 'hello'])
2
>>> q2(10, ['hi', 'bye', 'goodbye', 'hello'])
4
>>> q2(2, ['hi', 'bye', 'goodbye', 'hello'])
0
>>> q2(5, ['hello', 'bye'])
1
3. Write a recursive function q3(item1, item2) that returns True or False depending on whether or not the two given input items are "similar" according to the following definition.

Two items are similar if:
  1. they are the same type or both are numbers (i.e. int or float type), and
  2. if they are lists, they are of the same length and each pair of corresponding list items (i.e. items with the same index) is similar.
For example,
>>> q3(True, False)                           items are same type and are not lists
True
>>> q3(1, 'a')                                items are different types
False
>>> q3([],[])                      
True						
>>> q3([],[3])                                list lengths differ
False
>>> q3(['c'],[3])                             lists of same length but index 0 items are not similar
False
>>> q3([5.0],[3])                             lists of same length and corresponding lists items are similar
True
>>> q3([1,2,['a','b']],[3,4, [1,2,3]])        items at index 2 are not similar
False
>>> q3([1,2,[False, 'b']],[3, 4, [True, 'hello']])
True
>>> q3([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', [1]]]]])
True
>>> q3([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', 0]]]])
False
Note: although this function must be recursive, it may also contain a loop. One approach loops over the items of the lists, making recursive calls to test similarity of corresponding items.