首页 > 编程语言 > 详细

[2021 spring] CS61A Lab 8: Midterm Review

时间:2021-07-19 09:42:49      阅读:26      评论:0      收藏:0      [点我收藏+]

Lab08: https://inst.eecs.berkeley.edu/~cs61a/sp21/lab/lab08/#recursion-and-tree-recursion
期中复习,有些内容有点忘了,先写OOP。

Objects

Q6: Keyboard

按照doctest来写就好了,注意typing时调用press。

class Button:
    """
    Represents a single button
    """

    def __init__(self, pos, key):
        """
        Creates a button
        """
        self.pos = pos
        self.key = key
        self.times_pressed = 0

class Keyboard:
    """A Keyboard takes in an arbitrary amount of buttons, and has a
    dictionary of positions as keys, and values as Buttons.

    >>> b1 = Button(0, "H")
    >>> b2 = Button(1, "I")
    >>> k = Keyboard(b1, b2)
    >>> k.buttons[0].key
    ‘H‘
    >>> k.press(1)
    ‘I‘
    >>> k.press(2) #No button at this position
    ‘‘
    >>> k.typing([0, 1])
    ‘HI‘
    >>> k.typing([1, 0])
    ‘IH‘
    >>> b1.times_pressed
    2
    >>> b2.times_pressed
    3
    """

    def __init__(self, *args):
        self.buttons = {}
        for i in range(len(args)):
            self.buttons[i] = args[i]

    def press(self, info):
        """Takes in a position of the button pressed, and
        returns that button‘s output"""
        if info in self.buttons:
            button = self.buttons[info]
            button.times_pressed += 1
            return button.key
        return ""

    def typing(self, typing_input):
        """Takes in a list of positions of buttons pressed, and
        returns the total output"""
        output = ""
        for index in typing_input:
            output += self.press(index)
        return output

Q7: Bank Account

Account允许客户向账户中存钱,从账户中取钱,查看交易记录,取钱不能超过余额。
交易记录以元组列表的形式储存。
为了__repr__不再计算transaction中的存取款次数,直接初始化两个次数参数。

class Account:
    """A bank account that allows deposits and withdrawals.
    It tracks the current account balance and a transaction
    history of deposits and withdrawals.

    >>> eric_account = Account(‘Eric‘)
    >>> eric_account.deposit(1000000)   # depositing paycheck for the week
    1000000
    >>> eric_account.transactions
    [(‘deposit‘, 1000000)]
    >>> eric_account.withdraw(100)      # make a withdrawal to buy dinner
    999900
    >>> eric_account.transactions
    [(‘deposit‘, 1000000), (‘withdraw‘, 100)]
    >>> print(eric_account) #call to __str__
    Eric‘s Balance: $999900
    >>> eric_account.deposit(10)
    999910
    >>> eric_account #call to __repr__
    Accountholder: Eric, Deposits: 2, Withdraws: 1
    """

    interest = 0.02

    def __init__(self, account_holder):
        self.balance = 0
        self.holder = account_holder
        "*** YOUR CODE HERE ***"
        self.transactions = []
        self.deposits = 0
        self.withdraws = 0

    def deposit(self, amount):
        """Increase the account balance by amount, add the deposit
        to the transaction history, and return the new balance.
        """
        "*** YOUR CODE HERE ***"
        self.balance += amount
        self.transactions.append((‘deposit‘, amount))
        self.deposits += 1
        return self.balance

    def withdraw(self, amount):
        """Decrease the account balance by amount, add the withdraw
        to the transaction history, and return the new balance.
        """
        "*** YOUR CODE HERE ***"
        if amount <= self.balance:
            self.balance -= amount
            self.transactions.append((‘withdraw‘, amount))
            self.withdraws += 1
            return self.balance

    def __str__(self):
        "*** YOUR CODE HERE ***"
        return f"{self.holder}‘s Balance: ${self.balance}"

    def __repr__(self):
        "*** YOUR CODE HERE ***"
        return f"Accountholder: {self.holder}, Deposits: {self.deposits}, Withdraws: {self.withdraws}"

[2021 spring] CS61A Lab 8: Midterm Review

原文:https://www.cnblogs.com/ikventure/p/15028390.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!