Extra Practice for Exam 3
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Below are some additional questions that our TAs have written/collected for you to study. These are entirely optional; we do not record your scores, and these do not factor into your final grade.
These questions were chosen based on their relevance to topics we've covered so far. However, they should not be taken as representive of the types of questions on our exam, whether in format, difficulty, or coverage.
Return the fewest number of coins that you need to make up that amount.
If that amount of money cannot be made up by any combination of the coins, return -1. You may assume that you have an infinite number of each kind of coin. For example, if coins representing coins of different denominations and an integer amount representing a total amount of money.
def min_coins(coins, amount):
"""
coins (list of int): available denominations (e.g., [1, 5, 10, 25])
amount (int): the target value to reach
Return: (int) minimum number of coins required, or -1 if impossible.
"""
coins = [1, 5, 10, 25] and amount = 6, the result is 2 (one 5-cent coin and one 1-cent coin).
You are given a list of fruits, where each fruit is represented as a tuple:
Implement the function For example, for Note that we are not allowed to take 2 pears because of the limit.(name, value, cost, limit)
name: String name of the fruit.value: Integer happiness value you get from one unit.cost: Integer cost of one unit.limit: Maximum number of this specific fruit you are allowed to take.maximize_fruit_value(fruits, budget).def maximize_fruit_value(fruits, budget):
"""
fruits: list of tuples (name, value, cost, limit)
budget: total money available (int)
Return: (int) the maximum possible value you can achieve.
"""
fruits = [("Apple", 10, 2, 2), ("Pear", 15, 3, 1)], budget = 6, the optimal solution is to take 1 apple and 1 pear, for a total value of 25.
Question 3.
In this problem, you will implement three classes according to the specification below: one Our A stack is a last in, first out data structure. Think of a stack of pancakes. As you make pancakes, you create a stack of them with older pancakes going on the bottom and newer pancakes on the top. As you start eating the pancakes, you pick one off the top so you are removing the newest pancake added to the stack. When implementing your A queue is a first in, first out data structure. Think of a store checkout queue. The customer who has been in the line the longest gets the next available cashier. When implementing your Container class, one Stack class (a subclass of Container), and one Queue class (a subclass of Container).Container class will initialize an empty list. The two methods we will have are to calculate the size of the list and to add an element. The second method will be inherited by the two subclasses. We now want to create two subclasses of this generic Container class so that we can add more functionality -- the ability to remove elements from the list. A Stack and a Queue will add elements to the list in the same way, but will behave differently when removing an element.Stack class, you will have to think about which end of your list contains the element that has been in the list the shortest amount of time. This is the element you will want to remove and return.Queue class, you will have to think about which end of your list contains the element that has been in the list the longest. This is the element you will want to remove and return.class Container:
"""A container object is a list and can store elements of any type."""
def __init__(self):
"""Initializes an empty list."""
self.my_list = []
def size(self):
"""
Return the number of elements in the container
"""
# Your code here
def add(self, elem):
"""
Add elem to one end of the container list (add to same end each time).
Do not return anything.
"""
# Your code here
class Stack(Container):
"""A subclass of Container. Has an additional method to remove elements."""
def remove(self):
"""
Remove the newest element in the container.
Return the element removed or None if the stack contains no elements
"""
# Your code here
class Queue(Container):
"""A subclass of Container. Has an additional method to remove elements."""
def remove(self):
"""
Remove the oldest element in the container.
Return the element removed or None if the queue contains no elements
"""
# Your code here
Question 4.
Write the class according to the specifications below.class Circle:
def __init__(self, radius):
""" Initialize self with radius """
# your code here
def get_radius(self):
""" Return the radius of self """
# your code here
def set_radius(self, radius):
"""
radius is a number.
Change the radius of self to radius.
"""
# your code here
def get_area(self):
""" Return the area of self using pi = 3.14 """
# your code here
def equal(self, c):
"""
c is a Circle object
Return True if self and c have the same radius value
"""
# your code here
def bigger(self, c):
"""
c is a Circle object
Return self or c, the Circle object with the bigger radius.
If they are the same, return None
"""
# your code here
Question 5.
Consider the following class definitions.
Which of the following are True?
Check all that apply.class A:
def __init__(self):
self.i = 0
class B(A):
def __add__(self, n):
self.i = self.i + n.i
a1 = A()
a2 = A()
b1 = B
b2 = B()
Given an integer array For example, if money representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.def rob(money):
"""
money (list): amount of money at each house,
where money[i] = amount of money at house i
Return: maximum amount of money you can rob in one night.
"""
money = [1,2,3,1], the result is 4, because you can rob house 1 (money = 1) and then rob house 3 (money = 3).