一亩三分地论坛

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

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

[CS61A]HW 05

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-5-23 00:18:45 | 显示全部楼层 |阅读模式

[其他]CS61A #2 - 04@UCBerkely

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

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

x
HW05: http://gaotx.com/cs61a/hw/hw05/
My solution: http://gaotx.com/blogs/2015/05/16/cs61a-hw05/

评分

1

查看全部评分

FFFelix 发表于 2015-5-23 10:53:17 | 显示全部楼层
新鲜的帖子   把以前做的先交了

def make_withdraw(balance, password):
    """Return a password-protected withdraw function.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> w(90, 'hax0r')
    'Insufficient funds'
    >>> w(25, 'hwat')
    'Incorrect password'
    >>> w(25, 'hax0r')
    50
    >>> w(75, 'a')
    'Incorrect password'
    >>> w(10, 'hax0r')
    40
    >>> w(20, 'n00b')
    'Incorrect password'
    >>> w(10, 'hax0r')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    >>> w(10, 'l33t')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    """
    "*** YOUR CODE HERE ***"
    def withdraw(amount, pass_word):
        attempts = []
        nonlocal balance   
        if len(attempts) == 3:
            return 'Your account is locked. Attempts: ' + str(attempts)
        if password != pass_word:
            attempts.append(pass_word)
            return 'Incorrect password'
        if amount > balance:
            return 'Insufficient funds'
        balance = balance - amount
        return balance
    return withdraw

def make_joint(withdraw, old_password, new_password):
    """Return a password-protected withdraw function that has joint access to
    the balance of withdraw.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> make_joint(w, 'my', 'secret')
    'Incorrect password'
    >>> j = make_joint(w, 'hax0r', 'secret')
    >>> w(25, 'secret')
    'Incorrect password'
    >>> j(25, 'secret')
    50
    >>> j(25, 'hax0r')
    25
    >>> j(100, 'secret')
    'Insufficient funds'

    >>> j2 = make_joint(j, 'secret', 'code')
    >>> j2(5, 'code')
    20
    >>> j2(5, 'secret')
    15
    >>> j2(5, 'hax0r')
    10

    >>> j2(25, 'password')
    'Incorrect password'
    >>> j2(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> j(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> w(5, 'hax0r')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> make_joint(w, 'hax0r', 'hello')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    """
    "*** YOUR CODE HERE ***"
    result = withdraw(0, old_password)
    if type(result) == str:
        return result   
    def join(amount, pass_attempt):
        if pass_attempt == new_password:
            return withdraw(amount, old_password)
        return withdraw(amount, new_password)
    return join

class VendingMachine:
    """A vending machine that vends some product for some price.

    >>> v = VendingMachine('candy', 10)
    >>> v.vend()
    'Machine is out of stock.'
    >>> v.restock(2)
    'Current candy stock: 2'
    >>> v.vend()
    'You must deposit $10 more.'
    >>> v.deposit(7)
    'Current balance: $7'
    >>> v.vend()
    'You must deposit $3 more.'
    >>> v.deposit(5)
    'Current balance: $12'
    >>> v.vend()
    'Here is your candy and $2 change.'
    >>> v.deposit(10)
    'Current balance: $10'
    >>> v.vend()
    'Here is your candy.'
    >>> v.deposit(15)
    'Machine is out of stock. Here is your $15.'
    """
    "*** YOUR CODE HERE ***"
    def __init__(self, product, price):
        self.product = product
        self.price = price
        self.stock = 0  
        self.balance = 0
    def restock(self, n):
        self.stock += n
        return 'Current ' + self.product + 'stock: ' + str(self.stock)
    def deposit(self, n):
        if self.stock == 0:
            return 'Machine is out of stock. Here is your $ '  + str(n)
        self.balance += n
        return 'Current balance: $' + str(self.balance)
    def vend(self):
        if self.stock == 0:
            return 'Machine is out of stock.'   
        diff = self.price - self.balance
        if self.balance < self.price:
            return 'You must deposit $' + str(diff) + ' more.'
        message = 'Here is your ' + str(self.product)
        if diff != 0:
            message = message + 'and $' + str(-diff) + ' change'
        self.balance = 0
        self.stock -= 1
        return message + '.'


class MissManners:
    """A container class that only forward messages that say please.

    >>> v = VendingMachine('teaspoon', 10)
    >>> v.restock(2)
    'Current teaspoon stock: 2'
    >>> m = MissManners(v)
    >>> m.ask('vend')
    'You must learn to say please first.'
    >>> m.ask('please vend')
    'You must deposit $10 more.'
    >>> m.ask('please deposit', 20)
    'Current balance: $20'
    >>> m.ask('now will you vend?')
    'You must learn to say please first.'
    >>> m.ask('please hand over a teaspoon')
    'Thanks for asking, but I know not how to hand over a teaspoon'
    >>> m.ask('please vend')
    'Here is your teaspoon and $10 change.'
    >>> really_fussy = MissManners(m)
    >>> really_fussy.ask('deposit', 10)
    'You must learn to say please first.'
    >>> really_fussy.ask('please deposit', 10)
    'Thanks for asking, but I know not how to deposit'
    >>> really_fussy.ask('please please deposit', 10)
    'Thanks for asking, but I know not how to please deposit'
    >>> really_fussy.ask('please ask', 'please deposit', 10)
    'Current balance: $10'
    >>> fussy_three = MissManners(3)
    >>> fussy_three.ask('add', 4)
    'You must learn to say please first.'
    >>> fussy_three.ask('please add', 4)
    'Thanks for asking, but I know not how to add'
    >>> fussy_three.ask('please __add__', 4)
    7
    """
    "*** YOUR CODE HERE ***"
    def __init__(self, obj):
        self.obj = obj
    def ask(self, message, *args):
        if not message.startswith('please '):
            return 'You must learn to say please first.'
        attr = message[len('please '):]
        if not hasattr(self.obj, attr):
            return 'Thanks for asking, but I know not how to ' + attr
        return getattr(self.obj, attr)(*args)


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

