📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3556| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Lab06

全局:
公开课
学校名称: UCBerkely
Unit号: 3
开课时间: 04
课程全名: CS61A
平台: 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Lab Link: http://gaotx.com/cs61a/lab/lab06/
My solution: http://gaotx.com/blogs/2015/05/26/cs61a-lab06/

上一篇:[CS61A]Ants
下一篇:[CS61A]Quiz02
🔗
西风吹草 2015-6-2 10:55:52 | 只看该作者
全局:
Question 2:
class Person(object):
        def __init__(self, name):
                self.name = name
                self.previous = 'I squirreled it away before it could catch on fire.'
        def say(self, stuff):
                self.previous = stuff
                return stuff
        def ask(self, stuff):
                return self.say("Would you please " + stuff)
        def greet(self):
                return self.say("Hello, my name is " + self.name)
        def repeat(self):
                return self.previous

Question 3:
class Account(object):
        def __init__(self, account_holder):
                self.balance = 0
                self.holder = account_holder
                self.transactions = []
        def deposit(self, amount):
                self.transactions.append(('deposit', amount))
                self.balance += amount
                return self.balance
        def withdraw(self, amount):
                if amount > self.balance:
                        return 'Insufficient funds'
                self.transactions.append(('withdraw', amount))
                self.balance = self.balance - amount
                return self.balance

Question 4:
class BadBankAccount(Account):
        def withdraw(self, amount):
                if self.overdrawn:
                        print('You have overdrawn, please add more money!')
                        return self.balance
                self.balance -= amount
                return self.balance
        @property
        def overdrawn():
                return self.balance < 0
回复

使用道具 举报

🔗
wuxiaomin98 2015-6-6 02:31:32 | 只看该作者
全局:
# Q2
class Person(object):
    """Person class.

    >>> steven = Person("Steven")
    >>> steven.repeat()       # starts at whatever value you'd like
    'I squirreled it away before it could catch on fire.'
    >>> steven.say("Hello")
    'Hello'
    >>> steven.repeat()
    'Hello'
    >>> steven.greet()
    'Hello, my name is Steven'
    >>> steven.repeat()
    'Hello, my name is Steven'
    >>> steven.ask("preserve abstraction barriers")
    'Would you please preserve abstraction barriers'
    >>> steven.repeat()
    'Would you please preserve abstraction barriers'
    """
    def __init__(self, name):
        self.name = name
        "*** YOUR CODE HERE ***"
        self.last = 'I squirreled it away before it could catch on fire.'

    def say(self, stuff):
        "*** YOUR CODE HERE ***"
        self.last = stuff
        return stuff

    def ask(self, stuff):
        return self.say("Would you please " + stuff)

    def greet(self):
        return self.say("Hello, my name is " + self.name)

    def repeat(self):
        "*** YOUR CODE HERE ***"
        return self.last

# Q3
class Account(object):
    """A bank account that allows deposits and withdrawals.

    >>> eric_account = Account('Eric')
    >>> eric_account.deposit(1000000)   # depositing my paycheck for the week
    1000000
    >>> eric_account.transactions
    [('deposit', 1000000)]
    >>> eric_account.withdraw(100)      # buying dinner
    999900
    >>> eric_account.transactions
    [('deposit', 1000000), ('withdraw', 100)]
    """

    interest = 0.02

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

    def deposit(self, amount):
        """Increase the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        self.transactions.append(('deposit', amount))
        self.balance = self.balance + amount
        return self.balance

    def withdraw(self, amount):
        """Decrease the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        self.transactions.append(('withdraw', amount))
        if amount > self.balance:
            return 'Insufficient funds'
        self.balance = self.balance - amount
        return self.balance

