<
查看: 4067|回复: 25
收起左侧

[CS61A]Lab02 & Quiz01

|只看干货
sky420 | 显示全部楼层 |阅读模式
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   98% (92)
 
 
1% (1)    👎
公开课
学校名称: 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/

评分

参与人数 1大米 +10 收起 理由
AveMaleficum + 10

查看全部评分


上一篇:[CS61A]Homework 1
下一篇:[CS61A]Hog
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (5)
 
 
0% (0)    👎
lab2中的Question1 return x and y > 0是什么意思啊?一般而言,and两边应该都是逻辑表达式呀,可是这里确实只有x。不太明白。
回复

使用道具 举报

 楼主| sky420 2015-5-7 23:29:27 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   98% (92)
 
 
1% (1)    👎
在lab2,question1没有找到你说的expression,但也可以解释。x这里是一个integer,任何integer在control
statement中,除了0是False,其他的都是True。
回复

使用道具 举报

 楼主| sky420 2015-5-7 23:29:40 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   98% (92)
 
 
1% (1)    👎
乐百氏开开心心 发表于 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 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   97% (81)
 
 
2% (2)    👎
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大米 +10 收起 理由
AveMaleficum + 10

查看全部评分

回复

使用道具 举报

rarezhang 2015-5-8 01:24:52 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (156)
 
 
0% (0)    👎
交作业。 虽然我用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大米 +10 收起 理由
AveMaleficum + 10

查看全部评分

回复

使用道具 举报

goldpanda 2015-5-8 11:13:04 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (62)
 
 
0% (0)    👎
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大米 +10 收起 理由
AveMaleficum + 10

查看全部评分

回复

使用道具 举报

geminiiiiiii 2015-5-8 18:05:33 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
"""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大米 +10 收起 理由
AveMaleficum + 10

查看全部评分

回复

使用道具 举报

FFFelix 2015-5-8 20:09:38 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   96% (477)
 
 
3% (16)    👎
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大米 +10 收起 理由
AveMaleficum + 10

查看全部评分

回复

使用道具 举报

jy_121 2015-5-8 20:28:44 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   97% (220)
 
 
2% (5)    👎
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大米 +10 收起 理由
AveMaleficum + 10

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

隐私提醒:
■拉群请前往同学同事飞友|拉群结伴版块,其他版块拉群,帖子会被自动删除
■论坛不能删帖,为防止被骚扰甚至人肉,不要公开留微信等联系方式,请以论坛私信方式发送。
■特定版块可以超级匿名:https://tools.1point3acres.com/thread
■其他版块匿名方法:http://www.1point3acres.com/bbs/thread-405991-1-1.html

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