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

[CS61A]Lab02 & Quiz01

🔗
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

回复

使用道具 举报

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

使用道具 举报

🔗
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




回复

使用道具 举报

🔗
神罗天征 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~~
回复

使用道具 举报

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

使用道具 举报

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

               


回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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