# Q4
class BadBankAccount(Account):
    """ A subclass of bank account that allows an account holder to overdraw
    once, and then prevents them from withdrawing more money. You should also
    implement the property method overdrawn, which allows an account holder to
    check if they are overdrawn.

    >>> harold_account = BadBankAccount('Harold')
    >>> harold_account.deposit(100)   # depositing my paycheck for the week
    100
    >>> harold_account.withdraw(101)  # buying dinner
    -1
    >>> harold_account.overdrawn
    True
    >>> harold_account.withdraw(100000)
    You have overdrawn, please add more money!
    -1
    >>> harold_account.deposit(10)
    9
    >>> harold_account.overdrawn
    False
    """

    def withdraw(self, amount):
        """Decrease the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        if self.overdrawn:
            print('You have overdrawn, please add more money!')
            return self.balance
        self.balance = self.balance - amount
        return self.balance

    "*** YOUR CODE HERE ***"
    @property
    def overdrawn(self):
        return self.balance < 0
回复

使用道具 举报

🔗
reasonapp 2015-6-24 21:01:14 | 只看该作者
全局:
关于class的lab,so easy,上代码:
## Object-Oriented Programming ##

# Q2
class Person(object):
    """Person class.

    >>> steven = Person("Steven")
    >>> steven.repeat()       # starts at whatever value you'd like
    'I squirreled it away before it could catch on fire.'
    >>> steven.say("Hello")
    'Hello'
    >>> steven.repeat()
    'Hello'
    >>> steven.greet()
    'Hello, my name is Steven'
    >>> steven.repeat()
    'Hello, my name is Steven'
    >>> steven.ask("preserve abstraction barriers")
    'Would you please preserve abstraction barriers'
    >>> steven.repeat()
    'Would you please preserve abstraction barriers'
    """
    def __init__(self, name):
        self.name = name
        "*** YOUR CODE HERE ***"
        self.word = 'Leo is the best'

    def say(self, stuff):
        "*** YOUR CODE HERE ***"
        self.word = stuff
        return stuff

    def ask(self, stuff):
        return self.say("Would you please " + stuff)

    def greet(self):
        return self.say("Hello, my name is " + self.name)

    def repeat(self):
        "*** YOUR CODE HERE ***"
        return self.word

# Q3
class Account(object):
    """A bank account that allows deposits and withdrawals.

    >>> eric_account = Account('Eric')
    >>> eric_account.deposit(1000000)   # depositing my paycheck for the week
    1000000
    >>> eric_account.transactions
    [('deposit', 1000000)]
    >>> eric_account.withdraw(100)      # buying dinner
    999900
    >>> eric_account.transactions
    [('deposit', 1000000), ('withdraw', 100)]
    """

    interest = 0.02

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

    def deposit(self, amount):
        """Increase the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        self.transactions.append(('deposit', amount))
        self.balance = self.balance + amount
        return self.balance

    def withdraw(self, amount):
        """Decrease the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        self.transactions.append(('withdraw', amount))
        if amount > self.balance:
            return 'Insufficient funds'
        self.balance = self.balance - amount
        return self.balance

# Q4
class BadBankAccount(Account):
    """ A subclass of bank account that allows an account holder to overdraw
    once, and then prevents them from withdrawing more money. You should also
    implement the property method overdrawn, which allows an account holder to
    check if they are overdrawn.

    >>> harold_account = BadBankAccount('Harold')
    >>> harold_account.deposit(100)   # depositing my paycheck for the week
    100
    >>> harold_account.withdraw(101)  # buying dinner
    -1
    >>> harold_account.overdrawn
    True
    >>> harold_account.withdraw(100000)
    You have overdrawn, please add more money!
    -1
    >>> harold_account.deposit(10)
    9
    >>> harold_account.overdrawn
    False
    """

    def withdraw(self, amount):
        """Decrease the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        if self.balance < 0:
            print "You have overdrawn, please add more money!"
            print self.balance
        else:
            self.balance = self.balance - amount
            return self.balance

    "*** YOUR CODE HERE ***"
    def deposit(self, amount):
        self.balance = self.balance + amount
        return self.balance
   
    @property
    def overdrawn(self):
        if self.balance < 0:
            return True
        else:
            return False
回复

使用道具 举报

🔗
rockleecsu 2016-2-13 05:34:27 | 只看该作者
全局:
只做了必做题,交一下作业!!!!


