一亩三分地论坛

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

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

[CS61A]Lab02 & Quiz01

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

[其他]CS61A #1 - 4@UCBerkekly

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

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

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

Lab02: 对Basic Function和Constrol statements的应用
http://gaotx.com/cs61a/lab/lab02/
Quiz01: 第一个quiz,难度不大。
http://gaotx.com/cs61a/hw/quiz01/





补充内容 (2015-5-8 02:44):
我的lab02的答案
http://gaotx.com/blogs/2015/05/10/cs61a-lab02/

补充内容 (2015-5-8 03:47):
quiz01的答案
http://gaotx.com/blogs/2015/05/06/cs61a-quiz01/

补充内容 (2015-5-8 03:49):
New line for lab 2: http://gaotx.com/blogs/2015/05/07/cs61a-lab02/

评分

1

查看全部评分

乐百氏开开心心 发表于 2015-5-7 17:52:19 | 显示全部楼层
lab2中的Question1 return x and y > 0是什么意思啊?一般而言,and两边应该都是逻辑表达式呀,可是这里确实只有x。不太明白。
回复 支持 反对

使用道具 举报

 楼主| sky420 发表于 2015-5-7 23:29:27 | 显示全部楼层
在lab2,question1没有找到你说的expression,但也可以解释。x这里是一个integer,任何integer在control
statement中,除了0是False,其他的都是True。
回复 支持 反对

使用道具 举报

 楼主| sky420 发表于 2015-5-7 23:29:40 | 显示全部楼层
乐百氏开开心心 发表于 2015-5-7 17:52
lab2中的Question1 return x and y > 0是什么意思啊?一般而言,and两边应该都是逻辑表达式呀,可是这里确 ...


在lab2,question1没有找到你说的expression,但也可以解释。x这里是一个integer,任何integer在control
statement中,除了0是False,其他的都是True。
回复 支持 反对

使用道具 举报

go7going 发表于 2015-5-8 00:48:40 | 显示全部楼层
lab 02:

# Q6
def both_positive(x, y):

    return x > 0 and y > 0


# Q9
def factors(n):
    x = n
    while x > 0:
        if n % x == 0:
            print(x)
        x -= 1

# Q10
def fib(n):
   curr, next = 0, 1
   while n > 0:
        curr, next = next, curr + next
        n -= 1
   return curr
# Q11
def gets_discount(x, y):
    return (x <= 12 and y >= 65) or (x >= 65 and y <= 12)



# Q12
def is_factor(x, y):
   return x != 0 and y % x == 0


# Q13
def falling(n, k):
    total, stop = 1, n-k
    while n > stop:
        total, n = total*n, n-1
    return total

quiz 1:
def harmonic(x, y):
      return 2/(1/x + 1/y)

from math import pi

def pi_fraction(gap):

    numerator, denominator = 3, 1
    while abs(numerator/denominator-pi) > gap:
        denominator = denominator + 1
        numerator = round(pi * denominator)
    print(numerator, '/', denominator, '=', numerator/denominator)


def nearest_two(x):
   power_of_two = 1.0
   if x < 1:
        factor = 0.5
   else:
        factor = 2
   while abs(power_of_two * factor - x) < abs(power_of_two - x):
        power_of_two = power_of_two * factor
   if abs(power_of_two * 2 - x) == abs(power_of_two - x):
        power_of_two = power_of_two * 2
   return power_of_two

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

rarezhang 发表于 2015-5-8 01:24:52 | 显示全部楼层
交作业。 虽然我用python已经蛮久了,还是觉得quiz比较难。平时写一次性的程序写惯了,很少考虑细节。这种作业真是个好锻炼。
lab02:
https://bitbucket.org/rarezhang/ ... /lab02.py?at=master

lab02_extra:
https://bitbucket.org/rarezhang/ ... _extra.py?at=master

quiz01:
https://bitbucket.org/rarezhang/ ... quiz01.py?at=master


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

goldpanda 发表于 2015-5-8 11:13:04 | 显示全部楼层
lab 02
"""Required questions for lab 2"""

## Boolean Operators ##

# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
   
    return (x>0 and y>0)

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    factor = n
    while factor!=0:
        if n % factor == 0:
            print(factor)
        factor -= 1


# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    first = 0
    second = 1
  
    if n == 0:
        return first
    if n == 1:
        return second
    while n!=1:
        final = first + second
        first = second
        second = final
        n -= 1
    return final

quiz 1
"""Required questions for lab 2"""

## Boolean Operators ##

# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
   
    return (x>0 and y>0)

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    factor = n
    while factor!=0:
        if n % factor == 0:
            print(factor)
        factor -= 1


# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    first = 0
    second = 1
  
    if n == 0:
        return first
    if n == 1:
        return second
    while n!=1:
        final = first + second
        first = second
        second = final
        n -= 1
    return final

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

geminiiiiiii 发表于 2015-5-8 18:05:33 | 显示全部楼层
"""Required questions for lab 2"""

## Boolean Operators ##

# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
    "*** YOUR CODE HERE ***"
    return x > 0 and y > 0

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    "*** YOUR CODE HERE ***"
    i = n
    while i > 0:
        if n % i == 0:
            print(i)
        i -= 1
        pass

# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        a, b, i = 0, 1, 2
        while i <= n:
            t = b
            b = a + b
            a = t
            i += 1
        return b   
        pass

quiz01
def harmonic(x, y):
    """Return the harmonic mean of x and y.

    >>> harmonic(2, 6)
    3.0
    >>> harmonic(1, 1)
    1.0
    >>> harmonic(2.5, 7.5)
    3.75


    """
    "*** YOUR CODE HERE ***"
    return 2/(1/x + 1/y)

from math import pi

def pi_fraction(gap):
       numerator, denominator = 3, 1
    while abs(numerator/denominator - pi) > gap:
        denominator += 1
        numerator = round( pi*denominator )
        pass
    print(numerator, '/', denominator, '=', numerator/denominator)

def nearest_two(x):
    power_of_two = 1.0
    if x < 1:
        factor = 0.5
    else:
        factor = 2
    while abs(power_of_two * factor - x) < abs(power_of_two - x):
        power_of_two = power_of_two * factor
    if abs(power_of_two * 2 - x) == abs(power_of_two - x):
        power_of_two = power_of_two * 2
    return power_of_two

   

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

FFFelix 发表于 2015-5-8 20:09:38 | 显示全部楼层
lab02:
  1. """Required questions for lab 2"""

  2. ## Boolean Operators ##

  3. # Q6
  4. def both_positive(x, y):
  5.     """
  6.     Returns True if both x and y are positive.
  7.     >>> both_positive(-1, 1)
  8.     False
  9.     >>> both_positive(1, 1)
  10.     True
  11.     """
  12.     "*** YOUR CODE HERE ***"
  13.     return x > 0 and y > 0

  14. ## while Loops ##

  15. # Q9
  16. def factors(n):
  17.     """Prints out all of the numbers that divide `n` evenly.

  18.     >>> factors(20)
  19.     20
  20.     10
  21.     5
  22.     4
  23.     2
  24.     1
  25.     """
  26.     "*** YOUR CODE HERE ***"
  27.     k = n
  28.     while k > 0:
  29.         if n % k == 0:
  30.             print(k)
  31.         k -= 1

  32. # Q10
  33. def fib(n):
  34.     """Returns the nth Fibonacci number.

  35.     >>> fib(0)
  36.     0
  37.     >>> fib(1)
  38.     1
  39.     >>> fib(2)
  40.     1
  41.     >>> fib(3)
  42.     2
  43.     >>> fib(4)
  44.     3
  45.     >>> fib(5)
  46.     5
  47.     >>> fib(6)
  48.     8
  49.     """
  50.     "*** YOUR CODE HERE ***"
  51.     curr, next = 0, 1
  52.     while n > 0:
  53.         curr, next = next, curr + next
  54.         n -= 1
  55.     return curr
复制代码
quiz01:
  1. def harmonic(x, y):
  2.     """Return the harmonic mean of x and y.

  3.     >>> harmonic(2, 6)
  4.     3.0
  5.     >>> harmonic(1, 1)
  6.     1.0
  7.     >>> harmonic(2.5, 7.5)
  8.     3.75


  9.     """
  10.     "*** YOUR CODE HERE ***"
  11.     return 2/(1/x + 1/y)

  12. from math import pi

  13. def pi_fraction(gap):
  14.     """Print the fraction within gap of pi that has the smallest denominator.

  15.     >>> pi_fraction(0.01)
  16.     22 / 7 = 3.142857142857143
  17.     >>> pi_fraction(1)
  18.     3 / 1 = 3.0
  19.     >>> pi_fraction(1/8)
  20.     13 / 4 = 3.25
  21.     >>> pi_fraction(1e-6)
  22.     355 / 113 = 3.1415929203539825


  23.     """
  24.     numerator, denominator = 3, 1
  25.     "*** YOUR CODE HERE ***"
  26.     while abs(numerator / denominator - pi) > gap:
  27.         denominator += 1
  28.         numerator = round(pi * denominator)
  29.         
  30.     print(numerator, '/', denominator, '=', numerator/denominator)

  31. def nearest_two(x):
  32.     """Return the power of two that is nearest to x.

  33.     >>> nearest_two(8)    # 2 * 2 * 2 is 8
  34.     8.0
  35.     >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
  36.     8.0
  37.     >>> nearest_two(14)   # 14 is closer to 16 than 8
  38.     16.0
  39.     >>> nearest_two(2015)
  40.     2048.0
  41.     >>> nearest_two(.1)
  42.     0.125
  43.     >>> nearest_two(0.75) # Tie between 1/2 and 1
  44.     1.0
  45.     >>> nearest_two(1.5)  # Tie between 1 and 2
  46.     2.0


  47.     """
  48.     power_of_two = 1.0
  49.     "*** YOUR CODE HERE ***"
  50.     if x >= 1:
  51.         mul = 2
  52.     else:
  53.         mul = 0.5
  54.     while abs(power_of_two * mul - x) < abs(power_of_two - x):
  55.         power_of_two = power_of_two * mul
  56.     if abs(power_of_two * 2 - x) == abs(power_of_two - x):
  57.             power_of_two = power_of_two * 2
  58.     return power_of_two
