📣 VIP通行证夏日特惠 限时立减$68
楼主: lilirr
跳转到指定楼层
上一主题 下一主题
收起左侧

专门开一个帖分享cs61a的答案

 
🔗
RayHL 2019-7-16 06:13:54 | 只看该作者
全局:
请问楼主每次使用ok test之后都要我登陆账号,有解决方法嘛
回复

使用道具 举报

🔗
 楼主| lilirr 2019-7-16 11:39:54 | 只看该作者
全局:
RayHL 发表于 2019-7-16 06:13
请问楼主每次使用ok test之后都要我登陆账号,有解决方法嘛

在最后加--local
回复

使用道具 举报

🔗
 楼主| lilirr 2019-7-17 10:12:48 | 只看该作者
全局:
lab03_extra

  1. " Optional problems for Lab 3 """

  2. from lab03 import *

  3. ## Higher order functions

  4. def cycle(f1, f2, f3):
  5.     """Returns a function that is itself a higher-order function.

  6.     >>> def add1(x):
  7.     ...     return x + 1
  8.     >>> def times2(x):
  9.     ...     return x * 2
  10.     >>> def add3(x):
  11.     ...     return x + 3
  12.     >>> my_cycle = cycle(add1, times2, add3)
  13.     >>> identity = my_cycle(0)
  14.     >>> identity(5)
  15.     5
  16.     >>> add_one_then_double = my_cycle(2)
  17.     >>> add_one_then_double(1)
  18.     4
  19.     >>> do_all_functions = my_cycle(3)
  20.     >>> do_all_functions(2)
  21.     9
  22.     >>> do_more_than_a_cycle = my_cycle(4)
  23.     >>> do_more_than_a_cycle(2)
  24.     10
  25.     >>> do_two_cycles = my_cycle(6)
  26.     >>> do_two_cycles(1)
  27.     19
  28.     """
  29.     "*** YOUR CODE HERE ***"
  30.     def take_arg_n(n):
  31.         def take_arg_x(x):
  32.             sum_res,i = x,0
  33.             ori_ls = [f1,f2,f3]
  34.             func_ls = ori_ls*(n//3) + ori_ls[0:n%3]
  35.             if n == 0:
  36.                 return x
  37.             else:
  38.                 while i<n:
  39.                     sum_res = func_ls[i](sum_res)
  40.                     i +=1
  41.                 return sum_res
  42.         return take_arg_x
  43.     return take_arg_n
  44. ## Lambda expressions

  45. def is_palindrome(n):
  46.     """
  47.     Fill in the blanks '_____' to check if a number
  48.     is a palindrome.

  49.     >>> is_palindrome(12321)
  50.     True
  51.     >>> is_palindrome(42)
  52.     False
  53.     >>> is_palindrome(2015)
  54.     False
  55.     >>> is_palindrome(55)
  56.     True
  57.     """
  58.     x, y = n, 0
  59.     f = lambda: y*10+x%10
  60.     while x > 0:
  61.         x, y = x//10, f()
  62.     return y == n

  63. ## More recursion practice

  64. def skip_mul(n):
  65.     """Return the product of n * (n - 2) * (n - 4) * ...

  66.     >>> skip_mul(5) # 5 * 3 * 1
  67.     15
  68.     >>> skip_mul(8) # 8 * 6 * 4 * 2
  69.     384
  70.     """
  71.     if n == 2:
  72.         return 2
  73.     elif n == 1:
  74.         return 1
  75.     else:
  76.         return n * skip_mul(n - 2)

  77. def is_prime(n):
  78.     """Returns True if n is a prime number and False otherwise.

  79.     >>> is_prime(2)
  80.     True
  81.     >>> is_prime(16)
  82.     False
  83.     >>> is_prime(521)
  84.     True
  85.     """
  86.     "*** YOUR CODE HERE ***"
  87.     m = 1
  88.     def div_by_divider(m):
  89.         if n%m == 0:
  90.             if m==n:
  91.                 return True
  92.             else:
  93.                 return False
  94.         else:
  95.             return div_by_divider(m+1)
  96.     return div_by_divider(m+1)

  97. def interleaved_sum(n, odd_term, even_term):
  98.     """Compute the sum odd_term(1) + even_term(2) + odd_term(3) + ..., up
  99.     to n.

  100.     >>> # 1 + 2^2 + 3 + 4^2 + 5
  101.     ... interleaved_sum(5, lambda x: x, lambda x: x*x)
  102.     29
  103.     """
  104.     "*** YOUR CODE HERE ***"
  105.     def apply_odd_term(m):
  106.         if m == n:
  107.             return odd_term(m)
  108.         else:
  109.             return odd_term(m)+apply_even_term(m+1)

  110.     def apply_even_term(m):
  111.         if m == n:
  112.             return even_term(m)
  113.         return even_term(m) + apply_odd_term(m+1)
  114.    
  115.     def odd_then_even(n,od,ev):
  116.         if n == 0:
  117.             return 0
  118.         else:
  119.             return apply_odd_term(1)
  120.         
  121.     return odd_then_even(n,odd_term,even_term)

  122.    
  123. def ten_pairs(n):
  124.     """Return the number of ten-pairs within positive integer n.

  125.     >>> ten_pairs(7823952)
  126.     3
  127.     >>> ten_pairs(55055)
  128.     6
  129.     >>> ten_pairs(9641469)
  130.     6
  131.     """
  132.     "*** YOUR CODE HERE ***"
  133.     '''def count_appearance(x,n):#返回单个数字出现的次数
  134.         while n:
  135.             if n%10 == x:
  136.                 return 1+ count_appearance(x,n//10)
  137.             else:
  138.                 return count_appearance(x,n//10)
  139.         return 0

  140.     def is_ten_pairs(x,n):#返回是否能够配对成10
  141.         while n:
  142.             if x+n%10 == 10:#只能返回一个pair,并且没有考虑重复
  143.                 return True
  144.             else:
  145.                 return is_ten_pairs(x,n//10)
  146.         return False
  147.     def appeared(t):'''
  148.     def return_tp(last,all_but_last):
  149.         while last or n:
  150.             while all_but_last:
  151.                 if last + all_but_last%10 ==10:
  152.                     return 1+return_tp(last,all_but_last//10)
  153.                 else:
  154.                     return return_tp(last,all_but_last//10)
  155.             return ten_pairs(n//10)
  156.         return 0
  157.     return return_tp(n%10,n//10)
复制代码
回复

使用道具 举报

🔗
 楼主| lilirr 2019-7-19 17:15:05 | 只看该作者
全局:
exam_prep01

  1. exampre01
  2. def longest_increasing_suffix(n):
  3.     m,suffix,k = 10,0,1
  4.     while n:
  5.         n, last = n//10, n%10
  6.         if suffix == 0 or last <m*suffix//k:
  7.             m, suffix, k =10, suffix + last*k, k*10  
  8.         else:
  9.             return suffix
  10.     return suffix

  11. def sandwich(n):
  12.     tens,ones = (n//10)%10,n%10
  13.     n = n//100
  14.     while n:
  15.         if n%10 == ones:
  16.             return True
  17.         else:
  18.             tens, ones = n%10, tens
  19.             n = n//10
  20.     return False

  21. def luhn_sum(n):
  22.     def luhn_digit(digit):
  23.         x = digit*multiplier
  24.         return (x//10)+ (x%10)
  25.     total, multiplier = 0, 1
  26.     while n:
  27.         n, last = n//10, n%10
  28.         total += luhn_digit(last)
  29.         multiplier = 3-multiplier
  30.     return int(total)

复制代码
回复

使用道具 举报

🔗
 楼主| lilirr 2019-7-20 15:19:47 | 只看该作者
全局:
exam_prep02

  1. exam_prep02

  2. def kbonacci(n,k):
  3.     if n<k - 1:
  4.         return 0
  5.     elif n == k-1:
  6.         return 1
  7.     else:
  8.         total = 0
  9.         i = 0
  10.         while i < n:
  11.             total = total +kbonacci(i,k)

  12. def combine(left, right):
  13.     """Return all of LEFT's digits followed by all of RIGHT's digits."""
  14.     factor = 1
  15.     while factor <= right:
  16.         factor = factor * 10
  17.     return left * factor + right

  18. def reverse(n):
  19.     """Return the digits of N in reverse.
  20.     >>> reverse(122543)
  21.     345221
  22.     """
  23.     if n < 10:
  24.         return n
  25.     else:   
  26.         return combine(n%10 , reverse(n//10))

  27. def remove(n, digit):
  28.     """Return all digits of N that are not DIGIT, for DIGIT less than 10.
  29.     >>> remove(243132, 3)
  30.     2412
  31.     >>> remove(remove(243132, 1), 2)
  32.     433
  33.     """
  34.     removed = 0
  35.     while n != 0:
  36.         n, last = n//10, n%10
  37.         if last != digit:
  38.             removed = combine(removed,last)
  39.     return reverse(removed)


  40. square = lambda x: x * x
  41. double = lambda x: 2 * x
  42. def memory(x, f):
  43.     def g(h):
  44.         print(f(x))
  45.         return memory(x,h)
  46.     return g

  47. lamb = lambda lamb: lambda: lamb + lamb
  48. lamb(1000)() + (lambda b, c: b() * b() -c)(lamb(2), 1)
复制代码
回复

使用道具 举报

🔗
smoothiethu 2019-7-23 02:06:40 | 只看该作者
全局:
赞楼主,刚刷到lab02,希望能够一起组队,共同刷题。
回复

使用道具 举报

🔗
 楼主| lilirr 2019-7-23 15:46:08 | 只看该作者
全局:
hw04 (没做extra question)

  1. HW_SOURCE_FILE = 'hw04.py'

  2. ###############
  3. #  Questions  #
  4. ###############

  5. def intersection(st, ave):
  6.     """Represent an intersection using the Cantor pairing function."""
  7.     return (st+ave)*(st+ave+1)//2 + ave

  8. def street(inter):
  9.     return w(inter) - avenue(inter)

  10. def avenue(inter):
  11.     return inter - (w(inter) ** 2 + w(inter)) // 2

  12. w = lambda z: int(((8*z+1)**0.5-1)/2)

  13. def taxicab(a, b):
  14.     """Return the taxicab distance between two intersections.

  15.     >>> times_square = intersection(46, 7)
  16.     >>> ess_a_bagel = intersection(51, 3)
  17.     >>> taxicab(times_square, ess_a_bagel)
  18.     9
  19.     >>> taxicab(ess_a_bagel, times_square)
  20.     9
  21.     """
  22.     "*** YOUR CODE HERE ***"
  23.     return abs(street(a) - street(b)) + abs(avenue(a) - avenue(b))

  24. def squares(s):
  25.     """Returns a new list containing square roots of the elements of the
  26.     original list that are perfect squares.

  27.     >>> seq = [8, 49, 8, 9, 2, 1, 100, 102]
  28.     >>> squares(seq)
  29.     [7, 3, 1, 10]
  30.     >>> seq = [500, 30]
  31.     >>> squares(seq)
  32.     []
  33.     """
  34.     "*** YOUR CODE HERE ***"
  35.     ls = []
  36.     for ele in s:
  37.         ls += [ i for i in range(1,ele//2+2) if i*i==ele]
  38.     return ls
  39.             

  40. def g(n):
  41.     """Return the value of G(n), computed recursively.

  42.     >>> g(1)
  43.     1
  44.     >>> g(2)
  45.     2
  46.     >>> g(3)
  47.     3
  48.     >>> g(4)
  49.     10
  50.     >>> g(5)
  51.     22
  52.     >>> from construct_check import check
  53.     >>> check(HW_SOURCE_FILE, 'g', ['While', 'For'])
  54.     True
  55.     """
  56.     "*** YOUR CODE HERE ***"
  57.     if n <=3:
  58.         return n
  59.     else:
  60.         return g(n-1)+2*g(n-2)+3*g(n-3)

  61. def g_iter(n):
  62.     """Return the value of G(n), computed iteratively.

  63.     >>> g_iter(1)
  64.     1
  65.     >>> g_iter(2)
  66.     2
  67.     >>> g_iter(3)
  68.     3
  69.     >>> g_iter(4)
  70.     10
  71.     >>> g_iter(5)
  72.     22
  73.     >>> from construct_check import check
  74.     >>> check(HW_SOURCE_FILE, 'g_iter', ['Recursion'])
  75.     True
  76.     """
  77.     "*** YOUR CODE HERE ***"
  78.     bs1,bs2,bs3 = 1,2,3
  79.     gcase = 4
  80.     total = 0
  81.     if n ==1:
  82.         return bs1
  83.     elif n ==2:
  84.         return bs2
  85.     elif n ==3:
  86.         return bs3
  87.     else:
  88.         while gcase <=n:
  89.             new = 3*bs1+2*bs2+bs3
  90.             bs1,bs2,bs3 = bs2,bs3,new
  91.             gcase +=1
  92.         return new
  93.    
  94. def pingpong(n):
  95.     """Return the nth element of the ping-pong sequence.

  96.     >>> pingpong(7)
  97.     7
  98.     >>> pingpong(8)
  99.     6
  100.     >>> pingpong(15)
  101.     1
  102.     >>> pingpong(21)
  103.     -1
  104.     >>> pingpong(22)
  105.     0
  106.     >>> pingpong(30)
  107.     6
  108.     >>> pingpong(68)
  109.     2
  110.     >>> pingpong(69)
  111.     1
  112.     >>> pingpong(70)
  113.     0
  114.     >>> pingpong(71)
  115.     1
  116.     >>> pingpong(72)
  117.     0
  118.     >>> pingpong(100)
  119.     2
  120.     >>> from construct_check import check
  121.     >>> check(HW_SOURCE_FILE, 'pingpong', ['Assign', 'AugAssign'])
  122.     True
  123.     """
  124.     "*** YOUR CODE HERE ***"
  125.     def at_seven(k):
  126.         if k == 0 :
  127.             return False
  128.         elif k%7==0 or has_seven(k):
  129.             return True
  130.         else:
  131.             return False
  132.     def change_sw(sw):
  133.         return -sw

  134.     def sum_up(n,sw):
  135.         if n == 1:
  136.             return 1
  137.         elif n ==2:
  138.             return sum_up(1,sw)+sw
  139.         elif at_seven(n-1):
  140.             return sum_up(n-1,change_sw(sw))+sw
  141.         else:
  142.             return sum_up(n-1,sw)+sw
  143.    
  144.     if len([i for i in range(1,n+1) if at_seven(i-1)])%2==0: #如果有双数个变方向的点
  145.         return sum_up(n,1) #第n个前面的符号是+
  146.     else:
  147.         return sum_up(n,-1)
  148.    
  149. def has_seven(k):
  150.     """Returns True if at least one of the digits of k is a 7, False otherwise.

  151.     >>> has_seven(3)
  152.     False
  153.     >>> has_seven(7)
  154.     True
  155.     >>> has_seven(2734)
  156.     True
  157.     >>> has_seven(2634)
  158.     False
  159.     >>> has_seven(734)
  160.     True
  161.     >>> has_seven(7777)
  162.     True
  163.     """
  164.     if k % 10 == 7:
  165.         return True
  166.     elif k < 10:
  167.         return False
  168.     else:
  169.         return has_seven(k // 10)

  170. def count_change(amount):
  171.     """Return the number of ways to make change for amount.

  172.     >>> count_change(7)
  173.     6
  174.     >>> count_change(10)
  175.     14
  176.     >>> count_change(20)
  177.     60
  178.     >>> count_change(100)
  179.     9828
  180.     """
  181.     "*** YOUR CODE HERE ***"
  182.     i = 0
  183.     coin_ls = []
  184.     while pow(2,i)<=amount:
  185.         coin_ls += [pow(2,i)]
  186.         i +=1
  187.     def partition(n,coin):
  188.         if n <= 0 :
  189.             return 0
  190.         elif n == 1:
  191.             return 1
  192.         elif n ==coin:#2+0+8这种情况
  193.             return 1+partition(n,coin//2)
  194.         elif coin not in coin_ls:
  195.             return 0
  196.         else:
  197.             return partition(n-coin,coin)+partition(n,coin//2)
  198.     return partition(amount,coin_ls[-1])

  199. ###################
  200. # Extra Questions #
  201. ###################

  202. from operator import sub, mul

  203. def make_anonymous_factorial():
  204.     """Return the value of an expression that computes factorial.

  205.     >>> make_anonymous_factorial()(5)
  206.     120
  207.     >>> from construct_check import check
  208.     >>> check(HW_SOURCE_FILE, 'make_anonymous_factorial', ['Assign', 'AugAssign', 'FunctionDef', 'Recursion'])
  209.     True
  210.     """
  211.     return 'YOUR_EXPRESSION_HERE'
复制代码
回复

使用道具 举报

🔗
smoothiethu 2019-7-24 18:09:41 | 只看该作者
全局:

请问这个hog的problem6 的OK测试1的line1 应该是是什么啊一直过不去
回复

使用道具 举报

🔗
 楼主| lilirr 2019-7-27 10:55:22 | 只看该作者
全局:
smoothiethu 发表于 2019-7-24 18:09
请问这个hog的problem6 的OK测试1的line1 应该是是什么啊一直过不去

在github上找,每一个都有答案
一个参考
回复

使用道具 举报

🔗
smoothiethu 2019-7-27 11:19:28 | 只看该作者
全局:
lilirr 发表于 2019-7-27 10:55
在github上找,每一个都有答案
一个参考

谢谢!虽然我还是没找到test的答案= =
回复

使用道具 举报

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

本版积分规则

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