然后上传代码!
  1. ## Object-Oriented Programming ##

  2. # Q2
  3. class Person(object):
  4.     """Person class.

  5.     >>> steven = Person("Steven")
  6.     >>> steven.repeat()       # starts at whatever value you'd like
  7.     'I squirreled it away before it could catch on fire.'
  8.     >>> steven.say("Hello")
  9.     'Hello'
  10.     >>> steven.repeat()
  11.     'Hello'
  12.     >>> steven.greet()
  13.     'Hello, my name is Steven'
  14.     >>> steven.repeat()
  15.     'Hello, my name is Steven'
  16.     >>> steven.ask("preserve abstraction barriers")
  17.     'Would you please preserve abstraction barriers'
  18.     >>> steven.repeat()
  19.     'Would you please preserve abstraction barriers'
  20.     """
  21.     def __init__(self, name):
  22.         self.name = name
  23.         self.prev = "I squirreled it away before it could catch on fire."

  24.     def say(self, stuff):
  25.         self.prev = stuff
  26.         return stuff

  27.     def ask(self, stuff):
  28.         return self.say("Would you please " + stuff)

  29.     def greet(self):
  30.         return self.say("Hello, my name is " + self.name)

  31.     def repeat(self):
  32.         return self.prev

  33. # Q3
  34. class Account(object):
  35.     """A bank account that allows deposits and withdrawals.

  36.     >>> eric_account = Account('Eric')
  37.     >>> eric_account.deposit(1000000)   # depositing my paycheck for the week
  38.     1000000
  39.     >>> eric_account.transactions
  40.     [('deposit', 1000000)]
  41.     >>> eric_account.withdraw(100)      # buying dinner
  42.     999900
  43.     >>> eric_account.transactions
  44.     [('deposit', 1000000), ('withdraw', 100)]
  45.     """

  46.     interest = 0.02

  47.     def __init__(self, account_holder):
  48.         self.balance = 0
  49.         self.holder = account_holder
  50.         self.transactions = []

  51.     def deposit(self, amount):
  52.         """Increase the account balance by amount and return the
  53.         new balance.
  54.         """
  55.         self.balance = self.balance + amount
  56.         self.transactions.append(("deposit", amount))
  57.         return self.balance

  58.     def withdraw(self, amount):
  59.         """Decrease the account balance by amount and return the
  60.         new balance.
  61.         """
  62.         if amount > self.balance:
  63.             return 'Insufficient funds'
  64.         self.balance = self.balance - amount
  65.         self.transactions.append(("withdraw", amount))
  66.         return self.balance

  67. # Q4
  68. class BadBankAccount(Account):
  69.     """ A subclass of bank account that allows an account holder to overdraw
  70.     once, and then prevents them from withdrawing more money. You should also
  71.     implement the property method overdrawn, which allows an account holder to
  72.     check if they are overdrawn.

  73.     >>> harold_account = BadBankAccount('Harold')
  74.     >>> harold_account.deposit(100)   # depositing my paycheck for the week
  75.     100
  76.     >>> harold_account.withdraw(101)  # buying dinner
  77.     -1
  78.     >>> harold_account.overdrawn
  79.     True
  80.     >>> harold_account.withdraw(100000)
  81.     You have overdrawn, please add more money!
  82.     -1
  83.     >>> harold_account.deposit(10)
  84.     9
  85.     >>> harold_account.overdrawn
  86.     False
  87.     """

  88.     def withdraw(self, amount):
  89.         """Decrease the account balance by amount and return the
  90.         new balance.
  91.         """
  92.         "*** YOUR CODE HERE ***"
  93.         if self.overdrawn:
  94.             print("You have overdrawn, please add more money!")
  95.         else:
  96.             self.balance = self.balance - amount
  97.         return self.balance

  98.     "*** YOUR CODE HERE ***"

  99.     @property
  100.     def overdrawn(self):
  101.         return self.balance <= 0
复制代码
回复

使用道具 举报

🔗
Liaeve 2016-5-5 22:29:44 | 只看该作者
全局:
## Object-Oriented Programming ##

# Q2
class Person(object):
    """Person class.

    >>> steven = Person("Steven")
    >>> steven.repeat()       # starts at whatever value you'd like
    'I squirreled it away before it could catch on fire.'
    >>> steven.say("Hello")
    'Hello'
    >>> steven.repeat()
    'Hello'
    >>> steven.greet()
    'Hello, my name is Steven'
    >>> steven.repeat()
    'Hello, my name is Steven'
    >>> steven.ask("preserve abstraction barriers")
    'Would you please preserve abstraction barriers'
    >>> steven.repeat()
    'Would you please preserve abstraction barriers'
    """
    def __init__(self, name):
        self.name = name
        self.saved = 'I squirreled it away before it could catch on fire.'
        "*** YOUR CODE HERE ***"

    def say(self, stuff):
        "*** YOUR CODE HERE ***"
        self.saved = stuff
        return stuff

    def ask(self, stuff):
        return self.say("Would you please " + stuff)

    def greet(self):
        return self.say("Hello, my name is " + self.name)

    def repeat(self):
        "*** YOUR CODE HERE ***"
        return self.saved

