📣 VIP通行证夏日特惠 限时立减$68
楼主: sky420
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Homework 1

🔗
Kevin2015 2016-5-1 19:26:35 | 只看该作者
全局:
  1. from operator import add, sub
  2. def a_plus_abs_b(a, b):
  3.     if b < 0:
  4.         f = sub
  5.     else:
  6.         f = add
  7.     return f(a,b)

  8. print(a_plus_abs_b(2,-3))
复制代码


  1. def two_of_three(a, b, c):
  2.     """Return x*x + y*y, where x and y are the two largest members of the
  3.     positive numbers a, b, and c.

  4.     >>> two_of_three(1, 2, 3)
  5.     13
  6.     >>> two_of_three(5, 3, 1)
  7.     34
  8.     >>> two_of_three(10, 2, 8)
  9.     164
  10.     >>> two_of_three(5, 5, 5)
  11.     50
  12.     """
  13.     "*** YOUR CODE HERE ***"
  14.     return max(a * a + b * b, a * a + c * c, b * b + c * c)

  15. print(two_of_three(1, 2, 5))
复制代码




回复

使用道具 举报

🔗
lianyujia 2016-5-3 03:19:10 | 只看该作者
全局:
本帖最后由 lianyujia 于 2016-5-3 03:22 编辑
  1. from operator import add, sub
  2. import time
  3. def a_plus_abs_b(a, b):
  4.     """Return a+abs(b), but without calling abs.

  5.     >>> a_plus_abs_b(2, 3)
  6.     5
  7.     >>> a_plus_abs_b(2, -3)
  8.     5
  9.     """
  10.     if b < 0:
  11.         return a - b
  12.     else:
  13.         return a + b
  14.    

  15. def two_of_three(a, b, c):
  16.     """Return x*x + y*y, where x and y are the two largest members of the
  17.     positive numbers a, b, and c.

  18.     >>> two_of_three(1, 2, 3)
  19.     13
  20.     >>> two_of_three(5, 3, 1)
  21.     34
  22.     >>> two_of_three(10, 2, 8)
  23.     164
  24.     >>> two_of_three(5, 5, 5)
  25.     50
  26.     """
  27.     "*** YOUR CODE HERE ***"

  28.     if b <= a & b <= c:
  29.             return a*a + c*c
  30.     if c <= b & c <= a:
  31.             return a*a +b*b
  32.     else:
  33.             return b*b + c*c

  34.    

  35. def if_function(condition, true_result, false_result):
  36.     """Return true_result if condition is a true value, and
  37.     false_result otherwise.

  38.     >>> if_function(True, 2, 3)
  39.     2
  40.     >>> if_function(False, 2, 3)
  41.     3
  42.     >>> if_function(3==2, 3+2, 3-2)
  43.     1
  44.     >>> if_function(3>2, 3+2, 3-2)
  45.     5
  46.     """
  47.     if condition:
  48.         return true_result
  49.     else:
  50.         return false_result

  51. def with_if_statement():
  52.     """
  53.     >>> with_if_statement()
  54.     1
  55.     """
  56.     if c():
  57.         return t()
  58.     else:
  59.         return f()

  60. def with_if_function():
  61.     return if_function(c(), t(), f())

  62. def c():
  63.     "*** YOUR CODE HERE ***"   
  64.     return True

  65. def t():
  66.     "*** YOUR CODE HERE ***"
  67.     return 1

  68. def f():
  69.     "*** YOUR CODE HERE ***"
  70.     return 'shit' / 0


  71. def hailstone(n):
  72.     """Print the hailstone sequence starting at n and return its
  73.     length.
  74.     >>> a = hailstone(10)
  75.     10
  76.     5
  77.     16
  78.     8
  79.     4
  80.     2
  81.     1
  82.     >>> a
  83.     7
  84.     """
  85.     "*** YOUR CODE HERE ***"
  86.     length = 1
  87.     while n != 1:
  88.         print (int(n))
  89.         if n % 2 == 0:
  90.             n = n / 2
  91.         else:
  92.             n = 3*n + 1
  93.         length = length + 1
  94.     print (int(n))
  95.     return length


  96. challenge_question_program = """
  97. "*** YOUR CODE HERE ***
  98. """
  99. s = 'print("s=" + repr(s) + ";eval(s)")';eval(s) return s