复制代码
感觉难度比homework小挺多的

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

jy_121 发表于 2015-5-8 20:28:44 | 显示全部楼层
LAB02:

"""Required questions for lab 2"""
def welcome():
    print('welcome to')
    return 'hello'

def cs61a():
    print('cs61a')
    return 'world'
   
print(welcome(),cs61a())

## Boolean Operators ##

# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
    "*** YOUR CODE HERE ***"
    return x > 0 and y > 0

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    "*** YOUR CODE HERE ***"
    i = n
    while i >= 1:
        if n % i == 0:
            print(i)
        i -= 1

# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    first, second = 0,1
    while n != 0:
        first, second = second, first+second
        n -= 1
    return first



QUIZ01:

Question 1

def harmonic(x, y):
    """Return the harmonic mean of x and y.

    >>> harmonic(2, 6)
    3.0
    >>> harmonic(1, 1)
    1.0
    >>> harmonic(2.5, 7.5)
    3.75


    "*** YOUR CODE HERE ***"
    return 2/(1/x + 1/y)




Question 2

from math import pi

def pi_fraction(gap):
    """Print the fraction within gap of pi that has the smallest denominator.

    >>> pi_fraction(0.01)
    22 / 7 = 3.142857142857143
    >>> pi_fraction(1)
    3 / 1 = 3.0
    >>> pi_fraction(1/8)
    13 / 4 = 3.25
    >>> pi_fraction(1e-6)
    355 / 113 = 3.1415929203539825


    """
    numerator, denominator = 3, 1
    "*** YOUR CODE HERE ***"
     while abs(numerator / denominator - pi) > gap:
        denominator += 1
        numerator = round(denominator * pi)        
    print(numerator, '/', denominator, '=', numerator/denominator)




Question 3

def nearest_two(x):
    """Return the power of two that is nearest to x.

    >>> nearest_two(8)    # 2 * 2 * 2 is 8
    8.0
    >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
    8.0
    >>> nearest_two(14)   # 14 is closer to 16 than 8
    16.0
    >>> nearest_two(2015)
    2048.0
    >>> nearest_two(.1)
    0.125
    >>> nearest_two(0.75) # Tie between 1/2 and 1
    1.0
    >>> nearest_two(1.5)  # Tie between 1 and 2
    2.0


    """
    power_of_two = 1.0
    "*** YOUR CODE HERE ***"
      if x > 1:
        power = 2
    else:
        power = 0.5
    while abs(power_of_two * power - x) < abs(power_of_two - x):  
        power_of_two *= power

    if abs(power_of_two * 2 - x ) == abs(power_of_two - x):
        power_of_two = power_of_two * 2

    return power_of_two

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

karte_polo 发表于 2015-5-9 15:13:05 | 显示全部楼层
# Q6
def both_positive(x, y):

    return x > 0 and y > 0

## while Loops ##

# Q9
def factors(n):

    i = n
    while (i>0):
        if n%i==0:
            print(i)
        i-=1

# Q10
def fib(n):

    if n==0:
        return 0
    elif n==1:
        return 1
    else:
        return fib(n-1)+fib(n-2)

# Q11
def gets_discount(x,y):
    return x>=65 and y<=12 or x<=12 and y>=65

# Q12
def is_factor(x,y):
    return not (x==0 or y==0) and (x%y==0 or y%x==0)
        
#Q12

def falling(n,k):
    result,i = n,1
    if k ==0:
        result = 1
    while(i<k):
        result*=n-i
        i+=1
    return result

Quiz

Question 1
def harmonic(x, y):

    return 2.0*x*y/(x+y)

from math import pi

Question 2
def pi_fraction(gap):

    numerator, denominator = 3, 1
    while (abs(numerator/denominator-pi)>gap):
        numerator = numerator+1
        if ((numerator/denominator-pi)>gap):
            denominator = denominator+1
            numerator = 3*denominator
    print(numerator, '/', denominator, '=', numerator/denominator)

Question 3
def nearest_two(x):
    power_of_two = 1.0
    c = True
    while(c):
        if (abs(power_of_two-x)>=abs(2*power_of_two-x)):
            power_of_two *=2
        elif (abs(power_of_two-x)>abs(power_of_two/2-x)):
            power_of_two /=2
        else:
            c =False
    return power_of_two

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

BigCong 发表于 2015-5-9 21:00:12 | 显示全部楼层
quiz01
def harmonic(x, y):
    """Return the harmonic mean of x and y.

    >>> harmonic(2, 6)
    3.0
    >>> harmonic(1, 1)
    1.0
    >>> harmonic(2.5, 7.5)
    3.75

    """
    "*** YOUR CODE HERE ***"
    return 2/(1/x+1/y)

from math import pi

def pi_fraction(gap):
    """Print the fraction within gap of pi that has the smallest denominator.

    >>> pi_fraction(0.01)
    22 / 7 = 3.142857142857143
    >>> pi_fraction(1)
    3 / 1 = 3.0
    >>> pi_fraction(1/8)
    13 / 4 = 3.25
    >>> pi_fraction(1e-6)
    355 / 113 = 3.1415929203539825


    """
    numerator, denominator = 3, 1
    "*** YOUR CODE HERE ***"
    num=numerator/denominator
    dif = abs(num - pi)
    while dif>gap:
        numerator = numerator + 1
        num=numerator/denominator
        dif = abs(num - pi)
        if dif>1.42:
            denominator = denominator + 1
            numerator = denominator*2 - 1
    print(numerator, '/', denominator, '=', numerator/denominator)

def nearest_two(x):
    """Return the power of two that is nearest to x.

    >>> nearest_two(8)    # 2 * 2 * 2 is 8
    8.0
    >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
    8.0
    >>> nearest_two(14)   # 14 is closer to 16 than 8
    16.0
    >>> nearest_two(2015)
    2048.0
    >>> nearest_two(.1)
    0.125
    >>> nearest_two(0.75) # Tie between 1/2 and 1
    1.0
    >>> nearest_two(1.5)  # Tie between 1 and 2
    2.0


    """
    power_of_two = 1.0
    "*** YOUR CODE HERE ***"
    if x < 1:
        factor = 0.5
    else:
        factor = 2
    while abs(power_of_two * factor - x) < abs(power_of_two - x):
        power_of_two = power_of_two * factor
    if abs(power_of_two * 2 - x) == abs(power_of_two - x):
        power_of_two = power_of_two * 2
    return power_of_two

lab02
# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
    "*** YOUR CODE HERE ***"
    return x > 0 and y > 0

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    "*** YOUR CODE HERE ***"
    x = n
    while x > 0:
        if n % x == 0:
            print(x)
        x -= 1

# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    fibo_next = 1
    fibo = 0
    while n>0:
        n -= 1
        fibo_next, fibo = fibo + fibo_next, fibo_next
    print(fibo)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

dreamvsfact 发表于 2015-5-9 23:45:51 | 显示全部楼层
发一个时间复杂度大点的斐波拉契:

def fib(n):
    """Returns the nth Fibbonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
   
    if n == 0 or n == 1:
        return n
    else:
        return fib(n-1) + fib(n-2)

lab2 extra:

def gets_discount(x, y):
    """ Returns True if this is a combination of a senior citizen
    and a child, False otherwise.

    >>> gets_discount(65, 12)
    True
    >>> gets_discount(9, 70)
    True
    >>> gets_discount(40, 45)
    False
    >>> gets_discount(40, 75)
    False
    >>> gets_discount(65, 13)
    False
    >>> gets_discount(7, 9)
    False
    >>> gets_discount(73, 77)
    False
    >>> gets_discount(70, 31)
    False
    >>> gets_discount(10, 25)
    False
    """
    return (x >= 65 and y <=12) or (x <= 12 and y >= 65)



def is_factor(x, y):
    """ Returns True if x is a factor of y, False otherwise.

    >>> is_factor(3, 6)
    True
    >>> is_factor(4, 10)
    False
    >>> is_factor(0, 5)
    False
    >>> is_factor(0, 0)
    False
    """

    return x != 0 and y % x == 0


def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    """
    total = 1
    while k > 0:
        total = n * total
        n -= 1
        k -= 1
    return total


我的quiz1 的第一和第二题都做得和原答案一样,第三题不一样,所以发第三题的自己的答案:

q3:
ef nearest_two(x):
    """Return the power of two that is nearest to x.

    >>> nearest_two(8)    # 2 * 2 * 2 is 8
    8.0
    >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
    8.0
    >>> nearest_two(14)   # 14 is closer to 16 than 8
    16.0
    >>> nearest_two(2015)
    2048.0
    >>> nearest_two(.1)
    0.125
    >>> nearest_two(0.75) # Tie between 1/2 and 1
    1.0
    >>> nearest_two(1.5)  # Tie between 1 and 2
    2.0


    """
    power_of_two = 1.0

    if x < 1:
        while power_of_two > x:
            power_of_two = power_of_two / 2
        
        if x - power_of_two > power_of_two * 2 - x:
            return power_of_two * 2
        elif x - power_of_two < power_of_two * 2 - x:
            return power_of_two
        else:
            return max(power_of_two, power_of_two * 2)

    elif x > 1:
        while power_of_two < x:
            power_of_two = power_of_two * 2
        
        if power_of_two - x > x - power_of_two / 2:
            return power_of_two / 2
        elif power_of_two - x < x - power_of_two / 2:
            return power_of_two
        else:
            return max(power_of_two, power_of_two / 2)

    else:
        return 1




评分

1

查看全部评分

回复 支持 反对

使用道具 举报

神罗天征 发表于 2015-5-11 10:17:31 | 显示全部楼层
  1. def harmonic(x, y):
  2.     """Return the harmonic mean of x and y.

  3.     >>> harmonic(2, 6)
  4.     3.0
  5.     >>> harmonic(1, 1)
  6.     1.0
  7.     >>> harmonic(2.5, 7.5)
  8.     3.75


  9.     """
  10.     "*** YOUR CODE HERE ***"
  11.         return 2/(1/x+1/y)

  12. from math import pi

  13. def pi_fraction(gap):
  14.     """Print the fraction within gap of pi that has the smallest denominator.

  15.     >>> pi_fraction(0.01)
  16.     22 / 7 = 3.142857142857143
  17.     >>> pi_fraction(1)
  18.     3 / 1 = 3.0
  19.     >>> pi_fraction(1/8)
  20.     13 / 4 = 3.25
  21.     >>> pi_fraction(1e-6)
  22.     355 / 113 = 3.1415929203539825


  23.     """
  24.     numerator, denominator = 3, 1
  25.     "*** YOUR CODE HERE ***"
  26.     while (abs(numerator/denominator-pi)>gap):
  27.         denominator=denominator+1
  28.         numerator=round(denominator*pi)
  29.     print(numerator, '/', denominator, '=', numerator/denominator)

  30. def nearest_two(x):
  31.     """Return the power of two that is nearest to x.

  32.     >>> nearest_two(8)    # 2 * 2 * 2 is 8
  33.     8.0
  34.     >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
  35.     8.0
  36.     >>> nearest_two(14)   # 14 is closer to 16 than 8
  37.     16.0
  38.     >>> nearest_two(2015)
  39.     2048.0
  40.     >>> nearest_two(.1)
  41.     0.125
  42.     >>> nearest_two(0.75) # Tie between 1/2 and 1
  43.     1.0
  44.     >>> nearest_two(1.5)  # Tie between 1 and 2
  45.     2.0


  46.     """
  47.     power_of_two = 1.0
  48.     "*** YOUR CODE HERE ***"
  49.     i = 0
  50.     if (x>=1):
  51.         while(x>pow(2,i)):
  52.             i=i+1
  53.         a,b = x-pow(2,i-1),pow(2,i)-x
  54.         if(a>=b):
  55.             power_of_two = pow(2,i)
  56.         else:
  57.             power_of_two = pow(2,i-1)
  58.     else:
  59.         while(x<pow(2,i)):
  60.             i=i-1
  61.         a,b = x-pow(2,i),pow(2,i+1)-x
  62.         if(a>=b):
  63.             power_of_two = pow(2,i+1)
  64.         else:
  65.             power_of_two = pow(2,i)
  66.     return power_of_two
复制代码
quiz~~

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

guangstick 发表于 2015-5-17 08:24:27 | 显示全部楼层
# Question 9
def factors(n):
        """print out all the number that divide 'n' evenly.

        >>> factors(20)
        20
        10
        5
        4
        2
        1
        """
        OriNum = n
        while n>0:
                if OriNum%n == 0:
                        print(n)
                n-=1

# Question 10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    last1, last2=1, 0
    while n>0:
            last2, last1 = last1, last1+last2
            n-=1
    print(last2)

# Question 11
def gets_discount(x, y):
    """ Returns True if this is a combination of a senior citizen
    and a child, False otherwise.

    >>> gets_discount(65, 12)
    True
    >>> gets_discount(9, 70)
    True
    >>> gets_discount(40, 45)
    False
    >>> gets_discount(40, 75)
    False
    >>> gets_discount(65, 13)
    False
    >>> gets_discount(7, 9)
    False
    >>> gets_discount(73, 77)
    False
    >>> gets_discount(70, 31)
    False
    >>> gets_discount(10, 25)
    False
    """
    "*** YOUR CODE HERE ***"
    return (x >= 65 and y <= 12) or \
    (y >= 65 and x <= 12)
# Question 12
def is_factor(x, y):
    """ Returns True if x is a factor of y, False otherwise.

    >>> is_factor(3, 6)
    True
    >>> is_factor(4, 10)
    False
    >>> is_factor(0, 5)
    False
    >>> is_factor(0, 0)
    False
    >>> is_factor(5, 0)
    True
    """
    "*** YOUR CODE HERE ***"
    return x!=0 and not y%x

# Question 13
def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    """
    "*** YOUR CODE HERE ***"
    Outcome = 1
    while k>0:
        Outcome*= n
        n-= 1
        k-= 1
    print(Outcome)