karte_polo 发表于 2015-5-25 18:21:00 | 显示全部楼层
def make_withdraw(balance, password):
    """Return a password-protected withdraw function.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> w(90, 'hax0r')
    'Insufficient funds'
    >>> w(25, 'hwat')
    'Incorrect password'
    >>> w(25, 'hax0r')
    50
    >>> w(75, 'a')
    'Incorrect password'
    >>> w(10, 'hax0r')
    40
    >>> w(20, 'n00b')
    'Incorrect password'
    >>> w(10, 'hax0r')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    >>> w(10, 'l33t')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    """
    "*** YOUR CODE HERE ***"
    attempts=[]
    def withdraw(amount,p):
        nonlocal balance,attempts
        if len(attempts)<=3:
           if p==password:
              if amount > balance:
                 return 'Insufficient funds'
              balance = balance - amount
              return balance
           else:
               attempts+=[p]
               return 'Incorrect Password'
        else:
           return "Your account is locked. Attempts: "+str(attempts)
    return withdraw

def make_joint(withdraw, old_password, new_password):
    """Return a password-protected withdraw function that has joint access to
    the balance of withdraw.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> make_joint(w, 'my', 'secret')
    'Incorrect password'
    >>> j = make_joint(w, 'hax0r', 'secret')
    >>> w(25, 'secret')
    'Incorrect password'
    >>> j(25, 'secret')
    50
    >>> j(25, 'hax0r')
    25
    >>> j(100, 'secret')
    'Insufficient funds'

    >>> j2 = make_joint(j, 'secret', 'code')
    >>> j2(5, 'code')
    20
    >>> j2(5, 'secret')
    15
    >>> j2(5, 'hax0r')
    10

    >>> j2(25, 'password')
    'Incorrect password'
    >>> j2(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> j(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> w(5, 'hax0r')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> make_joint(w, 'hax0r', 'hello')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    """
    "*** YOUR CODE HERE ***"
    rt = withdraw(0,old_password)
    if type(rt)==str:
       return rt
    def joint_w(amount,p):
       if p==old_password or p == new_password:
          return withdraw(amount,old_password)
       else:
          return withdraw(amount,new_password)
    return joint_w

class VendingMachine:
    """A vending machine that vends some product for some price.

    >>> v = VendingMachine('candy', 10)
    >>> v.vend()
    'Machine is out of stock.'
    >>> v.restock(2)
    'Current candy stock: 2'
    >>> v.vend()
    'You must deposit $10 more.'
    >>> v.deposit(7)
    'Current balance: $7'
    >>> v.vend()
    'You must deposit $3 more.'
    >>> v.deposit(5)
    'Current balance: $12'
    >>> v.vend()
    'Here is your candy and $2 change.'
    >>> v.deposit(10)
    'Current balance: $10'
    >>> v.vend()
    'Here is your candy.'
    >>> v.deposit(15)
    'Machine is out of stock. Here is your $15.'
    """
    "*** YOUR CODE HERE ***"
    def __init__(self, product, price):
        self.product = product
        self.price = price
        self.stock = 0
        self.balance = 0
    stock = 0
    def restock(self,n):
        self.stock+=n
        return 'Current '+self.product+' stock: '+str(self.stock)
    def vend(self):
        if self.stock < 1:
            return 'Machine is out of stock.'
        if self.balance < self.price:
            return 'You must deposit $'+str(self.price-self.balance)+' more.'
        s = 'Here is your '+self.product+'.'
        self.stock-=1
        if self.balance>self.price:
            s = s+' and $'+str(self.balance-self.price)+' change.'
        self.balance-=self.price
        return s
    def deposit(self,n):
        if self.stock<1:
            return 'Machine is out of stock. Here is your $'+str(n)+'.'
        self.balance+=n
        return 'Current balance: $'+str(self.balance)
        
        

