gateway 2 Flashcards
def foo(arr, n):
if n== 0:
return 0
else:
return foo(arr, n-1) + arr[n-1]
example = np.array([2, 6, 8, 1, 4])
print(foo(example, example.size))
21
def dunno(word: str, n: int):
if n == 1:
print(word)
else:
print(word, end = “ “)
dunno(word, n-1)
dunno(“yes”, 5)
yes yes yes yes yes
def fun(x, y) :
if (x == 0) :
return y
else :
return fun(x - 1, x + y)
print(fun(3, 10))
16
Assume that n is greater than or equal to 1
def mystery(n):
if(n == 1):
return 0
else:
return 1 + mystery(n//2)
print(mystery(1))
print(mystery(5))
print(mystery(17))
0
2
4
Assume that n is greater than or equal to 0 */
def something(n):
if(n == 0):
return
else:
something(n // 2)
print(n % 2, end=””)
something(1)
something(5)
something(9)
11011001
Write a function called is_power_of_two that takes a positive integer as input and returns a bool. Return True if the value passed is a power of 2, and False otherwise
def is_power_of_two(n):
if n == 1:
return True
elif n % 2 != 0 or n == 0:
return False
else:
return is_power_of_two(n // 2)
Write a recursive function called print_triangle that takes an integer parameter (you may assume without checking that it is an integer greater than or equal to 1). Print the following triangle pattern, where the number of lines is determined by the value of the parameter.
**
*
def print_triangle(n):
if n == 0:
return
print(“*” * n)
print_triangle(n - 1)
Finally, modify the code minimally so that it prints this pattern instead:
*
**
**
**
**
def print_triangle(n):
if n == 6:
return
print(“*” * n)
print_triangle(n + 1)
Write a recursive function called count_down() that takes an integer as a parameter. It should output the integers starting at that value, down to a final value of 1.
def count_down(n):
if n == 1:
print(n)
else:
print(n)
count_down(n - 1)
Then modify the code slightly to implement a recursive count_up() function.
def count_up(n):
if n == 1:
print(n)
else:
count_up(n - 1)
print(n)
Write a recursive function called sum_1_to_n() that takes an integer parameter and returns the sum of the integers from 1 up to the parameter passed.
def sum_1_to_n(n):
if n <= 0:
return 0
else:
return n + sum_1_to_n(n - 1)
Write a recursive function called recursive_pow() that takes two parameters. The first is the base, and the second is an integer exponent.
def recursive_pow(base, exponent):
if exponent == 0:
return 1
elif exponent > 0:
return base * recursive_pow(base, exponent - 1)
else:
return 1 / recursive_pow(base, -exponent)
import copy
class ThreeNumbers:
def \_\_init\_\_(self, n1: float, n2: float, n3: float): self.number1 = n1 self.number2 = n2 self.number3 = n3
def main():
object1 = ThreeNumbers(2, 3.14159, 2.71828)
object2 = object1
object3 = copy.copy(object1)
object1.number1 = 9
object2.number1 = 47
print(object1)
print(object2)
print(object3)
print(object1.number1, object2.number1, object3.number1)
if __name__ == “__main__”:
main()
T/F Line 12 copies a reference (memory address)
truth
import copy
class ThreeNumbers:
def \_\_init\_\_(self, n1: float, n2: float, n3: float): self.number1 = n1 self.number2 = n2 self.number3 = n3
def main():
object1 = ThreeNumbers(2, 3.14159, 2.71828)
object2 = object1
object3 = copy.copy(object1)
object1.number1 = 9
object2.number1 = 47
print(object1)
print(object2)
print(object3)
print(object1.number1, object2.number1, object3.number1)
if __name__ == “__main__”:
main()
T/F Line 13 performs a deep copy
false
import copy
class ThreeNumbers:
def \_\_init\_\_(self, n1: float, n2: float, n3: float): self.number1 = n1 self.number2 = n2 self.number3 = n3
def main():
object1 = ThreeNumbers(2, 3.14159, 2.71828)
object2 = object1
object3 = copy.copy(object1)
object1.number1 = 9
object2.number1 = 47
print(object1)
print(object2)
print(object3)
print(object1.number1, object2.number1, object3.number1)
if __name__ == “__main__”:
main()
T/F Lines 16-18 output a memory address since __str__ was not implemented in the class ThreeNumbers.
truth