gateway 2 Flashcards

1
Q

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))

A

21

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

def dunno(word: str, n: int):
if n == 1:
print(word)
else:
print(word, end = “ “)
dunno(word, n-1)

dunno(“yes”, 5)

A

yes yes yes yes yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

def fun(x, y) :
if (x == 0) :
return y
else :
return fun(x - 1, x + y)

print(fun(3, 10))

A

16

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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))

A

0
2
4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)

A

11011001

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

**
*

A

def print_triangle(n):
if n == 0:
return
print(“*” * n)
print_triangle(n - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Finally, modify the code minimally so that it prints this pattern instead:

*
**
**
**
**

A

def print_triangle(n):
if n == 6:
return
print(“*” * n)
print_triangle(n + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

def count_down(n):
if n == 1:
print(n)
else:
print(n)
count_down(n - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Then modify the code slightly to implement a recursive count_up() function.

A

def count_up(n):
if n == 1:
print(n)
else:
count_up(n - 1)
print(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

def sum_1_to_n(n):
if n <= 0:
return 0
else:
return n + sum_1_to_n(n - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a recursive function called recursive_pow() that takes two parameters. The first is the base, and the second is an integer exponent.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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)

A

truth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

truth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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?

A

47 47 2

17
Q

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.

A

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()

18
Q

Write a block of code that creates a list that contains 366 Date objects, one for every day in 2024.

A

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)

19
Q

Write a __lt__ method for the Date class that determines if one Date precedes another Date in time

A

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

20
Q

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.

A

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

21
Q

What is output by the following code?

class ExampleClass:
cvn = 0
cvc = 0
cvp = 1

A

28
20
28 2 5 8
20 2 5 8