复制代码
回复

使用道具 举报

🔗
lianyujia 2016-5-3 03:25:31 | 只看该作者
全局:
第三问其实上面都不对,人家要求statement和function返回的结果不一样,你们返回都是一样的,我想了半天不会,整了个报错的。。。。。反正不一样。。。。最后一个不会写。。。太菜
回复

使用道具 举报

🔗
Kevin2015 2016-5-4 10:28:52 | 只看该作者
全局:
lianyujia 发表于 2016-5-3 03:25
第三问其实上面都不对,人家要求statement和function返回的结果不一样,你们返回都是一样的,我想了半天不 ...

哈哈,不用谦虚,我还就会前两道呢,慢慢来就好
回复

使用道具 举报

🔗
wkh279 2016-11-19 11:05:02 | 只看该作者
全局:
最后一问 challenge question想了一会儿没想出来。。。


  1. from operator import add, sub

  2. def a_plus_abs_b(a, b):
  3.     """Return a+abs(b), but without calling abs.

  4.     >>> a_plus_abs_b(2, 3)
  5.     5
  6.     >>> a_plus_abs_b(2, -3)
  7.     5
  8.     """
  9.     if b < 0:
  10.         f = sub(a,b)
  11.     else:
  12.         f = add(a,b)
  13.     return f

  14. def two_of_three(a, b, c):
  15.     """Return x*x + y*y, where x and y are the two largest members of the
  16.     positive numbers a, b, and c.

  17.     >>> two_of_three(1, 2, 3)
  18.     13
  19.     >>> two_of_three(5, 3, 1)
  20.     34
  21.     >>> two_of_three(10, 2, 8)
  22.     164
  23.     >>> two_of_three(5, 5, 5)
  24.     50
  25.     """
  26.     if a<b:
  27.         a,b=b,a
  28.     if a<c:
  29.         a,c=c,a
  30.     if b<c:
  31.         b,c=c,b
  32.     return a*a+b*b


  33. def if_function(condition, true_result, false_result):
  34.     """Return true_result if condition is a true value, and
  35.     false_result otherwise.

  36.     >>> if_function(True, 2, 3)
  37.     2
  38.     >>> if_function(False, 2, 3)
  39.     3
  40.     >>> if_function(3==2, 3+2, 3-2)
  41.     1
  42.     >>> if_function(3>2, 3+2, 3-2)
  43.     5
  44.     """
  45.     if condition:
  46.         return true_result
  47.     else:
  48.         return false_result

  49. def with_if_statement():
  50.     """
  51.     >>> with_if_statement()
  52.     1
  53.     """
  54.     if c():
  55.         return t()
  56.     else:
  57.         return f()

  58. def with_if_function():
  59.     return if_function(c(), t(), f())

  60. def c():
  61.     return 1

  62. def t():
  63.     return 1

  64. def f():
  65.     "Return an variable diliberitely so that with_if_function with will return an error but with_if_statement will not"
  66.     return error

  67. def hailstone(n):
  68.     """Print the hailstone sequence starting at n and return its
  69.     length.

  70.     >>> a = hailstone(10)
  71.     10
  72.     5
  73.     16
  74.     8
  75.     4
  76.     2
  77.     1
  78.     >>> a
  79.     7
  80.     """
  81.     length=0
  82.     while n>1:
  83.         print(n)
  84.         if n % 2==0:
  85.             n=n//2
  86.         else:
  87.             n=3*n+1
  88.         length+=1
  89.     print(n)
  90.     length+=1
  91.     return length

  92. challenge_question_program = """
  93. "*** YOUR CODE HERE ***"
  94. """


复制代码
回复

使用道具 举报

全局:
from operator import add, sub

def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = sub(a, b)
    else:
        f = add(a, b)
    return f(a, b)

