# Programming Data Structure And Algorithms Using Python | Week 1

Session: JULY-DEC 2023

Course Name: Programming Data Structure And Algorithms Using Python

#### These are Programming Data Structure And Algorithms Assignment 1 Answers

Q1. What is the value of g(728) for the function below?
def g(y):
b = 0
while y >= 3:
(y,b) = (y/3,b+1)
return(b)

Q2. What is f(90)-f(89), given the definition of f below?
def f(n):
s = 0
for i in range(2,n):
if n%i == 0 and i%2 == 1:
s = s+1
return(s)

These are Programming Data Structure And Algorithms Assignment 1 Answers

Q3. Consider the following function h.
def h(n):
s = True
for i in range(1,n+1):
if i*i == n:
s = False
return(s)
The function h(n) given above returns False for a positive number n if and only if:

n is an odd number.
n is a prime number.
n is a perfect square.
n is a composite number.

Answer: n is a perfect square.

Q4. Consider the following function foo.
def foo(m):
if m == 0:
return(0)
else:
return(m+foo(m-1))
Which of the following is correct?

The function always terminates with foo(n) = factorial of n
The function always terminates with foo(n) = n(n+1)/2
The function terminates for non­negative n with foo(n) = factorial of n
The function terminates for non­negative n with foo(n) = n(n+1)/2

Answer: The function always terminates with foo(n) = n(n+1)/2

These are Programming Data Structure And Algorithms Assignment 1 Answers

More Weeks of Programming Data Structure And Algorithms Using Python: Click here

Session: JAN-APR 2023

Course Name: Programming Data Structure And Algorithms Using Python

These are Programming Data Structure And Algorithms Assignment 1 Answers

#### Q1. What is the value of f(2343) for the function below?

``````def f(x):
d=0
y=1
while y <= x:
d=d+1
y=y*3
return(d)``````

Q2. What is h(53)-h(52), given the definition of h below?

``````def h(n):
s = 0
for i in range(1,n+1):
if n%i > 0:
s = s+1
return(s)``````

Q3. For what integer value n would g(92,n) return 13?

``````def g(m,n):
res = 0
while m >= n:
res = res+1
m = m-n
return(res)
``````

These are Programming Data Structure And Algorithms Assignment 1 Answers

Q4. Consider the following function mys:

``````def mys(m):
if m == 1:
return(1)
else:
return(m*mys(m-1))``````

Which of the following is correct, assuming we always pass an integer argument to mys?
a. The function always terminates with mys(n) = factorial of n
b. The function always terminates with mys(n) = 1+2+…+n
c. The function terminates for non-negative n with mys(n) = factorial of n
d. The function terminates for positive n with mys(n) = factorial of n

Answer: d. The function terminates for positive n with mys(n) = factorial of n

Session: JULY-DEC 2022

These are Programming Data Structure And Algorithms Assignment 1 Answers

1. What does h(27993) return for the following function definition?

``````def h(x):
(d,n) = (1,0)
while d <= x:
(d,n) = (d*3,n+1)
return(n)``````

2. What is g(60) – g(48), given the definition of g below?

``````def g(n):
s=0
for i in range(2,n):
if n%i == 0:
s = s+1
return(s)``````

These are Programming Data Structure And Algorithms Assignment 1 Answers

3. Consider the following function f.

``````def f(n):
s=0
for i in range(1,n+1):
if n//i == i and n%i == 0:
s = 1
return(s%2 == 1)``````

The function f(n) given above returns True for a positive number n if and only if:

a. n is an odd number.
b. n is a prime number.
c. n is a perfect square.
d. n is a composite number.

4. Consider the following function foo.

``````def foo(m):
if m == 0:
return(0)
else:
return(m+foo(m-1))``````

Which of the following is correct?

a. The function always terminates with foo(n) = factorial of n
b. The function always terminates with foo(n) = n(n+1)/2
c. The function terminates for non­negative n with foo(n) = factorial of n
d. The function terminates for non­negative n with foo(n) = n(n+1)/2