一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 534|回复: 5
收起左侧

[CS61A]Lab06

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-6-1 14:34:07 | 显示全部楼层 |阅读模式

[其他]CS61A #3 - 04@UCBerkely

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

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

评分

1

查看全部评分

西风吹草 发表于 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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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
回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-2-13 05:34:27 | 显示全部楼层
只做了必做题,交一下作业!!!!
lab06.png

然后上传代码!
  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
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-7 23:29

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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