#quiz1
def harmonic(x, y):
    """Return the harmonic mean of x and y.

    >>> harmonic(2, 6)
    3.0
    >>> harmonic(1, 1)
    1.0
    >>> harmonic(2.5, 7.5)
    3.75


    """
    "*** YOUR CODE HERE ***"
    return 2/(1/x+1/y)

#quiz2
from math import pi

def pi_fraction(gap):
    """Print the fraction within gap of pi that has the smallest denominator.

    >>> pi_fraction(0.01)
    22 / 7 = 3.142857142857143
    >>> pi_fraction(1)
    3 / 1 = 3.0
    >>> pi_fraction(1/8)
    13 / 4 = 3.25
    >>> pi_fraction(1e-6)
    355 / 113 = 3.1415929203539825


    """
    numerator, denominator = 3, 1
    "*** YOUR CODE HERE ***"
    "*** YOUR CODE HERE ***"
    while True:
        numerator = round(denominator * pi)
        if abs(numerator/denominator-pi) < gap:
            print(numerator, '/', denominator, '=', \
                numerator/denominator)
            return
        denominator+=1               
#quiz3

def nearest_two(x):
    """Return the power of two that is nearest to x.

    >>> nearest_two(8)    # 2 * 2 * 2 is 8
    8.0
    >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
    8.0
    >>> nearest_two(14)   # 14 is closer to 16 than 8
    16.0
    >>> nearest_two(2015)
    2048.0
    >>> nearest_two(.1)
    0.125
    >>> nearest_two(0.75) # Tie between 1/2 and 1
    1.0
    >>> nearest_two(1.5)  # Tie between 1 and 2
    2.0


    """
    power_of_two = 1.0
    "*** YOUR CODE HERE ***"
    def nearest(x,power_of_two, power_of_two_last):
        if abs(power_of_two-x)>abs(x-power_of_two_last):
            return power_of_two_last
        elif abs(power_of_two-x)==abs(x-power_of_two_last):
            return max(power_of_two,power_of_two_last)
        else:
            return power_of_two

    if x>=1:
        while power_of_two <= x:
            power_of_two*= 2
        return nearest(x,power_of_two,power_of_two/2)

    else:
        while power_of_two >= x:
            power_of_two/= 2
        return nearest(x,power_of_two,power_of_two*2)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

