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

[CS61A]Lab02 & Quiz01

全局:
公开课
学校名称: UCBerkekly
Unit号: 1
开课时间: 4
课程全名: CS61A
平台: 其他

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

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

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/

上一篇:[CS61A]Homework 1
下一篇:[CS61A]Hog
推荐
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
回复

使用道具 举报

推荐
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)
回复

使用道具 举报

推荐
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
回复

使用道具 举报

全局:
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

回复

使用道具 举报

🔗
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


回复

使用道具 举报

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

使用道具 举报

🔗
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

   

回复

使用道具 举报

🔗
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小挺多的
回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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