San Jose各个房价 <1.5m 区域买房总结

一亩三分地论坛

 找回密码
 Sign Up 注册获取更多干货
E轮2.5亿美元融资
K12教育独角兽一起作业
北京-诚聘人工智能/教育/大数据岗
坐标湾区
Games Startup
招聘游戏开发工程师
游戏初创公司招聘工程师、UIUX Designer和游戏策划
码农求职神器Triplebyte:
不用海投
内推多家公司面试
把贵司招聘信息放这里
查看: 848|回复: 5
收起左侧

[CS61A]Quiz02

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

[其他]CS61A #3 - 04@UCBerkely

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

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

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

评分

1

查看全部评分

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

使用道具 举报

全球28万学生4.7分推荐
西风吹草 发表于 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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-2-13 08:32:01 | 显示全部楼层
交作业,感觉第一题费了我挺久的时间,不知道是不是理解错了,我采用了偏DP的做法:
quiz02.png

代码:
  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
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

提醒:发帖可以选择内容隐藏,部分板块支持匿名发帖。请认真读完以下全部说明:

■隐藏内容方法: [hide=200]你想要隐藏的内容比如面经[/hide]
■意思是:用户积分低于200则看不到被隐藏的内容
■可以自行设置积分值,不建议太高(200以上太多人看不到),也不建议太低(那就没必要隐藏了)
■建议只隐藏关键内容,比如具体的面试题目、涉及隐私的信息,大部分内容没必要隐藏。
■微信/QQ/电子邮件等,为防止将来被骚扰甚至人肉,以论坛私信方式发给对方最安全。
■匿名发帖的板块和方法:http://www.1point3acres.com/bbs/thread-405991-1-1.html

关闭

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

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2018-5-26 08:50

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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