# [CS61A]Lab02 & Quiz01

|只看干货 sky420 | 显示全部楼层 |阅读模式
 本楼： 👍   0% (0) 0% (0)   👎 全局： 👍   98% (92) 1% (1)    👎
学校名称: UCBerkekly 1 4 CS61A 其他

### 注册一亩三分地论坛，查看更多干货！

x

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

http://gaotx.com/blogs/2015/05/10/cs61a-lab02/

quiz01的答案
http://gaotx.com/blogs/2015/05/06/cs61a-quiz01/

New line for lab 2: http://gaotx.com/blogs/2015/05/07/cs61a-lab02/

### 评分 乐百氏开开心心 2015-5-7 17:52:19 | 显示全部楼层
 本楼： 👍   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

### 评分 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

### 评分 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

### 评分 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

### 评分 FFFelix 2015-5-8 20:09:38 | 显示全部楼层
 本楼： 👍   0% (0) 0% (0)   👎 全局： 👍   96% (477) 3% (16)    👎
 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 ***"     k = n     while k > 0:         if n % k == 0:             print(k)         k -= 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 ***"     curr, next = 0, 1     while n > 0:         curr, next = next, curr + next         n -= 1     return curr 复制代码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 += 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 = 1.0     "*** YOUR CODE HERE ***"     if x >= 1:         mul = 2     else:         mul = 0.5     while abs(power_of_two * mul - x) < abs(power_of_two - x):         power_of_two = power_of_two * mul     if abs(power_of_two * 2 - x) == abs(power_of_two - x):             power_of_two = power_of_two * 2     return power_of_two 复制代码感觉难度比homework小挺多的 ### 评分 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

### 评分

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