Question

In: Computer Science

Exercise 1 – Calculating factorial (n!) Design a function calledget_factorial that takes an integer n as...

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()

Solutions

Expert Solution

# 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()


Related Solutions

The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined...
The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows: n! = n.(n-1). (n-2).……...1 (for values of n greater than 1) n!=1 (for n = 0 or 1) For instance, 5! = 5.4.3.2.1 which is 120. Write a Python program that reads a nonnegative integer n and calculates and prints its factorial. Your program should display a suitable error message if n entered as a negative integer.    Figure   6.   Exercise   6  ...
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of...
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of non-negative numbers from 1 to n. Design a program that asks the user to enter a nonnegative integer and then displays the factorial of that number. Module main. Asks the user to enter a non-negative integer. A loop is used to require user input until a nonnegative number is entered. Once a nonnegative number is entered, the integer is...
a program that takes an integer and outputs the resulting factorial number.
assembly x86 language program a program that takes an integer and outputs the resulting factorial number. 
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
Write a function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Design a function called middle_value, that takes 3 integers, and returns the integer with the middle...
Design a function called middle_value, that takes 3 integers, and returns the integer with the middle value. If there is a tie, any of the possible middle values can be returned. Example: middle_value(1, 2, 8) -> 2 middle_value(9, 7, 7) -> 7 middle_value(3, 3, 3) -> 3 Design a function called combine_strings that takes two phrases and appends the longer string onto the back of the shorter one with no space between the two phrases joined. Example: If the phrases...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments....
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments. Your function will then return the n largest values from that array as an array containing n elements. It is safe to assume that arr will have at least n elements. The resulting array should have the largest number on the end and the smallest number at the beginning. For Example: max_n(np.array([1,2,3,4,5]), 3) returns np.array([3,4,5]) max_n(np.array([10,9,8,7,6,5]), 4) returns np.array([7,8,9,10]) max_n(np.array([1,1,1]), 2) returns np.array([1,1])
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT