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

[CS61A]Quiz02

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

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

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

x
Quiz02:http://gaotx.com/cs61a/hw/quiz02/
My solution: http://gaotx.com/blogs/2015/05/25/cs61a-quiz02/

上一篇:[CS61A]Lab06
下一篇:[CS61A]Disc06
推荐
reasonapp 2015-6-26 01:26:06 | 只看该作者
全局:
Q2好难理解。。。每次都觉得是英语太差。。代码如下:
#Quiz 2
#Q1
def make_change(amount, coins):
    """Return a list of coins that sum to amount, preferring the smallest coins
    available and placing the smallest coins first in the returned list.

    The coins argument is a dictionary with keys that are positive integer
    denominations and values that are positive integer coin counts.

    >>> make_change(2, {2: 1})
    [2]
    >>> make_change(2, {1: 2, 2: 1})
    [1, 1]
    >>> make_change(4, {1: 2, 2: 1})
    [1, 1, 2]
    >>> make_change(4, {2: 1}) == None
    True

    >>> coins = {2: 2, 3: 2, 4: 3, 5: 1}
    >>> make_change(4, coins)
    [2, 2]
    >>> make_change(8, coins)
    [2, 2, 4]
    >>> make_change(25, coins)
    [2, 3, 3, 4, 4, 4, 5]
    >>> coins[8] = 1
    >>> make_change(25, coins)
    [2, 2, 4, 4, 5, 8]
    """
    if not coins:
        return None
    smallest = min(coins)
    rest = remove_one(coins, smallest)
    "*** YOUR CODE HERE ***"
   
    if amount == smallest:
        return [smallest]
    result = make_change(amount - smallest, rest)
    if result:
        return [smallest] + result
    else:
        return make_change(amount, rest)
        
def remove_one(coins, coin):
    """Remove one coin from a dictionary of coins. Return a new dictionary,
    leaving the original dictionary coins unchanged.

    >>> coins = {2: 5, 3: 2, 6: 1}
    >>> remove_one(coins, 2) == {2: 4, 3: 2, 6: 1}
    True
    >>> remove_one(coins, 6) == {2: 5, 3: 2}
    True
    >>> coins == {2: 5, 3: 2, 6: 1} # Unchanged
    True
    """
    copy = dict(coins)
    count = copy.pop(coin) - 1
    if count:
        copy[coin] = count
    return copy

class ChangeMachine:
    """A change machine holds a certain number of coins, initially all pennies.
    The change method adds a single coin of some denomination X and returns a
    list of coins that sums to X. The machine prefers to return the smallest
    coins available. The total value in the machine never changes, and it can
    always make change for any coin (perhaps by returning the coin passed in).

    The coins attribute is a dictionary with keys that are positive integer
    denominations and values that are positive integer coin counts.

    >>> m = ChangeMachine(2)
    >>> m.coins == {1: 2}
    True
    >>> m.change(2)
    [1, 1]
    >>> m.coins == {2: 1}
    True
    >>> m.change(2)
    [2]
    >>> m.coins == {2: 1}
    True
    >>> m.change(3)
    [3]
    >>> m.coins == {2: 1}
    True

    >>> m = ChangeMachine(10) # 10 pennies
    >>> m.coins == {1: 10}
    True
    >>> m.change(5) # takes a nickel & returns 5 pennies
    [1, 1, 1, 1, 1]
    >>> m.coins == {1: 5, 5: 1} # 5 pennies & a nickel remain
    True
    >>> m.change(3)
    [1, 1, 1]
    >>> m.coins == {1: 2, 3: 1, 5: 1}
    True
    >>> m.change(2)
    [1, 1]
    >>> m.change(2) # not enough 1's remaining; return a 2
    [2]
    >>> m.coins == {2: 1, 3: 1, 5: 1}
    True
    >>> m.change(8) # cannot use the 2 to make 8, so use 3 & 5
    [3, 5]
    >>> m.coins == {2: 1, 8: 1}
    True
    >>> m.change(1) # return the penny passed in (it's the smallest)
    [1]
    >>> m.change(9) # return the 9 passed in (no change possible)
    [9]
    >>> m.coins == {2: 1, 8: 1}
    True
    >>> m.change(10)
    [2, 8]
    >>> m.coins == {10: 1}
    True

    >>> m = ChangeMachine(9)
    >>> [m.change(k) for k in [2, 2, 3]]
    [[1, 1], [1, 1], [1, 1, 1]]
    >>> m.coins == {1: 2, 2: 2, 3: 1}
    True
    >>> m.change(5) # Prefers [1, 1, 3] to [1, 2, 2] (more pennies)
    [1, 1, 3]
    >>> m.change(7)
    [2, 5]
    >>> m.coins == {2: 1, 7: 1}
    True
    """
    def __init__(self, pennies):
        self.coins = {1: pennies}

    def change(self, coin):
        """Return change for coin, removing the result from self.coins."""
        "*** YOUR CODE HERE ***"
        self.coins[coin] = 1 + self.coins.get(coin, 0)
        result = make_change(coin, self.coins)
        for coin in result:
            count = self.coins.pop(coin) - 1
            if count:
                self.coins[coin] = count
        return result