tl0710 发表于 2015-5-19 21:27:23 | 显示全部楼层
quiz1的代码
def harmonic(x,y):
        return 2/(1/x+1/y)
print(harmonic(2,6))
from math import pi

def pi_fraction(gap):
        numerator,denominator=3,1
        while abs(numerator/denominator-pi)>gap:
                if numerator/denominator-pi>0:
                        denominator+=1
                else :
                        numerator+=1
        """return numerator,denominator"""
        print(numerator, '/', denominator, '=', numerator/denominator)
pi_fraction(0.0000001)

def nearest_two(x):
        power_of_two=1.0
        if x>1:
                while x-power_of_two>0:
                        power_of_two*=2
                if power_of_two-x>x-power_of_two/2>0:
                        power_of_two/=2
                else:
                        power_of_two
        else:
                while x-power_of_two<0:
                        power_of_two/=2
                if x-power_of_two>power_of_two*2-x>0:
                        power_of_two*=2
                else:
                        power_of_two
        return power_of_two
print(nearest_two(0.8))

               


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-21 08:05:53 | 显示全部楼层
quiz01:

def harmonic(x, y):
    """Return the harmonic mean of x and y.

    >>> harmonic(2, 6)
    3.0
    >>> harmonic(1, 1)
    1.0
    >>> harmonic(2.5, 7.5)
    3.75


    """
    "*** YOUR CODE HERE ***"
    return 2 / (1 / x + 1 / y)
from math import pi

def pi_fraction(gap):
    """Print the fraction within gap of pi that has the smallest denominator.

    >>> pi_fraction(0.01)
    22 / 7 = 3.142857142857143
    >>> pi_fraction(1)
    3 / 1 = 3.0
    >>> pi_fraction(1/8)
    13 / 4 = 3.25
    >>> pi_fraction(1e-6)
    355 / 113 = 3.1415929203539825


    """
    numerator, denominator = 3, 1
    "*** YOUR CODE HERE ***"
    while(abs(numerator / denominator - pi ) > gap):
        denominator = denominator + 1
        numerator = round(denominator * pi)
    print(numerator, '/', denominator, '=', numerator/denominator)

def nearest_two(x):
    """Return the power of two that is nearest to x.

    >>> nearest_two(8)    # 2 * 2 * 2 is 8
    8.0
    >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
    8.0
    >>> nearest_two(14)   # 14 is closer to 16 than 8
    16.0
    >>> nearest_two(2015)
    2048.0
    >>> nearest_two(.1)
    0.125
    >>> nearest_two(0.75) # Tie between 1/2 and 1
    1.0
    >>> nearest_two(1.5)  # Tie between 1 and 2
    2.0


    """
    power_of_two = 1.0
    "*** YOUR CODE HERE ***"
    if x > 1:
        while abs(x - power_of_two) > abs(x - power_of_two * 2):
            power_of_two *= 2
    else:
        while abs(x - power_of_two) > abs(x -  power_of_two * 0.5):
            power_of_two *= 0.5
    if abs(x - power_of_two) == abs(x - power_of_two * 2):
        power_of_two *= 2
    return power_of_two

