FILMON_HAILE.CP-Assignment IV
FILMON_HAILE.CP-Assignment IV
DEPARTMENT OF PRE-ENGINEERING
Computer programming
Assignment IV
❖ PREPARED BY:-
FILMON HAILE UGR/8564/17
SUBMITTED TO:-
1
1.In Python, what is the difference between “False” and False?
1. "False" (String)
• Type: String
2. False (Boolean)
2. Write a Python if-else statement that checks whether a given value is an even or odd
number.
2
a. Find the sum of the digits of a number:
digit_sum = 0
digit_sum += int(digit)
3
4. Write a Python program that checks if a given number is prime.
count = 0
if x > 1:
if x % i == 0:
count += 1
x=1
x *= x + 1
Answer: x=x*(x+1) =2
temperature = 50
4
elif temperature <= 40: print("too cold")
the code shows both "too hot" and "just right" because:
1. The first if (temperature >= 50) prints "too hot" (true for 50)
The else belongs only to the second if statement, not the first one, causing both messages to
appear.
x=0
answer: 200
8. How many times will the string “Hello World” be printed by the following while
statement?
count = 0
while count < 10:
print("Hello World ")
5
The while loop will print "Hello World" infinitely because
1. count starts at 0
2. The condition count < 10 is always true (since count is never incremented)
num = 7
num -= 2
print(num)
Answer
-1
Answer
20, 17, 14
counter = 10
while True:
counter = counter – 1
print(counter)
Answer for a.
6
7
Answer for b.
The loop will iterate infinitely (forever), but it will only print the above values before getting stuck .
12. Find the sum and average of a list of numbers using function composition. (Function
Composition is a way of combining functions such that the result of one function is
Answer
def calculate_sum(numbers):
total = 0
total += num
return total
def calculate_average(numbers):
# Main program
sum_of_numbers = calculate_sum(numbers)
average_of_numbers = calculate_average(numbers)
# Displaying results
print("Numbers:", numbers)
print("Sum:", sum_of_numbers)
print("Average:", average_of_numbers)
13. If called, the display function below is called with arguments as in display(‘Tests’,4),
7
What will be the output?
while n > 0:
print("")
n -= 1
Answer
Test
Tes
Te
T
14. Write a Python code that finds the sum of the first n natural numbers using a recursive
function.
Answer
def sum_natural_numbers(n):
if n == 0:
return 0
else:
return n + sum_natural_numbers(n - 1)
if y == 0:
return 1
elif y == 1:
return x
else:
return x * power(x, y - 1)
8
16. Write a Python program that finds the GCD of two numbers.
Answer
Using Recursion
return a
else:
return GCD(b, a % b)
while b != 0:
a, b = b, a % b
return a
Answer
x = count_from
while x >= 0:
print(x)
x -= count_by
18. For the given function definition, if f=fib(3) is called, write the sequence of the lines
executed from the beginning where the fib function is call to the end of the function call.
Hint the first to be executed is line 10 and the last is also line 10, note that there is
recursive calls.
1 def fib(n):
2 sum = 0
9
3 if n == 0 or n == 1:
4 return 1
6 sum += fib(n-1)
7 sum += fib(n-2)
8 return sum
10 f = fib(3)
Answer
10 -> 1 -> 2 -> 3 -> 6 -> 1 -> 2 -> 3 -> 6 -> 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 1 -> 2 -> 3 -> 4 ->
7 -> 8 -> 6 -> 7 -> 1 -> 2 -> 3 -> 4 -> 7 -> 8 -> 10
19. The following near_and_far() function accepts three ints a, b, and c as arguments
and Return True if one of b or c is "close" (differing from a by at most 1), while the other
is "far", differing from both other values by 2 or more. Fill all of the 5 spaces with the
correct comparison or logical operators. (Hint: near_far(2,3,4) returns true while
near_far(2,3,4) returns false)
def near_and_far(a, b, c):
20. For the sequence 2, 3, 6, 18, 108, 1944..., write a recursive function
next_produc(n) that calculates the nth number of the sequence.
Answer
10
def next_product(n):
if n == 0:
return 2
elif n == 1:
return 3
else:
return next_product(n-1) * next_product(n-2)
21. Write a recursive function reverse() that returns the value of its string argument,
except reversed. For example, reverse("python") should return "nohtyp".
Answer
def reverse(s):
if len(s) <= 1:
return s
11