class MissManners:
    """A container class that only forward messages that say please.

    >>> v = VendingMachine('teaspoon', 10)
    >>> v.restock(2)
    'Current teaspoon stock: 2'
    >>> m = MissManners(v)
    >>> m.ask('vend')
    'You must learn to say please first.'
    >>> m.ask('please vend')
    'You must deposit $10 more.'
    >>> m.ask('please deposit', 20)
    'Current balance: $20'
    >>> m.ask('now will you vend?')
    'You must learn to say please first.'
    >>> m.ask('please hand over a teaspoon')
    'Thanks for asking, but I know not how to hand over a teaspoon'
    >>> m.ask('please vend')
    'Here is your teaspoon and $10 change.'
    >>> really_fussy = MissManners(m)
    >>> really_fussy.ask('deposit', 10)
    'You must learn to say please first.'
    >>> really_fussy.ask('please deposit', 10)
    'Thanks for asking, but I know not how to deposit'
    >>> really_fussy.ask('please please deposit', 10)
    'Thanks for asking, but I know not how to please deposit'
    >>> really_fussy.ask('please ask', 'please deposit', 10)
    'Current balance: $10'
    >>> fussy_three = MissManners(3)
    >>> fussy_three.ask('add', 4)
    'You must learn to say please first.'
    >>> fussy_three.ask('please add', 4)
    'Thanks for asking, but I know not how to add'
    >>> fussy_three.ask('please __add__', 4)
    7
    """
    "*** YOUR CODE HERE ***"
    def __init__(self,object):
        self.object = object
    def ask(self,message,*args):
        if not message.startswith('please'):
            return  'You must learn to say please first.'
        attrs = message[len('please '):]
        if not hasattr(self.object,attrs):
            return  'Thanks for asking, but I know not how to '+str(attrs)
        return getattr(self.object,attrs)(*args)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

西风吹草 发表于 2015-5-31 07:18:52 | 显示全部楼层
本帖最后由 西风吹草 于 2015-5-31 07:25 编辑

Question 1:
def make_withdraw(balance, password):
        attempts = []
        def withdraw(amount, word):
                nonlocal balance
                if len(attempts) == 3:
                        return 'Your account is locked. Attempts:' + str(attempts)
                elif word != password:
                        attempts.append(word)
                        return 'Incorrect password'
                elif amount > balance:
                        return 'Insufficient funds'
                balance -= amount
                return balance
        return withdraw

Question 2:
def make_joint(withdraw, old_password, new_password):
        error = withdraw(0, old_password)
        if type(error) == str:
                return error
        def joint(amount, password_attempt):
                if password_attempt == new_password:
                        return withdraw(amount, old_password)
                return withdraw(amount, password_attempt)
        return joint

Question 3:
class VendingMachine:
        def __init__(self, some_item, item_price):
                self.balance = 0
                self.stock = 0
                self.price = item_price
                self.item = some_item
                self.change = 0
        def restock(self, n):
                self.stock += n
                return 'Current candy stock: ' + str(self.stock)
        def deposit(self, amount):
                if self.stock == 0:
                        return 'Machine is out of stock. Here is your $' + str(amount)
                else:
                        self.balance += amount
                        return 'Current balance: $' + str(self.balance)
        def vend(self):
                if self.stock == 0:
                        return 'Machine is out of stock.'
                else:
                        if self.balance < self.price:
                                return 'You must deposit $' + str(self.price - self.balance) + ' more.'
                        else:
                                self.stock -= 1
                                self.change = self.balance - self.price
                                self.balance = 0
                                if self.change == 0:
                                        return 'Here is your ' + str(self.item) + '.'
                                else:
                                        return  'Here is your ' + str(self.item) + ' and $' + str(self.change) + ' change.'

Question 4:
class MissManners:
                def__init__(self, object):
                                self.object= object
                defask(self, message, *args):
                                magic_word= 'please '
                                ifnot message.startswith(magic_word):
                                                return'You must learn to say please first.'
                                attr= message[len(magic_word):]
                                ifnot hasattr(self.object, attr):
                                                return'Thanks for asking, but I know not how to ' + attr
                                return getattr(self.object, attr)(*args)


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-6-4 05:46:41 | 显示全部楼层
边看答案边做的。。。。

def make_withdraw(balance, password):
    """Return a password-protected withdraw function.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> w(90, 'hax0r')
    'Insufficient funds'
    >>> w(25, 'hwat')
    'Incorrect password'
    >>> w(25, 'hax0r')
    50
    >>> w(75, 'a')
    'Incorrect password'
    >>> w(10, 'hax0r')
    40
    >>> w(20, 'n00b')
    'Incorrect password'
    >>> w(10, 'hax0r')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    >>> w(10, 'l33t')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    """
    "*** YOUR CODE HERE ***"
    pwds = []
    def withdraw(amount, pwd):
        nonlocal balance
        if len(pwds) == 3:
            return 'Your account is locked. Attempts: ' + str(pwds)
        if pwd != password:
            pwds.append(pwd)
            return 'Incorrect password'
        if amount > balance:
            return 'Insufficient funds'
        balance = balance - amount
        return balance
    return withdraw



