Preliminaries

Questions 1: Factorial

The factorial of a positive integer n is simply the product of all integers from n down through 1. E.g., the factorial of 5=5×4×3×2×1=120 .

Complete the implementation of factorial below.

You will notice following your implementation that we have provided test cases for your implementation. All assignment questions will be accompanied by such test cases — some of them will allow you to view the source code for the tests themselves, while others will only provide output letting you know whether you have passed/failed them. Typically, passing all test cases will net you full credit for the assignment, though we reserve the right to manually grade your solutions if deemed necessary.

STUDENT STARTER CODE

In [ ]:
def factorial(n):
    return n

Question 2: Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Complete the following function, which finds the sum of all the multiples of 3 or 5 below the argument n.

STUDENT STARTER CODE

In [ ]:
def multiples_of_3_and_5(n):
    return n

Question 3: Integer Right Triangles

Given a perimeter of 60, we can find two right triangles with integral length sides: [(10, 24, 26), (15, 20, 25)]. Complete the following function, which takes an integer p and returns a list of tuples (a, b, c) corresponding to the integral lengths of the sides of comforming right triangles. Each tuple should have a≤b<c, and the list should contain no duplicate solutions, and be sorted in order of ascending a.

Note that your solution should take care to limit the number of triangles it tests --- your function must complete in under 3 seconds for all values of p used in the tests below to earn credit.

STUDENT STARTER CODE

In [1]:
def integer_right_triangles(p):
    return []

Question 4: SIMPLE ASCII ART

For this next exercise, you'll need to complete the function print_pattern, which, when called with a string of length ≥ 1, will print an ASCII art pattern of concentric diamonds using those characters.

Sample input 1:

X


Sample output 1:


X

Sample input 2:



XY



Sample output 2:


..Y..
Y.X.Y
..Y..


Sample input 3:



WXYZ



Sample output 3:


......Z......
....Z.Y.Z....
..Z.Y.X.Y.Z..
Z.Y.X.W.X.Y.Z
..Z.Y.X.Y.Z..
....Z.Y.Z....
......Z......


You ought to find the string join and center methods helpful in your implementation. They are demonstrated here:



> '*'.join(['one', 'two', 'three'])

'one*two*three'

> '*'.join('abcde')

'a*b*c*d*e'

> 'hello'.center(11, '*')

'***hello***'

STUDENT STARTER CODE

In [ ]:
def print_pattern(chars):
    print(chars)

# don't modify the following code!
if __name__ == '__main__':
    arg = input()
    print_pattern(arg)