Lab02:
"""Required questions for lab 2"""

## Boolean Operators ##

# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
    "*** YOUR CODE HERE ***"
    return x > 0 and y > 0

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    "*** YOUR CODE HERE ***"
    i = n
    while (i <= n) and (i > 0):
        if n % i == 0:
            print(i)
        i = i -1

# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    i = 0
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        while i <= n:
            i += 1
            return fib(n - 2) + fib(n - 1)

Lab02 extra:
"""Extra questions for lab 2."""

# Q11
def gets_discount(x, y):
    """ Returns True if this is a combination of a senior citizen
    and a child, False otherwise.

    >>> gets_discount(65, 12)
    True
    >>> gets_discount(9, 70)
    True
    >>> gets_discount(40, 45)
    False
    >>> gets_discount(40, 75)
    False
    >>> gets_discount(65, 13)
    False
    >>> gets_discount(7, 9)
    False
    >>> gets_discount(73, 77)
    False
    >>> gets_discount(70, 31)
    False
    >>> gets_discount(10, 25)
    False
    """
    "*** YOUR CODE HERE ***"
    while (x >= 65 and y <= 12) or (x <= 12 and y >= 65):
        return True
    return False

# Q12
def is_factor(x, y):
    """ Returns True if x is a factor of y, False otherwise.

    >>> is_factor(3, 6)
    True
    >>> is_factor(4, 10)
    False
    >>> is_factor(0, 5)
    False
    >>> is_factor(0, 0)
    False
    """
    "*** YOUR CODE HERE ***"
    """while x == 0:
        return False
    while x != 0 and y % x == 0:
        return True"""
    return x != 0 and y % x == 0


# Q13
def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    """
    "*** YOUR CODE HERE ***"
    fall, i = n, 1
    if k == 0:
        return 1
    else:
        while i < k:
            fall = fall * (n - i)
            i += 1
        return fall

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-21 08:08:34 | 显示全部楼层
迟交作业了
再问一个很傻的问题, 在Lab02的第五个问题:
Question 5: What Would Python Print?

>>> True and 1 / 0 and False
ZeroDivisionError

>>> True or 1 / 0 or False
True

>>> True and 0
0

>>> False or 1
1

>>> 1 and 3 and 6 and 10 and 15
15

>>> 0 or False or 2 or 1 / 0
2

最后两个输出为什么不是True and False 而是15 和2 呢
是不是我看漏了哪个知识点 感觉想不明白
回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-5-26 22:51:12 | 显示全部楼层
Lab02:
Q6:
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
    "*** YOUR CODE HERE ***"
    if x < 0:
        x = False
    return x and y > 0

Q9:
# -*- coding: utf-8 -*-
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    "*** YOUR CODE HERE ***"
    i = n
    while i > 0:
        if n % i == 0:
            print(i)
            i -= 1
        elif n % i != 0:
            i -= 1
    return

Q10:
# -*- coding: utf-8 -*-
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    n1, n2 = 0, 1
    i = 0
    while i < n:
         n3 = n1 + n2
         n1 = n2
         n2 = n3
         i += 1
    return n1

Q11:
# -*- coding: utf-8 -*-
def gets_discount(x, y):
    """ Returns True if this is a combination of a senior citizen
    and a child, False otherwise.

    >>> gets_discount(65, 12)
    True
    >>> gets_discount(9, 70)
    True
    >>> gets_discount(40, 45)
    False
    >>> gets_discount(40, 75)
    False
    >>> gets_discount(65, 13)
    False
    >>> gets_discount(7, 9)
    False
    >>> gets_discount(73, 77)
    False
    >>> gets_discount(70, 31)
    False
    >>> gets_discount(10, 25)
    False
    """
    "*** YOUR CODE HERE ***"
    if x >= 65:
        if y <= 12:
            return True
        else:
            print("False")   
    elif x <= 12:
        if y >= 65:
            return True
        else:
            print("False")
    else:
        print("False")

Q12:
# -*- coding: utf-8 -*-
def is_factor(x, y):
    """ Returns True if x is a factor of y, False otherwise.

    >>> is_factor(3, 6)
    True
    >>> is_factor(4, 10)
    False
    >>> is_factor(0, 5)
    False
    >>> is_factor(0, 0)
    False
    """
    "*** YOUR CODE HERE ***"
    if x == 0:
        return False   
    elif y == 0:
        return True
   
    if y % x == 0:
        return True
    else:
        return False
      
Q13:
# -*- coding: utf-8 -*-
def is_factor(x, y):
    """ Returns True if x is a factor of y, False otherwise.

    >>> is_factor(3, 6)
    True
    >>> is_factor(4, 10)
    False
    >>> is_factor(0, 5)
    False
    >>> is_factor(0, 0)
    False
    """
    "*** YOUR CODE HERE ***"
    if x == 0:
        return False   
    elif y == 0:
        return True
   
    if y % x == 0:
        return True
    else:
        return False
        
def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    """
    "*** YOUR CODE HERE ***"
    total = n
    if k == 0:
        return 1
    else:
        while(k >1):
            total = total * (n-1)
            n = n-1
            k -= 1
        return total

