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

[CS61A]Homework 1

🔗
goldpanda 2015-5-7 10:37:31 | 只看该作者
全局:
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*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 True

def t():
    return 1

def f():
    1/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
    """
    print(int(n))
    if n == 1:
        return 1;
    elif n%2 == 0:
        return hailstone(n/2) + 1
    else:
        return hailstone(n*3+1) + 1
   

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

回复

使用道具 举报

🔗
mjtyumi 2015-5-7 11:59:24 | 只看该作者
全局:
Q1: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)

Q2:
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-pow(min(a,b,c),2)

Q3:
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():
    whatever


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(1)
        return length


回复

使用道具 举报

🔗
rarezhang 2015-5-7 12:28:28 | 只看该作者
全局:
交作业: https://bitbucket.org/rarezhang/ ... 1/hw01.py?at=master

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 ***"
    if a < b and a < c:
        s = b * b + c * c
    elif b < a and b < c:
        s = a * a + c * c
    else:
        s = a * a + b * b
    return s
        
        

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

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

使用道具 举报

🔗
jy_121 2015-5-7 17:52: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*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 True

def t():
    return 1

def f():
    1/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
    """
    print(int n)
    if n == 1:
        return 1;
    elif n%2 == 0:
        return hailstone(n/2) + 1
    else:
        return hailstone(n*3+1) + 1
   

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

使用道具 举报

🔗
 楼主| sky420 2015-5-7 23:47:51 | 只看该作者
全局:
# 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
    """
    if b < 0:
        f = sub
    else:
        f = add
    return f(a, b)
# Q2
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)
# Q3
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():# Either t() or f() is evaluated
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():# All expression are evaluated
    return if_function(c(), t(), f())

def c():
    return False

def t():
    None

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

使用道具 举报

全局:


Q2:
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)
Q3:
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():
        """Formal parameters are blinded to the value of functions,
        it means we need to evaluate these functions first and then
        pass the values to their corresponding positions"""
    return if_function(c(), t(), f())

def c():
    return True

def t():
    return 1

def f():
    return I, 'love you!'

Q4:
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
    """
    if n <= 0 or type(n) != int:
        print("Please enter a positive integer!")
    else:
        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
回复

使用道具 举报

🔗
geminiiiiiii 2015-5-8 10:43:43 | 只看该作者
全局:
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 a*a + b*b + c*c - min(min(a, b), c)*min(a, b, c)
    "*** YOUR CODE HERE ***"

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
    "*** YOUR CODE HERE ***"

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

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

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 = 0
    while n > 1:
        Print(n)
        i = i + 1
        if n % 2 == 0:
            n = n/2
        else n = n*3 +1
    Print(1)
    return i   

challenge_question_program = """
Print''
"*** YOUR CODE HERE ***"
"""

回复

使用道具 举报

🔗
reasonapp 2015-5-8 23:48:59 | 只看该作者
全局:
弄毕设弄到差点忘了due!!!!赶快补交!!

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
    """
    if b < 0:
        f = sub
    else:
        f = add
    return f(a, b)

Q2:
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 + b + c - (max(a, b, c)+ min(a, b, c))) * (a + b + c - (max(a, b, c)+ min(a, b, c))) + max(a, b, c ) * max(a, b, c)

Q3:
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
    "*** YOUR CODE HERE ***"

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

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

Q4:
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
        i = i + 1
        if((n+1) % 2):   
            n = n / 2
        else:
            n = 3 * n + 1
    print n
    return i
   
Q5:
没时间做了。。好难的样子
回复

使用道具 举报

🔗
LuckyBamboo 2015-5-9 11:27:18 | 只看该作者
全局:
赶最后的时间~~~

#Question 1
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)

#Question 2
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*a+b*b, a*a+c*c, b*b+c*c)

print two_of_three(1,2,3)


#Question 3
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

#Question 4
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:
        if n % 2 == 0:           
            n =  n / 2
        else:
            n = n * 3 + 1
        i = i + 1
    return i


#Question 5
challenge_question_program = """
"*** YOUR CODE HERE ***"
a = 'print itself'; print('it' + 'will' + a)
"""


回复

使用道具 举报

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

本版积分规则

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