回复

使用道具 举报

🔗
西风吹草 2015-6-2 10:56:27 | 只看该作者
全局:
Question 1:
def remove_one(coins, coin):
        copy = dict(coins)
        count = copy.pop(coin) - 1
        if count:
                copy[coin] = count
        return copy
def make_change(amount, coins):
        if not coins:
                return None
        smallest = min(coins)
        rest = remove_one(coins, smallest)
        if amount == smallest:
                return [smallest]
        result = make_change(amount - smallest, rest)
        if result:
                return [smallest] + result
        return make_change(amount, rest)

Question 2:
class ChangeMachine:
        def __init__(self, pennies):
                self.coins = {1: pennies}
        def change(self, coin):
                result = make_change(coin, self.coins)
                if result == None:
                        return [coin]
                else:
                        for x in result:
                                self.coins = remove_one(self.coins, x)
                        self.coins[coin] = self.coins.get(coin, 0) + 1
                        return result
回复

使用道具 举报

🔗
wuxiaomin98 2015-6-6 02:43:10 | 只看该作者
全局:
def make_change(amount, coins):
    """Return a list of coins that sum to amount, preferring the smallest coins
    available and placing the smallest coins first in the returned list.

    The coins argument is a dictionary with keys that are positive integer
    denominations and values that are positive integer coin counts.

    >>> make_change(2, {2: 1})
    [2]
    >>> make_change(2, {1: 2, 2: 1})
    [1, 1]
    >>> make_change(4, {1: 2, 2: 1})
    [1, 1, 2]
    >>> make_change(4, {2: 1}) == None
    True

    >>> coins = {2: 2, 3: 2, 4: 3, 5: 1}
    >>> make_change(4, coins)
    [2, 2]
    >>> make_change(8, coins)
    [2, 2, 4]
    >>> make_change(25, coins)
    [2, 3, 3, 4, 4, 4, 5]
    >>> coins[8] = 1
    >>> make_change(25, coins)
    [2, 2, 4, 4, 5, 8]
    """
    if not coins:
        return None
    smallest = min(coins)
    rest = remove_one(coins, smallest)
    "*** YOUR CODE HERE ***"
    if amount == smallest:
        return [smallest]
    result = make_change(amount-smallest, rest)
    if result:
        return [smallest] + result
    else:
        return make_change(amount, rest)

def remove_one(coins, coin):
    """Remove one coin from a dictionary of coins. Return a new dictionary,
    leaving the original dictionary coins unchanged.

    >>> coins = {2: 5, 3: 2, 6: 1}
    >>> remove_one(coins, 2) == {2: 4, 3: 2, 6: 1}
    True
    >>> remove_one(coins, 6) == {2: 5, 3: 2}
    True
    >>> coins == {2: 5, 3: 2, 6: 1} # Unchanged
    True
    """
    copy = dict(coins)
    count = copy.pop(coin) - 1
    if count:
        copy[coin] = count
    return copy

class ChangeMachine:
    """A change machine holds a certain number of coins, initially all pennies.
    The change method adds a single coin of some denomination X and returns a
    list of coins that sums to X. The machine prefers to return the smallest
    coins available. The total value in the machine never changes, and it can
    always make change for any coin (perhaps by returning the coin passed in).

    The coins attribute is a dictionary with keys that are positive integer
    denominations and values that are positive integer coin counts.

    >>> m = ChangeMachine(2)
    >>> m.coins == {1: 2}
    True
    >>> m.change(2)
    [1, 1]
    >>> m.coins == {2: 1}
    True
    >>> m.change(2)
    [2]
    >>> m.coins == {2: 1}
    True
    >>> m.change(3)
    [3]
    >>> m.coins == {2: 1}
    True

    >>> m = ChangeMachine(10) # 10 pennies
    >>> m.coins == {1: 10}
    True
    >>> m.change(5) # takes a nickel & returns 5 pennies
    [1, 1, 1, 1, 1]
    >>> m.coins == {1: 5, 5: 1} # 5 pennies & a nickel remain
    True
    >>> m.change(3)
    [1, 1, 1]
    >>> m.coins == {1: 2, 3: 1, 5: 1}
    True
    >>> m.change(2)
    [1, 1]
    >>> m.change(2) # not enough 1's remaining; return a 2
    [2]
    >>> m.coins == {2: 1, 3: 1, 5: 1}
    True
    >>> m.change(8) # cannot use the 2 to make 8, so use 3 & 5
    [3, 5]
    >>> m.coins == {2: 1, 8: 1}
    True
    >>> m.change(1) # return the penny passed in (it's the smallest)
    [1]
    >>> m.change(9) # return the 9 passed in (no change possible)
    [9]
    >>> m.coins == {2: 1, 8: 1}
    True
    >>> m.change(10)
    [2, 8]
    >>> m.coins == {10: 1}
    True

    >>> m = ChangeMachine(9)
    >>> [m.change(k) for k in [2, 2, 3]]
    [[1, 1], [1, 1], [1, 1, 1]]
    >>> m.coins == {1: 2, 2: 2, 3: 1}
    True
    >>> m.change(5) # Prefers [1, 1, 3] to [1, 2, 2] (more pennies)
    [1, 1, 3]
    >>> m.change(7)
    [2, 5]
    >>> m.coins == {2: 1, 7: 1}
    True
    """
    def __init__(self, pennies):
        self.coins = {1: pennies}

    def change(self, coin):
        """Return change for coin, removing the result from self.coins."""
        "*** YOUR CODE HERE ***"
        self.coins[coin] = 1 + self.coins.get(coin, 0)
        result = make_change(coin, self.coins)
        for coin in result:
            count = self.coin.pop(coin) - 1
            if count:
                self.coins[coin] = count
        return result

回复

使用道具 举报

🔗
rockleecsu 2016-2-13 08:32:01 | 只看该作者
全局:
交作业,感觉第一题费了我挺久的时间,不知道是不是理解错了,我采用了偏DP的做法:


代码:
  1. def make_change(amount, coins):
  2.     """Return a list of coins that sum to amount, preferring the smallest coins
  3.     available and placing the smallest coins first in the returned list.

  4.     The coins argument is a dictionary with keys that are positive integer
  5.     denominations and values that are positive integer coin counts.

  6.     >>> make_change(2, {2: 1})
  7.     [2]
  8.     >>> make_change(2, {1: 2, 2: 1})
  9.     [1, 1]
  10.     >>> make_change(4, {1: 2, 2: 1})
  11.     [1, 1, 2]
  12.     >>> make_change(4, {2: 1}) == None
  13.     True

  14.     >>> coins = {2: 2, 3: 2, 4: 3, 5: 1}
  15.     >>> make_change(4, coins)
  16.     [2, 2]
  17.     >>> make_change(8, coins)
  18.     [2, 2, 4]
  19.     >>> make_change(25, coins)
  20.     [2, 3, 3, 4, 4, 4, 5]
  21.     >>> coins[8] = 1
  22.     >>> make_change(25, coins)
  23.     [2, 2, 4, 4, 5, 8]
  24.     """
  25.     if not coins:
  26.         return None
  27.     smallest = min(coins)
  28.     rest = remove_one(coins, smallest)
  29.     if amount - smallest == 0:
  30.         return [smallest]
  31.     elif amount - smallest < 0:
  32.         return None
  33.     else:
  34.         lst = make_change(amount - smallest, rest)
  35.         copy = dict(coins)
  36.         copy.pop(smallest)
  37.         if lst != None:
  38.             return [smallest] + lst
  39.         return  make_change(amount, copy)


  40. def remove_one(coins, coin):
  41.     """Remove one coin from a dictionary of coins. Return a new dictionary,
  42.     leaving the original dictionary coins unchanged.

  43.     >>> coins = {2: 5, 3: 2, 6: 1}
  44.     >>> remove_one(coins, 2) == {2: 4, 3: 2, 6: 1}
  45.     True
  46.     >>> remove_one(coins, 6) == {2: 5, 3: 2}
  47.     True
  48.     >>> coins == {2: 5, 3: 2, 6: 1} # Unchanged
  49.     True
  50.     """
  51.     copy = dict(coins)
  52.     count = copy.pop(coin) - 1
  53.     if count:
  54.         copy[coin] = count
  55.     return copy

  56. class ChangeMachine:
  57.     """A change machine holds a certain number of coins, initially all pennies.
  58.     The change method adds a single coin of some denomination X and returns a
  59.     list of coins that sums to X. The machine prefers to return the smallest
  60.     coins available. The total value in the machine never changes, and it can
  61.     always make change for any coin (perhaps by returning the coin passed in).

  62.     The coins attribute is a dictionary with keys that are positive integer
  63.     denominations and values that are positive integer coin counts.

  64.     >>> m = ChangeMachine(2)
  65.     >>> m.coins == {1: 2}
  66.     True
  67.     >>> m.change(2)
  68.     [1, 1]
  69.     >>> m.coins == {2: 1}
  70.     True
  71.     >>> m.change(2)
  72.     [2]
  73.     >>> m.coins == {2: 1}
  74.     True
  75.     >>> m.change(3)
  76.     [3]
  77.     >>> m.coins == {2: 1}
  78.     True

  79.     >>> m = ChangeMachine(10) # 10 pennies
  80.     >>> m.coins == {1: 10}
  81.     True
  82.     >>> m.change(5) # takes a nickel & returns 5 pennies
  83.     [1, 1, 1, 1, 1]
  84.     >>> m.coins == {1: 5, 5: 1} # 5 pennies & a nickel remain
  85.     True
  86.     >>> m.change(3)
  87.     [1, 1, 1]
  88.     >>> m.coins == {1: 2, 3: 1, 5: 1}
  89.     True
  90.     >>> m.change(2)
  91.     [1, 1]
  92.     >>> m.change(2) # not enough 1's remaining; return a 2
  93.     [2]
  94.     >>> m.coins == {2: 1, 3: 1, 5: 1}
  95.     True
  96.     >>> m.change(8) # cannot use the 2 to make 8, so use 3 & 5
  97.     [3, 5]
  98.     >>> m.coins == {2: 1, 8: 1}
  99.     True
  100.     >>> m.change(1) # return the penny passed in (it's the smallest)
  101.     [1]
  102.     >>> m.change(9) # return the 9 passed in (no change possible)
  103.     [9]
  104.     >>> m.coins == {2: 1, 8: 1}
  105.     True
  106.     >>> m.change(10)
  107.     [2, 8]
  108.     >>> m.coins == {10: 1}
  109.     True

  110.     >>> m = ChangeMachine(9)
  111.     >>> [m.change(k) for k in [2, 2, 3]]
  112.     [[1, 1], [1, 1], [1, 1, 1]]
  113.     >>> m.coins == {1: 2, 2: 2, 3: 1}
  114.     True
  115.     >>> m.change(5) # Prefers [1, 1, 3] to [1, 2, 2] (more pennies)
  116.     [1, 1, 3]
  117.     >>> m.change(7)
  118.     [2, 5]
  119.     >>> m.coins == {2: 1, 7: 1}
  120.     True
  121.     """
  122.     def __init__(self, pennies):
  123.         self.coins = {1: pennies}

  124.     def change(self, coin):
  125.         """Return change for coin, removing the result from self.coins."""
  126.         if coin not in self.coins:
  127.             self.coins[coin] = 0
  128.         self.coins[coin] += 1
  129.         res_lst = make_change(coin, self.coins)
  130.         for item in res_lst:
  131.             self.coins = remove_one(self.coins, item)
  132.         return res_lst
复制代码
回复

使用道具 举报

