一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 1544|回复: 34
收起左侧

[CS61A]Homework 1

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

[其他]CS61A #1 - 2015-05-04@UCBerkely

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

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

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楼
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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

AaronTiberium 发表于 2015-5-6 23:08:19 | 显示全部楼层
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)
"""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

 楼主| 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()

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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

评分

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)
"""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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

评分

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)
"""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

 楼主| 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)
"""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

头像被屏蔽
coolyuanwei 发表于 2015-5-8 10:05:42 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

medfordwinter 发表于 2015-5-8 10:42:50 | 显示全部楼层


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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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 ***"
"""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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:
没时间做了。。好难的样子

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

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)
"""


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

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

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-11 02:28

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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