# Q3
class Account(object):
    """A bank account that allows deposits and withdrawals.

    >>> eric_account = Account('Eric')
    >>> eric_account.deposit(1000000)   # depositing my paycheck for the week
    1000000
    >>> eric_account.transactions
    [('deposit', 1000000)]
    >>> eric_account.withdraw(100)      # buying dinner
    999900
    >>> eric_account.transactions
    [('deposit', 1000000), ('withdraw', 100)]
    """

    interest = 0.02

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

    def deposit(self, amount):
        """Increase the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        self.balance = self.balance + amount
        self.transactions += [('deposit', amount)]
        return self.balance

    def withdraw(self, amount):
        """Decrease the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        if amount > self.balance:
            return 'Insufficient funds'
        self.balance = self.balance - amount
        self.transactions += [('withdraw', amount)]
        return self.balance

# Q4
class BadBankAccount(Account):
    """ A subclass of bank account that allows an account holder to overdraw
    once, and then prevents them from withdrawing more money. You should also
    implement the property method overdrawn, which allows an account holder to
    check if they are overdrawn.

    >>> harold_account = BadBankAccount('Harold')
    >>> harold_account.deposit(100)   # depositing my paycheck for the week
    100
    >>> harold_account.withdraw(101)  # buying dinner
    -1
    >>> harold_account.overdrawn
    True
    >>> harold_account.withdraw(100000)
    You have overdrawn, please add more money!
    -1
    >>> harold_account.deposit(10)
    9
    >>> harold_account.overdrawn
    False
    """

    def withdraw(self, amount):
        """Decrease the account balance by amount and return the
        new balance.
        """
        "*** YOUR CODE HERE ***"
        if self.balance < 0:
            print ('You have overdrawn, please add more money!')
            return self.balance
        self.balance = self.balance - amount
        return self.balance

    "*** YOUR CODE HERE ***"
    @property
    def overdrawn(self):
        return self.balance < 0

## Extra Object-Oriented Programming questions ##

from lab06 import *

# Q7
class CheckingAccount(Account):
    """A bank account that charges for withdrawals.

    >>> check = Check("Steven", 42)  # 42 dollars, payable to Steven
    >>> steven_account = CheckingAccount("Steven")
    >>> eric_account = CheckingAccount("Eric")
    >>> eric_account.deposit_check(check)  # trying to steal steven's money
    The police have been notified.
    >>> eric_account.balance
    0
    >>> check.deposited
    False
    >>> steven_account.balance
    0
    >>> steven_account.deposit_check(check)
    42
    >>> check.deposited
    True
    >>> steven_account.deposit_check(check)  # can't cash check twice
    The police have been notified.
    """
    withdraw_fee = 1
    interest = 0.01

    def withdraw(self, amount):
        return Account.withdraw(self, amount + self.withdraw_fee)
    def deposit_check(account_holder, Check):
        if account_holder == Check.payable_to and Check.amount > 0:
            self.deposit(Check.amount)
            Check.amount = 0
            Check.deposited = True
            return self.balance
        else:
            return 'The police have been notified.'

class Check(object):
    "*** YOUR CODE HERE ***"
    def __init__(self, payable_to, amount):
        self.payable_to = payable_to
        self.amount = amount
        self.deposited = False


# Q8
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.typing([0, 1])
    'HI'
    >>> k.typing([1, 0])
    'IH'
    >>> b1.pressed
    2
    >>> b2.pressed
    3
    """

    def __init__(self, *args):
        "*** YOUR CODE HERE ***"
        self.buttons = {buts.pos: buts for buts in args}

    def press(self, info):
        """Takes in a position of the button pressed, and
        returns that button's output"""
        "*** YOUR CODE HERE ***"
        if info in self.buttons.keys():
            self.buttons[info].pressed += 1
            return self.buttons[info].key
        return None

    def typing(self, typing_input):
        """Takes in a list of positions of buttons pressed, and
        returns the total output"""
        "*** YOUR CODE HERE ***"
        result = ''
        for pos in typing_input:
            result += self.press(pos)
        return result

class Button:
    def __init__(self, pos, key):
        self.pos = pos
        self.key = key
        self.pressed = 0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表