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

[CS61A]Lab02 & Quiz01

🔗
Chris1993 2015-10-24 22:43:46 | 只看该作者
全局:
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


    """
    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
    while abs(numerator/denominator-pi) > gap:
        denominator += 1
        numerator = round(pi*denominator)
  

    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 = 0.0
    if x >= 1:
        while pow(2,power_of_two+1) < x:
            power_of_two += 1
        if pow(2,power_of_two+1) == x:
            return pow(2,power_of_two + 1)
        else:
            if abs(pow(2,power_of_two)-x) < abs(pow(2,power_of_two+1) - x):
                return pow(2,power_of_two)
            else:
                return pow(2,power_of_two + 1)

    if x < 1:
        while pow(0.5,power_of_two+1) > x:
            power_of_two += 1
        if pow(0.5,power_of_two+1) == x:
            return pow(0.5,power_of_two + 1)
        else:
            if abs(pow(0.5,power_of_two) - x) <= abs(pow(0.5,power_of_two+1)-x):
                return pow(0.5,power_of_two)
            else:
                return pow(0.5,power_of_two + 1)
















回复

使用道具 举报

🔗
lh6210 2015-10-27 16:00:25 | 只看该作者
全局:
FFFelix 发表于 2015-5-8 20:09
lab02:quiz01:感觉难度比homework小挺多的

请教一下,如何把代码放在这个蓝色的框框里面?
觉得这样格式可以保存的很好。谢谢!
回复

使用道具 举报

🔗
rockleecsu 2015-12-23 09:07:49 | 只看该作者
全局:
交一下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.     x = n
  28.     while x > 0:
  29.         if n % x == 0:
  30.             print(x)
  31.         x -= 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.     cur, next = 0, 1
  52.     while n > 0:
  53.         cur, next = next, cur + next
  54.         n -= 1
  55.     return cur
复制代码
  1. """Extra questions for lab 2."""

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

  6.     >>> gets_discount(65, 12)
  7.     True
  8.     >>> gets_discount(9, 70)
  9.     True
  10.     >>> gets_discount(40, 45)
  11.     False
  12.     >>> gets_discount(40, 75)
  13.     False
  14.     >>> gets_discount(65, 13)
  15.     False
  16.     >>> gets_discount(7, 9)
  17.     False
  18.     >>> gets_discount(73, 77)
  19.     False
  20.     >>> gets_discount(70, 31)
  21.     False
  22.     >>> gets_discount(10, 25)
  23.     False
  24.     """
  25.     "*** YOUR CODE HERE ***"
  26.     if max(x, y) >= 65 and min(x, y) <= 12:
  27.         return True
  28.     return False


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

  32.     >>> is_factor(3, 6)
  33.     True
  34.     >>> is_factor(4, 10)
  35.     False
  36.     >>> is_factor(0, 5)
  37.     False
  38.     >>> is_factor(0, 0)
  39.     False
  40.     """
  41.     "*** YOUR CODE HERE ***"
  42.     if x == 0:
  43.         return False
  44.     if y == 0:
  45.         return True
  46.     return (y % x == 0)


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

  50.     >>> falling(6, 3)  # 6 * 5 * 4
  51.     120
  52.     >>> falling(4, 0)
  53.     1
  54.     >>> falling(4, 3)  # 4 * 3 * 2
  55.     24
  56.     >>> falling(4, 1)  # 4
  57.     4
  58.     """
  59.     "*** YOUR CODE HERE ***"
  60.     i = 0
  61.     res = 1
  62.     while i < k:
  63.         res *= n
  64.         n -= 1
  65.         i += 1

  66.     return res
复制代码
回复

使用道具 举报

🔗
rockleecsu 2015-12-23 13:36:07 | 只看该作者
全局:
完成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.0 / (1.0 / x + 1.0 / 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 True:
  27.             tmp = numerator / denominator
  28.             if abs(tmp - pi) < gap:
  29.                     break
  30.             if tmp > pi:
  31.                     denominator += 1
  32.             else:
  33.                     numerator += 1
  34.     print(numerator, '/', denominator, '=', numerator/denominator)

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

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


  51.     """
  52.     power_of_two = 1.0
  53.     "*** YOUR CODE HERE ***"
  54.     tmp_res = power_of_two
  55.     if x > 1.0:
  56.             while x > tmp_res:
  57.                     tmp_res *= 2
  58.             if abs(x - tmp_res) > abs(x - tmp_res / 2):
  59.                     power_of_two = tmp_res / 2
  60.             else:
  61.                     power_of_two = tmp_res
  62.     else:
  63.             while x < tmp_res:
  64.                     tmp_res /= 2
  65.             if abs(x - tmp_res) >= abs(x - tmp_res * 2):
  66.                     power_of_two = tmp_res * 2
  67.             else:
  68.                     power_of_two = tmp_res
  69.     return power_of_two
复制代码

quiz01.png (43.72 KB, 下载次数: 0)

quiz01.png
回复

使用道具 举报

🔗
lianyujia 2016-5-5 03:09:03 | 只看该作者
全局:
from operator import add
def double(x):
        return x+x
def square(y):
        return y*y
def f(z):
        return add(square(double(z)), 1)


def fib(n):
        a = 0
        b = 1
        count = 0
        while(n > 0):
                count = a + b
                a = b
                b = count
                n = n - 1
        return a

def factors(n):
        temp = n
        while(n > 0):
                if temp % n == 0:
                        print(n)
                n = n - 1


def both_positive(x, y):
"""
returns True if both x y are positive
"""
return (x > 0) and (y > 0)

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
    while abs(numerator / denominator - pi) > gap:
            denominator = denominator + 1
            numerator = round(pi * denominator)
    print(numerator, '/', denominator, '=', numerator/denominator)


from operator import abs

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
    if x > power_of_two:
            while power_of_two < x:
                    power_of_two = power_of_two * 2
            if abs(power_of_two - x) >= abs(power_of_two / 2 - x):
                    return power_of_two / 2
            return power_of_two
    elif x < power_of_two:
            while power_of_two > x:
                    power_of_two = power_of_two / 2
            if abs(power_of_two - x) >= abs(power_of_two * 2 - x):
                    return power_of_two*2
            return power_of_two
    "*** YOUR CODE HERE ***"
    return power_of_two
那啥  lab02有几个简单知道啥意思就懒得写了 TT
回复

使用道具 举报

全局:
请问在调用lab02.py中的函数时报错,显示“command not found”是什么问题?

截图00.jpg (17.09 KB, 下载次数: 0)

截图00.jpg
回复

使用道具 举报

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

本版积分规则

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