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

[CS61A]Homework 1

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

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

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

x
本帖最后由 AveMaleficum 于 2015-5-7 01:28 编辑

第一次作业,范围chapter1的section2到5。
http://gaotx.com/cs61a/hw/hw01/


补充内容 (2015-5-7 23:47):
我的答案
# Q1
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
    """
   ...

补充内容 (2015-5-7 23:48):
我的答案在15楼

上一篇:[CS61A]Lab0和Lab01
下一篇:[CS61A]Lab02 & Quiz01
推荐
rockleecsu 2015-12-21 10:56:14 | 只看该作者
全局:
完成作业!!!


代码如下:
  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.         "*** YOUR CODE HERE ***"
  27.         return a * a + b * b + c * c - min(a, b, c) ** 2

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

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

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

  53. def with_if_function():
  54.         return if_function(c(), t(), f())

  55. def c():
  56.         "*** YOUR CODE HERE ***"
  57.         return True

  58. def t():
  59.         "*** YOUR CODE HERE ***"
  60.         return 1

  61. def f():
  62.         "*** YOUR CODE HERE ***"
  63.         print("Wrong")

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

  67.         >>> a = hailstone(10)
  68.         10
  69.         5
  70.         16
  71.         8
  72.         4
  73.         2
  74.         1
  75.         >>> a
  76.         7
  77.         """
  78.         "*** YOUR CODE HERE ***"
  79.         iteration = 1
  80.         print(n)
  81.         while n != 1:
  82.                 iteration += 1
  83.                 if n % 2 == 0:
  84.                         n //= 2
  85.                 else:
  86.                         n = n * 3 + 1
  87.                 print(n)
  88.         return iteration
复制代码
回复

使用道具 举报

推荐
wuxiaomin98 2015-5-12 10:44:29 | 只看该作者
全局:
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 = a - b
    else:
        f = a + b
    return f

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
    """
    "*** YOUR CODE HERE ***"
    return max(a, b) * max(a, b) + max(b, c) * max(b, 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():
    "*** YOUR CODE HERE ***"
    return false
def t():
    "*** YOUR CODE HERE ***"
    return 0
def f():
    "*** YOUR CODE HERE ***"
    return 1
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
    """
    "*** YOUR CODE HERE ***"
    step = 1
    print(n)
    while n != 1:
        if n % 2 == 0:
            n = n / 2
            print(int(n))
        else:
            n = n * 3 +1
            print(int(n))
        step = step + 1
    return step
Assignment: Homework 1
OK, version v1.3.13
=====================================================================

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests

---------------------------------------------------------------------
Doctests for a_plus_abs_b

>>> from hw01 import *
>>> a_plus_abs_b(2, 3)
5
>>> a_plus_abs_b(2, -3)
5
-- OK! --

---------------------------------------------------------------------
Doctests for two_of_three

>>> from hw01 import *
>>> 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
-- OK! --

---------------------------------------------------------------------
Doctests for hailstone

>>> from hw01 import *
>>> a = hailstone(10)
10
5
16
8
4
2
1
>>> a
7
-- OK! --

---------------------------------------------------------------------
Test summary
    Passed: 3
    Failed: 0
[ooooooooook] 100.0% passed
回复

使用道具 举报

推荐
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.    
复制代码
回复

使用道具 举报

🔗
go7going 2015-5-6 22:16:40 | 只看该作者
全局:
HW 1 all the codes:

Q1: from operator import add, sub

def a_plus_abs_b(a, b):
     if b < 0:
        f = sub
    else:
        f = add
    return f(a, b)

Q2: def two_of_three(a, b, c):
return max(a*a+b*b, a*a+c*c, b*b+c*c)

Q3: def if_function(condition, true_result, false_result):
if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
if c():
        return t()
    else:
        return f()

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

def c():
    return False

def t():
    1/0

def f():
    return 1

Q4: def hailstone(n):
length = 1
    while n != 1:
        print(n)
        if n % 2 == 0:
            n = n / 2     
        else:
            n = 3 * n + 1
        length = length + 1
    print(n)              
    return length

评分

参与人数 2大米 +1 学分 +1 收起 理由
非雨 + 1 给你点个赞!
AveMaleficum + 1

查看全部评分

回复

使用道具 举报

全局:
from operator import add, sub

def a_plus_abs_b(a,b):
       
        if b < 0:
                f = sub
        else:
                f = add
        return f(a,b)