🔗
Liaeve 2016-5-3 21:09:44 | 只看该作者
全局:
  1. def make_change(amount, coins):
  2.     """Return a list of coins that sum to amount, preferring the smallest coins
  3.     available and placing the smallest coins first in the returned list.

  4.     The coins argument is a dictionary with keys that are positive integer
  5.     denominations and values that are positive integer coin counts.

  6.     >>> make_change(2, {2: 1})
  7.     [2]
  8.     >>> make_change(2, {1: 2, 2: 1})
  9.     [1, 1]
  10.     >>> make_change(4, {1: 2, 2: 1})
  11.     [1, 1, 2]
  12.     >>> make_change(4, {2: 1}) == None
  13.     True

  14.     >>> coins = {2: 2, 3: 2, 4: 3, 5: 1}
  15.     >>> make_change(4, coins)
  16.     [2, 2]
  17.     >>> make_change(8, coins)
  18.     [2, 2, 4]
  19.     >>> make_change(25, coins)
  20.     [2, 3, 3, 4, 4, 4, 5]
  21.     >>> coins[8] = 1
  22.     >>> make_change(25, coins)
  23.     [2, 2, 4, 4, 5, 8]
  24.     """
  25.     if not coins:
  26.         return None
  27.     smallest = min(coins)
  28.     rest = remove_one(coins, smallest)
  29.     "*** YOUR CODE HERE ***"
  30.     if amount == smallest:
  31.         return [smallest]
  32.     lst = make_change(amount - smallest, rest)
  33.     if lst:
  34.         return [smallest] + lst
  35.     else:
  36.         return make_change(amount, rest)

  37. def remove_one(coins, coin):
  38.     """Remove one coin from a dictionary of coins. Return a new dictionary,
  39.     leaving the original dictionary coins unchanged.

  40.     >>> coins = {2: 5, 3: 2, 6: 1}
  41.     >>> remove_one(coins, 2) == {2: 4, 3: 2, 6: 1}
  42.     True
  43.     >>> remove_one(coins, 6) == {2: 5, 3: 2}
  44.     True
  45.     >>> coins == {2: 5, 3: 2, 6: 1} # Unchanged
  46.     True
  47.     """
  48.     copy = dict(coins)
  49.     count = copy.pop(coin) - 1
  50.     if count:
  51.         copy[coin] = count
  52.     return copy

  53. class ChangeMachine:
  54.     """A change machine holds a certain number of coins, initially all pennies.
  55.     The change method adds a single coin of some denomination X and returns a
  56.     list of coins that sums to X. The machine prefers to return the smallest
  57.     coins available. The total value in the machine never changes, and it can
  58.     always make change for any coin (perhaps by returning the coin passed in).

  59.     The coins attribute is a dictionary with keys that are positive integer
  60.     denominations and values that are positive integer coin counts.

  61.     >>> m = ChangeMachine(2)
  62.     >>> m.coins == {1: 2}
  63.     True
  64.     >>> m.change(2)
  65.     [1, 1]
  66.     >>> m.coins == {2: 1}
  67.     True
  68.     >>> m.change(2)
  69.     [2]
  70.     >>> m.coins == {2: 1}
  71.     True
  72.     >>> m.change(3)
  73.     [3]
  74.     >>> m.coins == {2: 1}
  75.     True

  76.     >>> m = ChangeMachine(10) # 10 pennies
  77.     >>> m.coins == {1: 10}
  78.     True
  79.     >>> m.change(5) # takes a nickel & returns 5 pennies
  80.     [1, 1, 1, 1, 1]
  81.     >>> m.coins == {1: 5, 5: 1} # 5 pennies & a nickel remain
  82.     True
  83.     >>> m.change(3)
  84.     [1, 1, 1]
  85.     >>> m.coins == {1: 2, 3: 1, 5: 1}
  86.     True
  87.     >>> m.change(2)
  88.     [1, 1]
  89.     >>> m.change(2) # not enough 1's remaining; return a 2
  90.     [2]
  91.     >>> m.coins == {2: 1, 3: 1, 5: 1}
  92.     True
  93.     >>> m.change(8) # cannot use the 2 to make 8, so use 3 & 5
  94.     [3, 5]
  95.     >>> m.coins == {2: 1, 8: 1}
  96.     True
  97.     >>> m.change(1) # return the penny passed in (it's the smallest)
  98.     [1]
  99.     >>> m.change(9) # return the 9 passed in (no change possible)
  100.     [9]
  101.     >>> m.coins == {2: 1, 8: 1}
  102.     True
  103.     >>> m.change(10)
  104.     [2, 8]
  105.     >>> m.coins == {10: 1}
  106.     True

  107.     >>> m = ChangeMachine(9)
  108.     >>> [m.change(k) for k in [2, 2, 3]]
  109.     [[1, 1], [1, 1], [1, 1, 1]]
  110.     >>> m.coins == {1: 2, 2: 2, 3: 1}
  111.     True
  112.     >>> m.change(5) # Prefers [1, 1, 3] to [1, 2, 2] (more pennies)
  113.     [1, 1, 3]
  114.     >>> m.change(7)
  115.     [2, 5]
  116.     >>> m.coins == {2: 1, 7: 1}
  117.     True
  118.     """
  119.     def __init__(self, pennies):
  120.         self.coins = {1: pennies}

  121.     def change(self, coin):
  122.         """Return change for coin, removing the result from self.coins."""
  123.         "*** YOUR CODE HERE ***"
  124.         self.coins[coin] = 1 + self.coins.get(coin, 0)
  125.         result = make_change(coin, self.coins)
  126.         for coin in result:
  127.             count = self.coins.pop(coin) - 1
  128.             if count:
  129.                 self.coins[coin] = count
  130.         return result
复制代码
回复

使用道具 举报

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

本版积分规则

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