German University in Cairo
Media Engineering and Technology Faculty
Prof. Dr. Mohammed A.Salem
Assoc. Prof. Milad Ghantous
Assoc. Prof. Mohammed Hamed
Introduction to Computer Science, Winter Semester 2024-2025
Practice Assignment 7
Discussion: 09.12.2023 - 14.12.2023
To be discussed in Tutorials:
Exercise 7-1 Shapes
a) Write a function that takes as a parameter an integer n and prints a line that consists of n stars.
Example:
n = 5
output:
*****
b) Use the function you implemented in part a to print a square that consists of n lines.
Example:
n = 4
output:
****
****
****
****
c) Use the function you implemented in part a to print a triangle that consists of n lines.
Example:
n = 5
output:
*
**
***
****
*****
Exercise 7-2 Persistent numbers
a) Write a function that takes a number and returns the multiplication of its digits.
b) Persistent numbers are numbers where the sequential product of its digits eventually produces a
single digit number. For example, take the number 764. 7 * 6 * 4 = 168; 1 * 6 * 8 = 48;
4 * 8 = 32; and finally, 3 * 2 = 6. The number of multiplication steps required to reach the
single digit number from the given number is referred to as the persistence of the starting number.
1
Thus the persistence of 764 is 4.
Write a function that takes an integer as input and returns its persistence.
Note: You have to use the function multiplyDigits described above with the most
appropriate loop.
For example:
persistence(764) –> 4
persistence(2) –> 0
persistence(23) –> 1
Exercise 7-3 Fibonacci
The Fibonacci numbers are defined as follows. The zeroth Fibonacci number is 0. The first Fibonacci
number is 1. The second Fibonacci number is 1 + 0 = 1. The third Fibonacci number is 1 + 1 = 2.
In other words, except for the first two numbers each Fibonacci number is the sum of the two previous
numbers.
Thus, Fibonacci Numbers, or “How many rabbits will you get after a year?” is given by
F ib(0) = 0
F ib(1) = 1
F ib(n) = F ib(n − 1) + F ib(n − 2)
Write a Python program Fibonacci that computes the nth Fibonacci number:
Write a function fib to calculate the nth Fibonacci number.
For more information on the use of Fibonacci Numbers please see Dr. Ron Knott’s excellent site at
http://www.ee.surrey.ac.uk/Personal/R.Knott/Fibonacci/fib.html
Exercise 7-4 Prime
Write a function isPrime that determines whether a number is a prime number. A number is prime if
it is divisible only by one and itself. For example 11 is a prime number and 14 is not a prime number.
A Sample output would be:
Enter a number: 3
3 is prime
To be solved in Labs:
Exercise 7-5 Maximum
Define a function that finds the maximum of two integers and return it.
Exercise 7-6 Sum of Digits
• Write a Python function isNumber that takes a character and returns True if the character is a
number. and False otherwise.
Examples:
2
isNumber(’c’) returns False
isNumber(’6’) returns True
• Use isNumber function you implemented in part a to write a function sumOfDigits that takes a
string consisting of text and non-negative numbers as input and returns the sum of the digits of all
the numbers in the string.
Examples:
sentence: " The year has 12 months, each month has 4 weeks and the week has 7 days."
output: 14
explanation: 1+2+4+7
Exercise 7-7 Character Count
a) Write a function named count that accepts two arguments: a String value, and a char value. Your
function is to return the total number of times the second argument (the single character value)
appears inside of the first argument.
b) Write a function named occurrence that takes two parameters, a character and a list of strings.
Your function should return a list where each index has the number of occurrence of the character
in the corresponding string in the input list.
Hint: you can use the function count that you implemented in part a.
Example:
l = ["aaa","aba","bcd","aaa"]
c = "a"
output: [3,2,0,3]
Exercise 7-8 Euler
Write a function Euler to calculate the value of the mathematical constant e which is defined as:
1 1 1 1
e= + + + ... +
1! 2! 3! n!
Implement first the function factorial (f actorial(n) = n!)
Exercise 7-9 Perfect Number
A perfect number is a positive integer that is equal to the sum of its proper positive divisors. A proper
divisor of an integer n is an integer between 1 (inclusive) and n (exclusive) that divides n with no remain-
der. For example, 6 is a perfect number because 6 = 1+2+3.
Write an algorithm that prints all perfect integers that are less than or equal to a given integer n. The
program should consist of three functions:
• A function that will calculate the sum of divisors of a given integer n
• A function that will check whether a number is a perfect number
• A function that will print all perfect numbers that are less than or equal a given integer n
Extra Exercises:
3
Exercise 7-10 Palindrome
Write a Python program that determines whether the text the user inputs is a palindrome or not. A
palindrome is a piece of text that can be read the same way in either direction (left to right and right to
left). Examples of palindromes include words such as racecar and noon.
Exercise 7-11 Power
Write a program that implements the definition of power,where m and n are integers entered by the user,
such that m is the base and n is the exponent:
power(m, n) = mn
Exercise 7-12 Letter Mapping
The international standard letter/number mapping is given below.
a) Write a function that given an uppercase letter as parameter will return a number as indicated
above.
For example, if the character c has a value ’K’, the function should return 5.
b) Write a function ranslae that uses the etNumber function. The function takes as input a phone numbe
and returns a string as output. The input string may contain letters. The method should translate
a letter (uppercase or lowercase) to a digit as described above and leaves all other characters un-
touched.
For example, if the function takes as input the string -800-Flowers , then the function should return \ve
800-3569377 .
If the function takes as input 800flowers , then the function should return \verb 8003569377