楼主: sky420
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Homework 1

🔗
panda8787 2015-5-9 13:53:21 | 只看该作者
全局:
from operator import add, sub
import random

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

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

print(a_plus_abs_b(3,-6))

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.
    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
print(two_of_three(1,2,3))

def if_function(condition, true_result, false_result):
    # """Return true_result if condition is a true value, and
    # false_result otherwise.
    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():
    return 1/0

def f():
    return 1

print(with_if_function())
print(with_if_statement())

def hailstone(n):
    # """Print the hailstone sequence starting at n and return its
    # length.
    count = 1
    print(n)
    while n!= 1:
        if n%2 ==0:
            n = n/2
        else:
            n = 3*n + 1
        print(n)
        count += 1
    return count

a = hailstone(10)
print(a)
回复

使用道具 举报

🔗
karte_polo 2015-5-9 14:07:40 | 只看该作者
全局:
from operator import add, sub

def a_plus_abs_b(a, b):

    if b < 0:
        f = a-b
    else:
        f = a+b
    return f(a, b)

def two_of_three(a, b, c):

    a_s =mul(a,a)
    b_s =mul(b,b)
    c_s =mul(c,c)
    if a>=b:
        if b>=c:
        return add(a_s,b_s)
        else:
        return add(a_s,c_s)
    else:
        if a>=c:
        return add(a_s,b_s)
        else:
        return add(b_s,c_s)

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==2

def t():
    return None

def f():
    return 1

def hailstone(n):

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


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

回复

使用道具 举报

🔗
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
回复

使用道具 举报

🔗
qiuyuxiruchou 2015-5-14 10:54:23 | 只看该作者
全局:
from operator import add, sub
""" use "python2 -m doctest name.py" to test  """
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)
print a_plus_abs_b(2,3)

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
    """
    return max(a*a+b*b, a*a+c*c, b*b+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 False

def t():
    return 0

def f():
    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
    """
    count=1
    while n!=1:
        print n  
        if n%2==0:
            n=n/2
        else:
            n=n*3+1
        count=count+1
    print n
    return count
       
challenge_question_program = """
s = 'print("s = " + " eval(s)" + repr(s) )'; eval(s);return s
"""
print challenge_question_program
回复

使用道具 举报

🔗
urekuk 2015-5-16 14:42:19 | 只看该作者
全局:
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 = add(a, -b)
    else:
        f = add(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 a * a + b * b + c * c - min(a, b, c) * min(a, 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 True

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


def f():
    "*** YOUR CODE HERE ***"
    print('f() Executed.')

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 ***"
    numOfSteps = 1
    print(n)
    while(n != 1):
            if(n % 2 == 0): # n is even number
                    n = n // 2
            else: # n is odd number
                    n = n * 3 + 1
            numOfSteps += 1
            print(n)
    return numOfSteps
回复

使用道具 举报

🔗
guangstick 2015-5-17 04:31:04 | 只看该作者
全局:
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
    """
    return a*a+b*b+c*c-min(a,b,c)*min(a,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():
    return 1

def t():
    print('1')

def f():
    1/0 #zero division

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

使用道具 举报

🔗
Liaeve 2015-10-14 19:19:04 | 只看该作者
全局:
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 = add(a, sub(0, b))
    else:
        f = add(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
    """
    f = a * a + b * b + c * c - min(a, b, c) * min(a, b, c)
    return f

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 False

def t():
    1/0

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

使用道具 举报

🔗
Chris1993 2015-10-22 14:28:17 | 只看该作者
全局:
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
    """
    return max(a,b)*max(a,b)+max(min(a,b),c)*max(min(a,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():
    return True

def t():
    return 1

def f():
   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
    """
    a = 1
    print(n)
    while(n != 1):
        if(n%2 == 0):
            n = n/2
            print(n)
            a += 1

        else:
            n = 3*n +1
            print(n)
            a += 1
   
回复

使用道具 举报

🔗
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
复制代码
回复

使用道具 举报

🔗
小菜 2015-12-29 13:21:29 | 只看该作者
全局:
想知道大家做project的时候有没什么好办法验证自己做的对不对?(除了对答案)那个ok没法用。
回复

使用道具 举报

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

本版积分规则

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