Homework 5
CS1210 Computer Science 1 - Fall 2020
Due Monday, March 15, by 10:00pm
6 points

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

For 0 through 4, q1 produces:
>>> q1(0)
[1]
>>> q1(1)
[1, 4, 1]
>>> q1(2)
[1, 4, 1, 9, 1, 4, 1]
>>> q1(3)
[1, 4, 1, 9, 1, 4, 1, 16, 1, 4, 1, 9, 1, 4, 1]
>>> q1(4)
[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(listOfStrings, n) that takes a list of strings, listOfStrings, and a number, n, as input and returns the number of strings in the list with length greater than n. The function must be recursive and must contain no loops.
For example,
>>> q2(['hi', 'bye', 'goodbye', 'hello'], 4)
2
>>> q2(['hi', 'bye', 'goodbye', 'hello'], 10)
0
>>> q2(['hi', 'bye', 'goodbye', 'hello'], 1)
4
>>> q2(['hello', 'bye'], 3)
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
HINT: 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.

4. Write recursive function q4(inString) that implements the algorithm below, and returns a list of all permutations of the input string (i.e. all the possible arrangements of the string's characters). Presume that all the characters in the input are different (so all of the permutations will be different.)
For example,
>>> q4("b")
['b']
>>> q4("ab")
['ab', 'ba']
>>> q4("zab")
['zab', 'azb', 'abz', 'zba', 'bza', 'baz']
>>> q4("dcba")
['dcba', 'cdba', 'cbda', 'cbad', 'dbca', 'bdca', 'bcda', 'bcad', 'dbac', 'bdac', 'badc', 'bacd', 'dcab', 'cdab', 'cadb', 'cabd', 'dacb', 'adcb', 'acdb', 'acbd', 'dabc', 'adbc', 'abdc', 'abcd']
ALGORITHM:
# if the string has just one character:
#    It is the only perm. Return it in a list
# else:
#    Use a recursive call to get a list of the permutations
#                                of the "tail" of the string, i.e.string[1:]
#    Then, for each item in the list above,
#      create result permutations by "inserting" string[0] at each possible
#      index position 0...len(input string), adding each of these
#      result permutations to a result list that you are building.
#    Return the list of all of the result permutations.
#
# E.g. If the input is "abcd"
#      the list the tail's permutations is ["bcd", "cbd", "cdb", "bdc", "dbc", "dcb"]
#      Then, when working with, say, "cbd" from that list, we
#      generate "acbd", "cabd", "cbad", and "cbda" by "inserting" 'a' at
#      each possible position, 0-3, in "cbd"