def make_joint(withdraw, old_password, new_password):
    """Return a password-protected withdraw function that has joint access to
    the balance of withdraw.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> make_joint(w, 'my', 'secret')
    'Incorrect password'
    >>> j = make_joint(w, 'hax0r', 'secret')
    >>> w(25, 'secret')
    'Incorrect password'
    >>> j(25, 'secret')
    50
    >>> j(25, 'hax0r')
    25
    >>> j(100, 'secret')
    'Insufficient funds'

    >>> j2 = make_joint(j, 'secret', 'code')
    >>> j2(5, 'code')
    20
    >>> j2(5, 'secret')
    15
    >>> j2(5, 'hax0r')
    10

    >>> j2(25, 'password')
    'Incorrect password'
    >>> j2(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> j(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> w(5, 'hax0r')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> make_joint(w, 'hax0r', 'hello')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    """
    "*** YOUR CODE HERE ***"
    init = withdraw(0, old_password)
    if type(init) == str:
        return init
    def joint(amount, pwd):
        if pwd == old_password or pwd == new_password:
            return withdraw(amount, old_password)
        return withdraw(amount, pwd)
    return joint


class VendingMachine:
    """A vending machine that vends some product for some price.

    >>> v = VendingMachine('candy', 10)
    >>> v.vend()
    'Machine is out of stock.'
    >>> v.restock(2)
    'Current candy stock: 2'
    >>> v.vend()
    'You must deposit $10 more.'
    >>> v.deposit(7)
    'Current balance: $7'
    >>> v.vend()
    'You must deposit $3 more.'
    >>> v.deposit(5)
    'Current balance: $12'
    >>> v.vend()
    'Here is your candy and $2 change.'
    >>> v.deposit(10)
    'Current balance: $10'
    >>> v.vend()
    'Here is your candy.'
    >>> v.deposit(15)
    'Machine is out of stock. Here is your $15.'
    """
    "*** YOUR CODE HERE ***"
    def _init_(self, product, price):
        self.product = product
        self.price = price
        self.stock = 0
        self.balance = 0

    def restock(self, n):
        self.stock += n
        return 'Current ' + self.product + 'stock ' + str(self.stock)

    def vend(self):
        if self.stock < 1:
            return 'Machine is out of stock'
        if self.balance < self.price:
            return 'You must deposit $ ' + str(self.price - self.balance) + ' more.'

        msg = 'Here is your ' + self.product + '.'
        self.stock -= 1
        if self.balance > self.price:
            msg = 'Here is your ' + self.product + 'and $ ' + str(self.balance - self.price) + 'change.'
        else:
            msg = 'Here is your ' + self.product + '.'
        self.balance = 0
        return msg

    def deposit(self, amount):
        if self.stock < 1:
            return 'Machine is out of stock. Here is your $ ' + str(amount) + '.'
        self.balance += amount
        return 'Current balance: $ ' + str(self.balance)



class MissManners:
    """A container class that only forward messages that say please.

    >>> v = VendingMachine('teaspoon', 10)
    >>> v.restock(2)
    'Current teaspoon stock: 2'
    >>> m = MissManners(v)
    >>> m.ask('vend')
    'You must learn to say please first.'
    >>> m.ask('please vend')
    'You must deposit $10 more.'
    >>> m.ask('please deposit', 20)
    'Current balance: $20'
    >>> m.ask('now will you vend?')
    'You must learn to say please first.'
    >>> m.ask('please hand over a teaspoon')
    'Thanks for asking, but I know not how to hand over a teaspoon'
    >>> m.ask('please vend')
    'Here is your teaspoon and $10 change.'
    >>> really_fussy = MissManners(m)
    >>> really_fussy.ask('deposit', 10)
    'You must learn to say please first.'
    >>> really_fussy.ask('please deposit', 10)
    'Thanks for asking, but I know not how to deposit'
    >>> really_fussy.ask('please please deposit', 10)
    'Thanks for asking, but I know not how to please deposit'
    >>> really_fussy.ask('please ask', 'please deposit', 10)
    'Current balance: $10'
    >>> fussy_three = MissManners(3)
    >>> fussy_three.ask('add', 4)
    'You must learn to say please first.'
    >>> fussy_three.ask('please add', 4)
    'Thanks for asking, but I know not how to add'
    >>> fussy_three.ask('please __add__', 4)
    7
    """
    "*** YOUR CODE HERE ***"
    def _init_(self, obj):
        self.obj = obj

    def ask(self, msg, *args):
        word = 'please'
        if not msg.startwith(word):
            return 'You mush learn to say please first'
        attr = msg[len(word):]
        if not hasattr(self.obj, attr):
            return 'Thanks for asking, but I know not how to ' + attr
        return getattr(self.obj, attr)(*args)


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-23 00:30:34 | 显示全部楼层
第四题真心不会做,startwith是什么鬼,其余的都做出来了,代码如下:
#hw05
#Q1
def make_withdraw(balance, password):
    """Return a password-protected withdraw function.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> w(90, 'hax0r')
    'Insufficient funds'
    >>> w(25, 'hwat')
    'Incorrect password'
    >>> w(25, 'hax0r')
    50
    >>> w(75, 'a')
    'Incorrect password'
    >>> w(10, 'hax0r')
    40
    >>> w(20, 'n00b')
    'Incorrect password'
    >>> w(10, 'hax0r')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    >>> w(10, 'l33t')
    "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
    """
    "*** YOUR CODE HERE ***"
    l = [password]
    password_wrong =[]

    def withdraw(amount,password_att):
        nonlocal balance
        # nonlocal password
        if len(password_wrong) < 3:        
            if password_att in l:
                if amount > balance:
                    return 'Insufficient funds'
                balance = balance - amount
                return balance
            if password_att not in l:
                # if len(password_wrong) < 3:        
                password_wrong.append(password_att)
                print ("'Incorrect password'")
        else:
            print ("Your account is locked. Attempts: " + '[' + str(password_wrong[0]) +
                    ', ' + password_wrong[1] + ', ' + password_wrong[2] + ']')
    return withdraw

