Description
1 Patterns (20 points)
1) Write a function named rectangle that given two integers n and m prints out
two identical rectangles of width n and height m, separated by an empty column,
using the symbol ’∗’. You may use a loop or recursion. For instance, the call
rectangle(4,6) should exactly lead to the following result on IDLE:
**** ****
**** ****
**** ****
**** ****
**** ****
**** ****
2) Write a function named triangle that, given a strictly positive integer n
given as argument, prints out a triangle of height n, using the symbol ’∗’. You may
use a loop or recursion.
By indexing the height from 0 (top) to n − 1 (bottom), this triangle should be
such that: (1) Each row at depth i, i > 0, has two more stars than the number of
stars of the previous row at depth i−1. (2) At depth 0, the function prints one star.
For instance, the call triangle(6) should exactly lead to the following result
on IDLE:
∗
∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
2
2 Function on lists (15 points)
Write a function clean1(aList) that takes a list of integers aList as argument,
and returns a new list where multiple occurrences of values have been removed.
The function should NOT modify its argument. This means that if we print
the list (given as argument) after executing the function, it did not change. For
instance, the following statements:
al = [1,2,3,4,4,4,5,1,2,1,5]
newlist = clean1(al)
print(al)
print(newlist)
print the following result (a different order of values is acceptable):
[1,2,3,4,4,4,5,1,2,1,5]
[1,2,3,4,5]
The principle of your algorithm may be the following:
• Create a new empty list tmp.
• Use a “for loop” to fill tmp with each new value found in the argument list.
• Return tmp.
3 Function on lists (15 points)
Write a function clean2(aList) that takes a list of integers aList as argument,
and modifies this list, such as multiple occurrences of values have been removed.
The procedure does not return a list: it modifies its argument (reference
aList).
3