In: Computer Science
Exercise 1 – Calculating factorial (n!)
Design a function calledget_factorial that takes an integer n as a parameter, and returns n!.
Note #1: 0! = 1:
Note #2: n! is calculated by the following formula: ?! = ? ∗ (? − 1)! ?? ????h?? ???? ?? ??????? ??: ?! = ? ∗ (? − 1) ∗ (? − 2) ∗ ... ∗ 3 ∗ 2 ∗ 1
Exercise 2 – Counting the number of occurrences a letter is found in a phrase
Design a function called get_occurrences that takes two strings as a parameters, and returns the number of times first string appears in the second string.
For example: get_occurrences("x","anthony") à 0 (there are no x’s in Anthony) get_occurrences("y","anthony") à 1 (there is one y in Anthony) get_occurrences("n","anthony") à 2 (there are two n’s in Anthony)
We require that you use loops in your answer. In addition to using a loop, remember the helpful string operations we learned in lecture:
name = "Anthony”
len(name) -> 7
name[3] -> "h"
Exercise 3 – Counting the number of factors of a value within a given range
Design a function called count_multiples that takes three whole positive numbers as parameters. The first two parameters return a range of values to search; the function should return how many numbers within the range are multiples of the third parameter.
For example: count_multiples(1, 9, 2) -> 4 (as 2 goes into 2, 4, 6, and 8)
count_multiples(1, 8, 2) -> 4 (as 2 goes into 2, 4, 6, and 8)
count_multiples(2, 8, 2) -> 4 (as 2 goes into 2, 4, 6, and 8)
count_multiples(20, 28, 3) -> 3 (as 3 goes into 21, 24, and 27)
count_multiples(107, 255, 7) -> 21 (7 goes into 112, 119, ..., 245, 252)
Hint: As you are solving the problem, try and print out any multiples you find as you search the range of values (given by the first and second parameters).
Exercise 4 – Printing out 10 multiples per row
Design a function called print_ten_multiples that takes an integer as a parameter. The function should print out the first 10 multiples of all numbers from 1 up to the given number.
The values printed should be formatted with "3d". Example: print(format(x, "3d"))
Examples of how the function output should look with this formatting:
print_ten_multiples(3):
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
print_ten_multiples(11):
1 2 3 4 5 6 7 8 9 10
2 4 6 8101214161820
3 6 912151821242730
4 81216202428323640
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
tests = 0
passed = 0
def main():
test_get_factorial()
#test_get_occurrences()
#test_count_multiples()
#test_print_ten_multiples()
print("Test results:", passed, "/", tests)
def test_get_factorial():
print("testing get_factorial...")
result = get_factorial(0)
#TODO: add more tests here
def test_get_occurrences():
print("testing get_occurrences...")
result = get_occurrences("x","anthony")
print_test("testing with x and anthony",
result==0)
#TODO: add more tests here
def test_count_multiples():
print("testing count_multiples...")
result = count_multiples(1, 9, 2)
print_test("testing with 1, 9, 2", result==4)
#TODO: add more tests here
def test_print_ten_multiples():
print("testing print_ten_multiples")
print("\nTesting with 3")
print_ten_multiples(3) # expects final row to be 3 -
30
#TODO: add more tests here
# TODO: Complete function design
def get_factorial(n):
...
# TODO: Complete function design
def get_occurrences(c, text):
...
# TODO: Complete function design
def count_multiples(start, end, n):
...
# TODO: Complete function design
def print_ten_multiples(max):
...
# (str, bool -> None)
# takes the name or description of a test and whether the
# test produced the expected output (True) or not (False)
# and prints out whether that test passed or failed
# NOTE: You should not have to modify this in any way.
def print_test(test_name, result_correct):
global tests
global passed
tests += 1
if(result_correct):
print(test_name + ": passed")
passed += 1
else:
print(test_name + ": failed")
# The following code will call your main function
if __name__ == '__main__':
main()
# TODO: Complete function design
def get_factorial(n):
fact=1
for i in range(1,n+1):
fact*=n
return fact
# TODO: Complete function design
def get_occurrences(c, text):
count=0
for i in text:
if i==c:
count+=1
return count
# TODO: Complete function design
def count_multiples(start, end, n):
count=0
for i in range(start,end+1):
if i%n==0:
count+=1
return count
# TODO: Complete function design
def print_ten_multiples(max):
for i in range(1,max+1):
for j in range(1,11):
print('{:3d}'.format(i*j),end=' ')
print()