#Q2
def make_joint(withdraw, old_password, new_password):
    """Return a password-protected withdraw function that has joint access to
    the balance of withdraw.

    >>> w = make_withdraw(100, 'hax0r')
    >>> w(25, 'hax0r')
    75
    >>> make_joint(w, 'my', 'secret')
    'Incorrect password'
    >>> j = make_joint(w, 'hax0r', 'secret')
    >>> w(25, 'secret')
    'Incorrect password'
    >>> j(25, 'secret')
    50
    >>> j(25, 'hax0r')
    25
    >>> j(100, 'secret')
    'Insufficient funds'

    >>> j2 = make_joint(j, 'secret', 'code')
    >>> j2(5, 'code')
    20
    >>> j2(5, 'secret')
    15
    >>> j2(5, 'hax0r')
    10

    >>> j2(25, 'password')
    'Incorrect password'
    >>> j2(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> j(5, 'secret')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> w(5, 'hax0r')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    >>> make_joint(w, 'hax0r', 'hello')
    "Your account is locked. Attempts: ['my', 'secret', 'password']"
    """
    "*** YOUR CODE HERE ***"
    if type(withdraw(0, old_password)) == str:
        return withdraw(0, old_password)
    def join_account(amount, password):
        if password == new_password:
            password = old_password
        return withdraw(amount, password)
    return join_account


#Q3
class VendingMachine:
    """A vending machine that vends some product for some price.

    >>> v = VendingMachine('candy', 10)
    >>> v.vend()
    'Machine is out of stock.'
    >>> v.restock(2)
    'Current candy stock: 2'
    >>> v.vend()
    'You must deposit $10 more.'
    >>> v.deposit(7)
    'Current balance: $7'
    >>> v.vend()
    'You must deposit $3 more.'
    >>> v.deposit(5)
    'Current balance: $12'
    >>> v.vend()
    'Here is your candy and $2 change.'
    >>> v.deposit(10)
    'Current balance: $10'
    >>> v.vend()
    'Here is your candy.'
    >>> v.deposit(15)
    'Machine is out of stock. Here is your $15.'
    """
    "*** YOUR CODE HERE ***"
    def __init__(self, product_name, price):
        self.stock = 0
        self.name = product_name
        self.price = price
        self.balance = 0

    def vend(self):
        if self.stock == 0:
            return 'Machine is out of stock.'
        if self.stock > 0:
            if self.balance < self.price:
                print("You must deposit $" + str(self.price - self.balance) + " more.")
            if self.balance > self.price:
                print("Here is your " + str(self.name) + " and $"
                    + str(self.balance - self.price) + " change.")
                self.balance = 0
                self.stock -= 1
            if self.balance == self.price:
                self.balance = 0
                print("Here is your " + str(self.name) + '.')
                self.stock -= 1
               
    def restock(self, x):
        self.stock += x
        print("Current " + str(self.name) + " stock: " + str(self.stock))
        
    def deposit(self, x):
        if self.stock == 0:
            print("Machine is out of stock. Here is your $" + str(x))
        else:
            self.balance += x
            print("Current balance: $" + str(self.balance))
#Q4
class MissManners:
    """A container class that only forward messages that say please.

    >>> v = VendingMachine('teaspoon', 10)
    >>> v.restock(2)
    'Current teaspoon stock: 2'
    >>> m = MissManners(v)
    >>> m.ask('vend')
    'You must learn to say please first.'
    >>> m.ask('please vend')
    'You must deposit $10 more.'
    >>> m.ask('please deposit', 20)
    'Current balance: $20'
    >>> m.ask('now will you vend?')
    'You must learn to say please first.'
    >>> m.ask('please hand over a teaspoon')
    'Thanks for asking, but I know not how to hand over a teaspoon'
    >>> m.ask('please vend')
    'Here is your teaspoon and $10 change.'
    >>> really_fussy = MissManners(m)
    >>> really_fussy.ask('deposit', 10)
    'You must learn to say please first.'
    >>> really_fussy.ask('please deposit', 10)
    'Thanks for asking, but I know not how to deposit'
    >>> really_fussy.ask('please please deposit', 10)
    'Thanks for asking, but I know not how to please deposit'
    >>> really_fussy.ask('please ask', 'please deposit', 10)
    'Current balance: $10'
    >>> fussy_three = MissManners(3)
    >>> fussy_three.ask('add', 4)
    'You must learn to say please first.'
    >>> fussy_three.ask('please add', 4)
    'Thanks for asking, but I know not how to add'
    >>> fussy_three.ask('please __add__', 4)
    7
    """
    "*** YOUR CODE HERE ***"
    def __init__(self, object):
        self.object = object

    def ask(self, message, *args):
        magic_word = 'please '
        if not message.startswith(magic_word):
            return 'You must learn to say please first.'
        attr = message[len(magic_word):]
        if not hasattr(self.object, attr):
            return 'Thanks for asking, but I know not how to ' + attr
        return getattr(self.object, attr)(*args)