def two_of_three(a, b, c):
    """Return x*x + y*y, where x and y are the two largest members of the
    positive numbers a, b, and c.

    >>> two_of_three(1, 2, 3)
    13
    >>> two_of_three(5, 3, 1)
    34
    >>> two_of_three(10, 2, 8)
    164
    >>> two_of_three(5, 5, 5)
    50
    """
    if a <= b & b <= c:
        return b*b + c*c
    else:
        if b >= c:
            return a*a + b*b
        else:
            return a*a + c*c


def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    return true

def t():
    return 1

def f():
    return

def hailstone(n):
    """Print the hailstone sequence starting at n and return its
    length.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    print(n)
    i = 1
    while(n != 1):
        if n%2 == 0:
            n = n/2
            print(n)
        else:
            n = n * 3 + 1
            print(n)
        i = i + 1
    return i


challenge_question_program = """
s = 'print("s = " + repr(s) + "; eval(s)")'; eval(s)
"""
回复

使用道具 举报

全局:
rarezhang 发表于 2015-5-7 12:28
交作业: https://bitbucket.org/rarezhang/ucberkely_cs61a/src/63ddd9c4e7f39a8f5abcde757f3c86530dcede2 ...

哈哈,hailstone和我一样直接想到递归,我当时都忘了这门课还没讲递归呢
回复

使用道具 举报

🔗
梁笑晨 2018-2-7 16:15:26 | 只看该作者
全局:
  1. from operator import add, sub

  2. def a_plus_abs_b(a, b):
  3.     """Return a+abs(b), but without calling abs.

  4.     >>> a_plus_abs_b(2, 3)
  5.     5
  6.     >>> a_plus_abs_b(2, -3)
  7.     5
  8.     """
  9.     if b < 0:
  10.         f = sub
  11.     else:
  12.         f = add
  13.     return f(a, b)

  14. def two_of_three(a, b, c):
  15.     """Return x*x + y*y, where x and y are the two largest members of the
  16.     positive numbers a, b, and c.

  17.     >>> two_of_three(1, 2, 3)
  18.     13
  19.     >>> two_of_three(5, 3, 1)
  20.     34
  21.     >>> two_of_three(10, 2, 8)
  22.     164
  23.     >>> two_of_three(5, 5, 5)
  24.     50
  25.     """
  26.     assert a > 0 and b > 0 and c > 0
  27.     list = [a, b, c]
  28.     list.remove(max(list))
  29.     return pow(max(a, b,c),2) + pow(max(list),2)

  30. def if_function(condition, true_result, false_result):
  31.     """Return true_result if condition is a true value, and
  32.     false_result otherwise.

  33.     >>> if_function(True, 2, 3)
  34.     2
  35.     >>> if_function(False, 2, 3)
  36.     3
  37.     >>> if_function(3==2, 3+2, 3-2)
  38.     1
  39.     >>> if_function(3>2, 3+2, 3-2)
  40.     5
  41.     """
  42.     if condition:
  43.         return true_result
  44.     else:
  45.         return false_result

  46. def with_if_statement():
  47.     """
  48.     >>> with_if_statement()
  49.     1
  50.     """
  51.     if c():
  52.         return t()
  53.     else:
  54.         return f()

  55. def with_if_function():
  56.     return if_function(c(), t(), f())

  57. def c():
  58.     "*** YOUR CODE HERE ***"
  59.     return False

  60. def t():
  61.     "*** YOUR CODE HERE ***"
  62.     return 0

  63. def f():
  64.     "*** YOUR CODE HERE ***"
  65.     return 1

  66. def hailstone(n):
  67.     """Print the hailstone sequence starting at n and return its
  68.     length.

  69.     >>> a = hailstone(10)
  70.     10
  71.     5
  72.     16
  73.     8
  74.     4
  75.     2
  76.     1
  77.     >>> a
  78.     7
  79.     """
  80.     "*** YOUR CODE HERE ***"
  81.     assert n > 0
  82.     k = 1
  83.     print(n)
  84.     while n != 1:
  85.         k = k + 1
  86.         if n % 2 == 0:
  87.             n = int(n / 2)
  88.         else:
  89.             n = 3 * n + 1
  90.         print(n)
  91.     return k
  92.         
  93.    

  94. challenge_question_program = """
  95. "*** YOUR CODE HERE ***"
  96. """
  97. s = 'print("s = " + repr(s) + "; eval(s)")'; eval(s)


复制代码
回复

使用道具 举报

🔗
Fucksky 2018-2-22 13:45:25 | 只看该作者
全局:
  1. from operator import add, sub

  2. def a_plus_abs_b(a, b):
  3.     """Return a+abs(b), but without calling abs.

  4.     >>> a_plus_abs_b(2, 3)
  5.     5
  6.     >>> a_plus_abs_b(2, -3)
  7.     5
  8.     """
  9.     if b < 0:
  10.         f = sub
  11.     else:
  12.         f = add
  13.     return f(a, b)

  14. def two_of_three(a, b, c):
  15.     """Return x*x + y*y, where x and y are the two largest members of the
  16.     positive numbers a, b, and c.

  17.     >>> two_of_three(1, 2, 3)
  18.     13
  19.     >>> two_of_three(5, 3, 1)
  20.     34
  21.     >>> two_of_three(10, 2, 8)
  22.     164
  23.     >>> two_of_three(5, 5, 5)
  24.     50
  25.     """
  26.     return sub(a*a+b*b+c*c, min(a, b, c)*min(a, b, c))

  27. def largest_factor(n):
  28.     """Return the largest factor of n that is smaller than n.

  29.     >>> largest_factor(15) # factors are 1, 3, 5
  30.     5
  31.     >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
  32.     40
  33.     >>> largest_factor(13) # factor is 1 since 13 is prime
  34.     1
  35.     """
  36.     largest_factor = 1
  37.     for i in range(1,n):
  38.         if n%i == 0:
  39.             largest_factor = i
  40.     return largest_factor

  41. def if_function(condition, true_result, false_result):
  42.     """Return true_result if condition is a true value, and
  43.     false_result otherwise.

  44.     >>> if_function(True, 2, 3)
  45.     2
  46.     >>> if_function(False, 2, 3)
  47.     3
  48.     >>> if_function(3==2, 3+2, 3-2)
  49.     1
  50.     >>> if_function(3>2, 3+2, 3-2)
  51.     5
  52.     """
  53.     if condition:
  54.         return true_result
  55.     else:
  56.         return false_result


  57. def with_if_statement():
  58.     """
  59.     >>> with_if_statement()
  60.     1
  61.     """
  62.     if c():
  63.         return t()
  64.     else:
  65.         return f()

  66. def with_if_function():
  67.     return if_function(c(), t(), f())

  68. def c():
  69.     "*** YOUR CODE HERE ***"
  70.     return True

  71. def t():
  72.     "*** YOUR CODE HERE ***"
  73.     return 1

  74. def f():
  75.     "*** YOUR CODE HERE ***"
  76.     return RuntimeError

  77. def hailstone(n):
  78.     """Print the hailstone sequence starting at n and return its
  79.     length.

  80.     >>> a = hailstone(10)
  81.     10
  82.     5
  83.     16
  84.     8
  85.     4
  86.     2
  87.     1
  88.     >>> a
  89.     7
  90.     """
  91.     "*** YOUR CODE HERE ***"
  92.     count = 0
  93.     while True:
  94.         print(n)
  95.         count = count+1
  96.         if n == 1:
  97.             break
  98.         if n%2 == 0:
  99.             n = n/2
  100.         else:
  101.             n = 3*n+1
  102.     return count
  103.    
复制代码
回复

使用道具 举报

🔗
sry19 2018-9-15 16:42:35 | 只看该作者
全局:
最后一题,是之前s已经存储好了,直接执行eval(s),然后执行的时候s='print("s="......;eval(s)")',所以repr的时候已经是整个语句了吧??
回复

使用道具 举报

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

本版积分规则

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