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
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()
What does line 19 output?
47 47 2
Write a block of python code that creates a list called rectangles. The list should contain 100 Rectangle objects, whose centers are (0, 0), (0.01, 0.01), (0.02, 0.02), …(0.99, 0.99). The half-width and half-height of each rectangle is 0.01. Then write a block of code that draws all 100 rectangles in the list.
rectangles = []
for i in range(100):
cx = i * 0.01
cy = i * 0.01
rectangle = Rectangle(cx, cy, 0.01, 0.01)
rectangles.append(rectangle)
Draw all 100 rectangles in the list
for rectangle in rectangles:
rectangle.draw()
Write a block of code that creates a list that contains 366 Date objects, one for every day in 2024.
dates = []
for month in range(1, 13):
days_in_current_month = Date.days_in_month_ly if Date(month).is_leap_year() else Date.days_in_month[month - 1]
for day in range(1, days_in_current_month + 1):
date = Date(month, day, 2024)
dates.append(date)
Write a __lt__ method for the Date class that determines if one Date precedes another Date in time
def __lt__(self, other):
if self.year < other.year:
return True
elif self.year > other.year:
return False
else:
if self.month < other.month:
return True
elif self.month > other.month:
return False
else:
return self.day < other.day
For the Rectangle class defined in question 1, add code so that == returns True when the center and dimensions of two rectangles are all the same, and False otherwise.
def __eq__(self, other):
if isinstance(other, Rectangle):
return (
self.x == other.x and
self.y == other.y and
self.width == other.width and
self.height == other.height
)
return False
What is output by the following code?
class ExampleClass:
cvn = 0
cvc = 0
cvp = 1
28
20
28 2 5 8
20 2 5 8