回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-2-12 13:41:24 | 显示全部楼层
完成作业。。。感觉难度越来越高了。。。。伯克利太吊了,很难想象这是他们第一门编程课。。。。
hw05.png

代码:
  1. def make_withdraw(balance, password):
  2.     """Return a password-protected withdraw function.

  3.     >>> w = make_withdraw(100, 'hax0r')
  4.     >>> w(25, 'hax0r')
  5.     75
  6.     >>> w(90, 'hax0r')
  7.     'Insufficient funds'
  8.     >>> w(25, 'hwat')
  9.     'Incorrect password'
  10.     >>> w(25, 'hax0r')
  11.     50
  12.     >>> w(75, 'a')
  13.     'Incorrect password'
  14.     >>> w(10, 'hax0r')
  15.     40
  16.     >>> w(20, 'n00b')
  17.     'Incorrect password'
  18.     >>> w(10, 'hax0r')
  19.     "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
  20.     >>> w(10, 'l33t')
  21.     "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
  22.     """
  23.     wrong_pwd_lst = []
  24.     def withdraw(amount, pwd):
  25.         nonlocal balance
  26.         if len(wrong_pwd_lst) == 3:
  27.             return "Your account is locked. Attempts: " +  str(wrong_pwd_lst)
  28.         elif pwd != password:
  29.             wrong_pwd_lst.append(pwd)
  30.             return "Incorrect password"
  31.         elif amount > balance:
  32.             return "Insufficient funds"
  33.         balance -= amount
  34.         return balance
  35.     return withdraw


  36. def make_joint(withdraw, old_password, new_password):
  37.     """Return a password-protected withdraw function that has joint access to
  38.     the balance of withdraw.

  39.     >>> w = make_withdraw(100, 'hax0r')
  40.     >>> w(25, 'hax0r')
  41.     75
  42.     >>> make_joint(w, 'my', 'secret')
  43.     'Incorrect password'
  44.     >>> j = make_joint(w, 'hax0r', 'secret')
  45.     >>> w(25, 'secret')
  46.     'Incorrect password'
  47.     >>> j(25, 'secret')
  48.     50
  49.     >>> j(25, 'hax0r')
  50.     25
  51.     >>> j(100, 'secret')
  52.     'Insufficient funds'

  53.     >>> j2 = make_joint(j, 'secret', 'code')
  54.     >>> j2(5, 'code')
  55.     20
  56.     >>> j2(5, 'secret')
  57.     15
  58.     >>> j2(5, 'hax0r')
  59.     10

  60.     >>> j2(25, 'password')
  61.     'Incorrect password'
  62.     >>> j2(5, 'secret')
  63.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  64.     >>> j(5, 'secret')
  65.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  66.     >>> w(5, 'hax0r')
  67.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  68.     >>> make_joint(w, 'hax0r', 'hello')
  69.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  70.     """
  71.     def joint(amount, pwd):
  72.         if pwd == new_password:
  73.             return withdraw(amount, old_password)
  74.         return withdraw(amount, pwd)

  75.     operation = withdraw(0, old_password)
  76.     if type(operation) != str:
  77.         return joint
  78.     else:
  79.         return operation


  80. class VendingMachine:
  81.     """A vending machine that vends some product for some price.

  82.     >>> v = VendingMachine('candy', 10)
  83.     >>> v.vend()
  84.     'Machine is out of stock.'
  85.     >>> v.restock(2)
  86.     'Current candy stock: 2'
  87.     >>> v.vend()
  88.     'You must deposit $10 more.'
  89.     >>> v.deposit(7)
  90.     'Current balance: $7'
  91.     >>> v.vend()
  92.     'You must deposit $3 more.'
  93.     >>> v.deposit(5)
  94.     'Current balance: $12'
  95.     >>> v.vend()
  96.     'Here is your candy and $2 change.'
  97.     >>> v.deposit(10)
  98.     'Current balance: $10'
  99.     >>> v.vend()
  100.     'Here is your candy.'
  101.     >>> v.deposit(15)
  102.     'Machine is out of stock. Here is your $15.'
  103.     """
  104.     def __init__(self, kind, price):
  105.         """Constructor"""
  106.         self.kind = kind
  107.         self.price = price
  108.         self.amount = 0
  109.         self.change = 0

  110.     def deposit(self, cash):
  111.         """deposit change into the vending machine"""
  112.         self.change += cash
  113.         if self.amount == 0:
  114.             return self.vend() + " Here is your $%s." % (self.change)
  115.         return "Current balance: $%d" % (self.change)

  116.     def vend(self):
  117.         """vend one item if meet the requirement"""
  118.         if self.amount == 0:
  119.             return "Machine is out of stock."
  120.         elif self.change < self.price:
  121.             return "You must deposit $%d more." % (self.price - self.change)
  122.         cash_back = self.change - self.price
  123.         self.amount -= 1
  124.         self.change = 0
  125.         ret_str = "Here is your %s." % (self.kind)
  126.         if cash_back != 0:
  127.             ret_str = ret_str[:-1] + " and $%d change." % (cash_back)
  128.         return ret_str

  129.     def restock(self, amount):
  130.         """Refill the vending machine"""
  131.         self.amount += amount
  132.         return "Current %s stock: %d"  % (self.kind, self.amount)


  133. class MissManners:
  134.     """A container class that only forward messages that say please.

  135.     >>> v = VendingMachine('teaspoon', 10)
  136.     >>> v.restock(2)
  137.     'Current teaspoon stock: 2'
  138.     >>> m = MissManners(v)
  139.     >>> m.ask('vend')
  140.     'You must learn to say please first.'
  141.     >>> m.ask('please vend')
  142.     'You must deposit $10 more.'
  143.     >>> m.ask('please deposit', 20)
  144.     'Current balance: $20'
  145.     >>> m.ask('now will you vend?')
  146.     'You must learn to say please first.'
  147.     >>> m.ask('please hand over a teaspoon')
  148.     'Thanks for asking, but I know not how to hand over a teaspoon'
  149.     >>> m.ask('please vend')
  150.     'Here is your teaspoon and $10 change.'
  151.     >>> really_fussy = MissManners(m)
  152.     >>> really_fussy.ask('deposit', 10)
  153.     'You must learn to say please first.'
  154.     >>> really_fussy.ask('please deposit', 10)
  155.     'Thanks for asking, but I know not how to deposit'
  156.     >>> really_fussy.ask('please please deposit', 10)
  157.     'Thanks for asking, but I know not how to please deposit'
  158.     >>> really_fussy.ask('please ask', 'please deposit', 10)
  159.     'Current balance: $10'
  160.     >>> fussy_three = MissManners(3)
  161.     >>> fussy_three.ask('add', 4)
  162.     'You must learn to say please first.'
  163.     >>> fussy_three.ask('please add', 4)
  164.     'Thanks for asking, but I know not how to add'
  165.     >>> fussy_three.ask('please __add__', 4)
  166.     7
  167.     """
  168.     def __init__(self, vm):
  169.         self.vm = vm

  170.     def ask(self, message, *args):
  171.         if len(message) < 6 or message[:6] != "please":
  172.             return "You must learn to say please first."
  173.         method = message[7:]    # split
  174.         # check if the method is callable
  175.         if hasattr(self.vm, method) and callable(getattr(self.vm, method)):
  176.             return getattr(self.vm, method)(*args)
  177.         else:
  178.             return "Thanks for asking, but I know not how to %s" % (method)
