[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Assignment IV

Uploaded by

meriaddislenovo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Assignment IV

Uploaded by

meriaddislenovo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AAiT, AAU

Assignment III

Computer Programing June 2024

Assignment Instructions:
Attempt the following 21 questions and submit your solution individually.

Deadline: May 26, 2025 11:59 PM

Submission Link: Here

1. In Python, what is the difference between “False” and False?


2. Write a python if else statement that checks whether a given value is even or odd
number.
3. Write a python program to:
a. Find sum of digits of a number:
b. Reverse the given number:
c. Check the number is palindrome or not
4. Write a python program that checks if a given number is prime.
5. What is the value of x after the following statement execution?

x=1
x *= x + 1
6. Who do the following code display?
temperature = 50
if temperature >= 50:
print("too hot")
if temperature < 50:
print("too cold")
elif temperature <= 40:
print("too cold")
else:
print("just right")
7. After execution of the following code, what would be the value of y?
x=0
y = 100 if x > 0 else 200
8. How many times do the string “Hello World” will be printed by the following while
statement?
count = 0
while count < 10:
print("Hello World ")
9. What will the following python code fragment will display?
num = 7
while num > 0:
num -= 2
AAiT, AAU

print(num)
10. What sequence will the function range(20,11,-3) will return?
11. For following python code fragment:
a. What will be printed on the screen?
b. How many times will the while loop iterate?
counter = 10
while True:
if counter < 5: continue
counter = counter – 1
print(counter)
12. Find sum and average of list of numbers using function composition. (Function
composition is a way of combining functions such that the result of one function is
passed as the argument of the next function)
13. If called the display function bellow is called with arguments as in display(‘Tests’,4),
what will be the output?
def display(str, n):
while n > 0:
for i in range(n):
print(str[i],end="")
print("")
n -= 1
14. Write a python code that find sum of the first n natural numbers using recursive function.
15. Write a python program that solves x power of y recursively.
16. Write a python program that finds GCD of two numbers.
17. Using while loop, implement countdown by n.

def countdown_by_n(count_from, count_by):


"""Prints a count down from count_from by count_by. Stops printing before the result
goes negative.”””
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
3 if n == 0 or n == 1:
4 return 1
5
6 sum += fib(n-1)
7 sum += fib(n-2)
8 return sum
9
10 f=fib(3)
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):


cond1 = abs(a-b) ___ 1 and abs(b-c) ____ 2 and abs(a-c) ____ 2
cond2 = abs(a-c) ____1 and abs(a-b) ____ 2 and abs(c-b) ____ 2
return cond1 ____ cond2
AAiT, AAU

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.
21. Write a recursive function reverse() that returns the value of its string argument,
except reversed. For example, reverse("python") should return "nohtyp".

You might also like