被毕业和论文拖到现在才开始跟,希望还能有大米,马上开始Quiz!

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

Liaeve 发表于 2015-10-20 21:22:50 | 显示全部楼层
"""Required questions for lab 2"""

## Boolean Operators ##

# Q6
def both_positive(x, y):
    """
    Returns True if both x and y are positive.
    >>> both_positive(-1, 1)
    False
    >>> both_positive(1, 1)
    True
    """
    "*** YOUR CODE HERE ***"
    return (x > 0) and (y > 0)

## while Loops ##

# Q9
def factors(n):
    """Prints out all of the numbers that divide `n` evenly.

    >>> factors(20)
    20
    10
    5
    4
    2
    1
    """
    "*** YOUR CODE HERE ***"
    a = n
    while a >= 1:
        if 20 % a == 0:
            print(a)
        a -= 1

# Q10
def fib(n):
    """Returns the nth Fibonacci number.

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(2)
    1
    >>> fib(3)
    2
    >>> fib(4)
    3
    >>> fib(5)
    5
    >>> fib(6)
    8
    """
    "*** YOUR CODE HERE ***"
    p , q , i = 0 , 1 , n
    while(i > 0 ):
        p , q = q , p + q
        i -= 1
    return p

"""Extra questions for lab 2."""

# Q11
def gets_discount(x, y):
    """ Returns True if this is a combination of a senior citizen
    and a child, False otherwise.

    >>> gets_discount(65, 12)
    True
    >>> gets_discount(9, 70)
    True
    >>> gets_discount(40, 45)
    False
    >>> gets_discount(40, 75)
    False
    >>> gets_discount(65, 13)
    False
    >>> gets_discount(7, 9)
    False
    >>> gets_discount(73, 77)
    False
    >>> gets_discount(70, 31)
    False
    >>> gets_discount(10, 25)
    False
    """
    "*** YOUR CODE HERE ***"
    return (x < 13 and y > 64) or (x > 64 and y < 13)

# Q12
def is_factor(x, y):
    """ Returns True if x is a factor of y, False otherwise.

    >>> is_factor(3, 6)
    True
    >>> is_factor(4, 10)
    False
    >>> is_factor(0, 5)
    False
    >>> is_factor(0, 0)
    False
    """
    "*** YOUR CODE HERE ***"
    return y % x == 0 and x != 0


# Q13
def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    """
    "*** YOUR CODE HERE ***"
    if n == 0:
        prod = 1
    else:
        prod = 1
        while k > 0:
            k -= 1
            prod = prod * (n - k)
    return prod
   
quiz01

def harmonic(x, y):
    """Return the harmonic mean of x and y.

    >>> harmonic(2, 6)
    3.0
    >>> harmonic(1, 1)
    1.0
    >>> harmonic(2.5, 7.5)
    3.75


    """
    "*** YOUR CODE HERE ***"
    a = (1 / x) + (1 / y)
    sum = 2 / a
    return sum

from math import pi

def pi_fraction(gap):
    """Print the fraction within gap of pi that has the smallest denominator.

    >>> pi_fraction(0.01)
    22 / 7 = 3.142857142857143
    >>> pi_fraction(1)
    3 / 1 = 3.0
    >>> pi_fraction(1/8)
    13 / 4 = 3.25
    >>> pi_fraction(1e-6)
    355 / 113 = 3.1415929203539825


    """
    numerator, denominator = 3, 1
    "*** YOUR CODE HERE ***"

    while abs(numerator / denominator - pi) > gap:
        denominator = denominator + 1
        x = denominator * pi
        numerator = round(x)
    print(numerator, '/', denominator, '=', numerator/denominator)

def nearest_two(x):
    """Return the power of two that is nearest to x.

    >>> nearest_two(8)    # 2 * 2 * 2 is 8
    8.0
    >>> nearest_two(11.5) # 11.5 is closer to 8 than 16
    8.0
    >>> nearest_two(14)   # 14 is closer to 16 than 8
    16.0
    >>> nearest_two(2015)
    2048.0
    >>> nearest_two(.1)
    0.125
    >>> nearest_two(0.75) # Tie between 1/2 and 1
    1.0
    >>> nearest_two(1.5)  # Tie between 1 and 2
    2.0


    """
    power_of_two = 1.0
    "*** YOUR CODE HERE ***"
    if x < 1:
        m = 1 / x
    else:
        m = x

    while power_of_two < m:
        power_of_two *= 2

    if power_of_two != m:
        if power_of_two - m > m - power_of_two / 2 :
            power_of_two = power_of_two / 2

    if x < 1:
        power_of_two = 1 / power_of_two

    return power_of_two
回复 支持 反对

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2016-12-6 01:10

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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