# [2021-Fall] Hw06 of CS61A of UCB

## OOP

### Q1: Vending Machine

In this question you'll create a vending machine that only outputs a single product and provides change when needed.

Create a class called VendingMachine that represents a vending machine for some product. A VendingMachineobject returns strings describing its interactions. Remember to match exactly the strings in the doctests -- including punctuation and spacing!

Fill in the VendingMachine class, adding attributes and methods as appropriate, such that its behavior matches the following doctests:

According to the wiki, a vending machine is an automated machine that provides items to consumers after cash, or other forms of payment are inserted into the machine or otherwise made

1. restock, 补充商品, 这个比较简单
2. add_funds
1. 商品有库存的时候才会记住你付了多少钱
2. 商品没有库存的时候直接退款给你
3. vend
1. 商品有库存的时候
1. 你支付的钱💰如果足够就可以买
2. 💰 不够的话会告诉你还差多少钱
3. 不要忘记购买成功后库存要 - 1
2. 商品没有库存
1. 提示要补充货物
class VendingMachine:
"""A vending machine that vends some product for some price.
"""
def __init__(self, product, price):
self.product = product
self.price = price
self.balance = 0
self.stocks = 0

def restock(self, num):
"""Restock num items to our vending machine"""
self.stocks += num
return f"Current {self.product} stock: {self.stocks}"

"""Add funds to balance, return funds if no stocks"""
if self.stocks != 0:
self.balance += fund
return f"Current balance: ${self.balance}" else: return f"Nothing left to vend. Please restock. Here is your${fund}."

def vend(self):
"""Vend a product"""
if self.stocks == 0:
return 'Nothing left to vend. Please restock.'
else:
if self.balance < self.price:
return f"You must add ${self.price - self.balance} more funds." else: change = self.balance - self.price self.balance = 0 self.stocks -= 1 if change == 0: return f"Here is your {self.product}." else: return f"Here is your {self.product} and${change} change."


### Q2: Mint

A mint is a place where coins are made. In this question, you'll implement a Mint class that can output a Coin with the correct year and worth.

• Each Mint instance has a year stamp. The update method sets the year stamp to the present_year class attribute of the Mint class.
• The create method takes a subclass of Coin and returns an instance of that class stamped with the mint's year (which may be different from Mint.present_year if it has not been updated.)
• A Coin's worth method returns the cents value of the coin plus one extra cent for each year of age beyond 50. A coin's age can be determined by subtracting the coin's year from the present_year class attribute of the Mint class.

class Mint:
"""A mint creates coins by stamping on years.
"""
present_year = 2021

def __init__(self):
self.update()

def create(self, kind):
return kind(self.year)

def update(self):
self.year = Mint.present_year

class Coin:
def __init__(self, year):
self.year = year

def worth(self):
age = Mint.present_year - self.year
if age > 50:
return self.cents + (age - 50)
else:
return self.cents


### Q3: Store Digits

Write a function store_digits that takes in an integer n and returns a linked list where each element of the list is a digit of n.

Important: Do not use any string manipulation functions like str and reversed

def store_digits(n):
"""Stores the digits of a positive number n in a linked list.
"""
while n > 0:
all_but_last, last = n // 10, n % 10
# every time we insert node in the front of the linklist
new_node = Link(n % 10, sentinel.rest)
sentinel.rest = new_node
n = all_but_last
return sentinel.rest


### Q4: Mutable Mapping

Implement deep_map_mut(fn, link), which applies a function fn onto all elements in the given linked list link. If an element is itself a linked list, apply fn to each of its elements, and so on.

Hint: The built-in isinstance function may be useful.

>>> s = Link(1, Link(2, Link(3, Link(4))))
True
>>> isinstance(s, int)
False


def deep_map_mut(fn, link):
"""Mutates a deep link by replacing each item found with the
result of calling fn on the item.  Does NOT create new Links (so
"""
# base case 1. do thing if it is empty
return
# base case 2. if it is an integer

else:


### Q5: Two List

Implement a function two_list that takes in two lists and returns a linked list. The first list contains the values that we want to put in the linked list, and the second list contains the number of each corresponding value. Assume both lists are the same size and have a length of 1 or greater. Assume all elements in the second list are greater than 0.

def two_list(vals, amounts):
"""
Returns a linked list according to the two lists that were passed in. Assume
vals and amounts are the same size. Elements in vals represent the value, and the
corresponding element in amounts represents the number of this value desired in the
final linked list. Assume all elements in amounts are greater than 0. Assume both
lists have at least one element.
"""
idx = 0
pos = sentinel
while idx < len(vals):
val, amount = vals[idx], amounts[idx]
for _ in range(amount):
pos.rest = new_node
pos = pos.rest
idx += 1
return sentinel.rest


## Extra Questions

### Q6: Next Virahanka Fibonacci Object

Implement the next method of the VirFib class. For this class, the value attribute is a Fibonacci number. The next method returns a VirFib instance whose value is the next Fibonacci number. The next method should take only constant time.

Note that in the doctests, nothing is being printed out. Rather, each call to .next() returns a VirFib instance. The way each VirFib instance is displayed is determined by the return value of its __repr__ method.

Hint: Keep track of the previous number by setting a new instance attribute inside next. You can create new instance attributes for objects at any point, even outside the __init__ method.

### Q7: Is BST

Write a function is_bst, which takes a Tree t and returns True if, and only if, t is a valid binary search tree, which means that:

• Each node has at most two children (a leaf is automatically a valid binary search tree)
• The children are valid binary search trees
• For every node, the entries in that node's left child are less than or equal to the label of the node
• For every node, the entries in that node's right child are greater than the label of the node

An example of a BST is: Note that, if a node has only one child, that child could be considered either the left or right child. You should take this into consideration.

Hint: It may be helpful to write helper functions bst_min and bst_max that return the minimum and maximum, respectively, of a Tree if it is a valid binary search tree.

1. 左子树最小值 <= 当前节点. 而不是 <
2. 当前节点如果只有一个子树, 那么在那边都可以, 这里要做两种情况的判定
def is_bst(t):
"""Returns True if the Tree t has the structure of a valid BST.
"""
def bst_min(t):
"""Return the min value of the tree t"""
if t.is_leaf():
return t.label
sub_branch_min = min([bst_min(b) for b in t.branches])
return min(t.label, sub_branch_min)

def bst_max(t):
"""Return the max value of the tree t"""
if t.is_leaf():
return t.label
sub_branch_max = max([bst_max(b) for b in t.branches])
return max(t.label, sub_branch_max)

# base case 1. a leaf node is a BST
if t.is_leaf():
return True
# base case 2. each node has at most 2 children
if len(t.branches) > 2:
return False
# base case 3. a node with a single child
# it can be considered either the left or the right
if len(t.branches) == 1:
return (bst_max(t.branches) < t.label or bst_min(t.branches) > t.label) \
and is_bst(t.branches)

left_max = bst_max(t.branches)
right_min = bst_min(t.branches)
return left_max <= t.label < right_min and is_bst(t.branches) and is_bst(t.branches)

posted @ 2022-02-27 15:54  MartinLwx  阅读(103)  评论(0编辑  收藏  举报