def two_of_three(a, b, c):

        if a >= b:
                if b >= c:
                        return a*a + b*b
                else:
                        return a*a + c*c

        else:
                if a >= c:
                        return a*a + b*b
                else:
                        return b*b + c*c


def if_function(condition, true_result, false_result):
       
        if condition:
                return true_result
        else:
                return false_result

def with_if_statement():
       
        if c():
                return t()
        else:
                return f()

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

def c():
        return 1==0

def t():
        return 0

def f():
        return 1


def hailstone(n):

        length = 1
        while(n!=1):
                print(n)
                if n%2==0:
                        n /= 2
                else:
                        n = n*3 + 1
                length += 1
        print(n)
        return length

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

使用道具 举报

🔗
 楼主| sky420 2015-5-6 23:54:56 | 只看该作者
全局:
AaronTiberium 发表于 2015-5-6 23:08
from operator import add, sub

def a_plus_abs_b(a,b):

eval的input是一个spring value,evaluate这个input,并输出结果,例如,s='print("Hello!")', eval(s)输出Hello!相当于运行了 print("Hello!")。
repr把任何input直接转换成spring value然后print出来,例如,s='print("Hello!")',repr输出'\'print("Hello")\'',注意在repr中,如果inpu的中有,single quote, ('),在output中就会被写成\'。

s= 'print("s = " + repr(s) + "; eval(s)")'; eval(s)
在这个命令中,在repr(s)之前, s='print("s=")', 所以repr(s)输出的是当前的s,不是最终的s。而eval(s)中的s是全部的s,s= 'print("s = " + repr(s) + "; eval(s)")'。eval(s)中,evaluate的部分是print("s = " + repr(s) + "; eval(s)")。
回复

使用道具 举报

🔗
Howie 2015-5-7 00:05:16 | 只看该作者
全局:
sky420 发表于 2015-5-6 23:54
eval的input是一个spring value,evaluate这个input,并输出结果,例如,s='print("Hello!")', eval(s)输 ...

话说这个question3就是为了说明call expression的时候,里面的值都会算,所以如果true跑死了。。就跑不到false了么。。
回复

使用道具 举报

🔗
 楼主| sky420 2015-5-7 00:33:14 | 只看该作者
全局:
Howie 发表于 2015-5-7 00:05
话说这个question3就是为了说明call expression的时候,里面的值都会算,所以如果true跑死了。。就跑不到 ...

不是,而是会先跑formal parameter,然后在世body
回复

使用道具 举报

🔗
Howie 2015-5-7 00:34:09 | 只看该作者
全局:
sky420 发表于 2015-5-7 00:33
不是,而是会先跑formal parameter,然后在世body

额 我就是说的这个意思。。所以参数里面跑死了 就不对了,,就是这个意思?
回复

使用道具 举报

🔗
 楼主| sky420 2015-5-7 01:18:19 | 只看该作者
全局:
对的。你说“里面”,我以为是指body
回复

使用道具 举报

🔗
FFFelix 2015-5-7 08:27:55 | 只看该作者
全局:
好几天以前做的  不太想的起来了。 先直接交了~
import doctest
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
    else:
        f = add
    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
    """
    "*** YOUR CODE HERE ***"
    d = min(a,b,c)
    return a * a + b * b + c * c - d * d

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():
    "*** YOUR CODE HERE ***"
    return True

def t():
    "*** YOUR CODE HERE ***"
    return 1

def f():
    "*** YOUR CODE HERE ***"
    return 0

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
    """
    "*** YOUR CODE HERE ***"
    len = 1
    while n >= 1:
        print (n)

        if n == 1:
            return len
        elif n % 2 == 0:
            n = n // 2
        else:
            n = 3 * n + 1

        len += 1
    return len

if __name__ == "__main__":
    doctest.testmod()

回复

使用道具 举报

🔗
BigCong 2015-5-7 08:50:12 | 只看该作者
全局:
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
    else:
        f = add
    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
    """
    "*** YOUR CODE HERE ***"
    minum = min(min(a,b),c)
    return a*a+b*b+c*c-minum*minum
   


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 1

def t():
    return 1

def f():
    Nonexxx

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
    """
    "*** YOUR CODE HERE ***"
    i=1
    while n!=1:
        print(int(n))
        if n%2:
            n=n*3+1
        else:
            n=n/2
        i = i+1
        if n==1:
            print(1)
    return i
回复

使用道具 举报

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

本版积分规则

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