复制代码
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-4-24 18:04:05 | 显示全部楼层
  1. def make_withdraw(balance, password):
  2.     """Return a password-protected withdraw function.

  3.     >>> w = make_withdraw(100, 'hax0r')
  4.     >>> w(25, 'hax0r')
  5.     75
  6.     >>> w(90, 'hax0r')
  7.     'Insufficient funds'
  8.     >>> w(25, 'hwat')
  9.     'Incorrect password'
  10.     >>> w(25, 'hax0r')
  11.     50
  12.     >>> w(75, 'a')
  13.     'Incorrect password'
  14.     >>> w(10, 'hax0r')
  15.     40
  16.     >>> w(20, 'n00b')
  17.     'Incorrect password'
  18.     >>> w(10, 'hax0r')
  19.     "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
  20.     >>> w(10, 'l33t')
  21.     "Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
  22.     """
  23.     "*** YOUR CODE HERE ***"
  24.     incor_password = []
  25.     def withdraw(amount, input_password):
  26.         nonlocal balance
  27.         if len(incor_password) == 3:
  28.             return 'Your account is locked. Attempts: '+ str(incor_password[:])
  29.         elif input_password != password:
  30.             incor_password.append(input_password)
  31.             return 'Incorrect password'
  32.         else:
  33.             if amount <= balance:
  34.                 balance = balance - amount
  35.                 return balance
  36.             else:
  37.                 return 'Insufficient funds'
  38.     return withdraw


  39. def make_joint(withdraw, old_password, new_password):
  40.     """Return a password-protected withdraw function that has joint access to
  41.     the balance of withdraw.

  42.     >>> w = make_withdraw(100, 'hax0r')
  43.     >>> w(25, 'hax0r')
  44.     75
  45.     >>> make_joint(w, 'my', 'secret')
  46.     'Incorrect password'
  47.     >>> j = make_joint(w, 'hax0r', 'secret')
  48.     >>> w(25, 'secret')
  49.     'Incorrect password'
  50.     >>> j(25, 'secret')
  51.     50
  52.     >>> j(25, 'hax0r')
  53.     25
  54.     >>> j(100, 'secret')
  55.     'Insufficient funds'

  56.     >>> j2 = make_joint(j, 'secret', 'code')
  57.     >>> j2(5, 'code')
  58.     20
  59.     >>> j2(5, 'secret')
  60.     15
  61.     >>> j2(5, 'hax0r')
  62.     10

  63.     >>> j2(25, 'password')
  64.     'Incorrect password'
  65.     >>> j2(5, 'secret')
  66.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  67.     >>> j(5, 'secret')
  68.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  69.     >>> w(5, 'hax0r')
  70.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  71.     >>> make_joint(w, 'hax0r', 'hello')
  72.     "Your account is locked. Attempts: ['my', 'secret', 'password']"
  73.     """
  74.     "*** YOUR CODE HERE ***"
  75.     initial_try = withdraw(0, old_password)
  76.     if type(initial_try) == str:
  77.         return initial_try
  78.     def joint(amount, set_new_password):
  79.         if set_new_password == new_password:
  80.             return withdraw(amount, old_password)
  81.         else:
  82.             return withdraw(amount, set_new_password)
  83.     return joint

  84. class VendingMachine:
  85.     """A vending machine that vends some product for some price.

  86.     >>> v = VendingMachine('candy', 10)
  87.     >>> v.vend()
  88.     'Machine is out of stock.'
  89.     >>> v.restock(2)
  90.     'Current candy stock: 2'
  91.     >>> v.vend()
  92.     'You must deposit $10 more.'
  93.     >>> v.deposit(7)
  94.     'Current balance: $7'
  95.     >>> v.vend()
  96.     'You must deposit $3 more.'
  97.     >>> v.deposit(5)
  98.     'Current balance: $12'
  99.     >>> v.vend()
  100.     'Here is your candy and $2 change.'
  101.     >>> v.deposit(10)
  102.     'Current balance: $10'
  103.     >>> v.vend()
  104.     'Here is your candy.'
  105.     >>> v.deposit(15)
  106.     'Machine is out of stock. Here is your $15.'
  107.     """
  108.     "*** YOUR CODE HERE ***"
  109.     def __init__(self, product, price):
  110.         self.stock = 0
  111.         self.balance = 0
  112.         self.price = price
  113.         self.product = product
  114.     def vend(self):
  115.         if self.stock == 0:
  116.             if self.balance == 0:
  117.                 return 'Machine is out of stock.'
  118.             else:
  119.                 return 'Machine is out of stock. Here is your $' + str(self.balance) + '.'
  120.         elif self.balance < self.price:
  121.             return 'You must deposit $' + str(self.price - self.balance) + ' more.'
  122.         elif self.balance > self.price:
  123.             change = self.balance - self.price
  124.             self.balance =0
  125.             self.stock -= 1
  126.             return 'Here is your ' + str(self.product) + ' and $' + str(change) + ' change.'
  127.         else:
  128.             self.balance =0
  129.             self.stock -= 1
  130.             return 'Here is your ' + str(self.product) + '.'
  131.     def restock(self, number):
  132.         self.stock += number
  133.         return 'Current ' + str(self.product) + ' stock: '+ str(self.stock)
  134.     def deposit(self, money):
  135.         self.balance += money
  136.         if self.stock == 0:
  137.             return 'Machine is out of stock. Here is your $' + str(self.balance) + '.'
  138.         return 'Current balance: $' + str(self.balance)

  139. class MissManners:
  140.     """A container class that only forward messages that say please.

  141.     >>> v = VendingMachine('teaspoon', 10)
  142.     >>> v.restock(2)
  143.     'Current teaspoon stock: 2'
  144.     >>> m = MissManners(v)
  145.     >>> m.ask('vend')
  146.     'You must learn to say please first.'
  147.     >>> m.ask('please vend')
  148.     'You must deposit $10 more.'
  149.     >>> m.ask('please deposit', 20)
  150.     'Current balance: $20'
  151.     >>> m.ask('now will you vend?')
  152.     'You must learn to say please first.'
  153.     >>> m.ask('please hand over a teaspoon')
  154.     'Thanks for asking, but I know not how to hand over a teaspoon'
  155.     >>> m.ask('please vend')
  156.     'Here is your teaspoon and $10 change.'
  157.     >>> really_fussy = MissManners(m)
  158.     >>> really_fussy.ask('deposit', 10)
  159.     'You must learn to say please first.'
  160.     >>> really_fussy.ask('please deposit', 10)
  161.     'Thanks for asking, but I know not how to deposit'
  162.     >>> really_fussy.ask('please please deposit', 10)
  163.     'Thanks for asking, but I know not how to please deposit'
  164.     >>> really_fussy.ask('please ask', 'please deposit', 10)
  165.     'Current balance: $10'
  166.     >>> fussy_three = MissManners(3)
  167.     >>> fussy_three.ask('add', 4)
  168.     'You must learn to say please first.'
  169.     >>> fussy_three.ask('please add', 4)
  170.     'Thanks for asking, but I know not how to add'
  171.     >>> fussy_three.ask('please __add__', 4)
  172.     7
  173.     """
  174.     "*** YOUR CODE HERE ***"
  175.     def __init__(self, object):
  176.         self.object = object
  177.     def ask(self, message, *args):
  178.         if not message.startswith('please'):
  179.             return 'You must learn to say please first.'
  180.         info = message[len('please '):]
  181.         if not hasattr(self.object, info):
  182.             return 'Thanks for asking, but I know not how to ' + str(info)
  183.         return getattr(self.object, info)(*args)
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2016-12-9 18:03

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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