File size: 19,937 Bytes
c4a44b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{"assignment_id":"reverse.py_ca277_reverse","func_name":"reverse","reference_solution":"def reverse(a):\n   b=[]\n   i=0\n   while i < len(a):\n      b.append(a[len(a)-1-i])\n      i = i + 1\n   return b","description":"Reverse a list of elements.","test":"assert reverse([])==[] and reverse([20, 10, 0, -10, -20])==[-20, -10, 0, 10, 20] and reverse(['toto', True, [10, 0, 9], 12.8, 6])==[6, 12.8, [10, 0, 9], True, 'toto']"}
{"assignment_id":"func_selection_sort.py_ca116_selection_sort","func_name":"selection_sort","reference_solution":"def selection_sort(a):\n\ti=0\n\twhile i<len(a):\n\t\tp=i\n\t\tj=i+1\n\t\twhile j < len(a):\n\t\t\tif a[j] < a[p] :\n\t\t\t\tp=j\n\t\t\tj=j+1\n\t\ttmp=a[p]\n\t\ta[p]=a[i]\n\t\ta[i]=tmp\n\t\ti=i+1\n\treturn a\n\n","description":"Repeatedly move minimimum of remaining sublist to front.","test":"assert selection_sort([])==[] and selection_sort([0])==[0] and selection_sort([25204, -1, -18176])==[-18176, -1, 25204] and selection_sort([-18176, -1, 25204])==[-18176, -1, 25204]"}
{"assignment_id":"func_reverse.py_ca116_reverse","func_name":"reverse","reference_solution":"def reverse(a):\n   b=[]\n   i=0\n   while i < len(a):\n      b.append(a[len(a)-1-i])\n      i = i + 1\n   return b","description":"Reverse a list of elements.","test":"assert reverse([])==[] and reverse([0])==[0] and reverse([-103, 1867157052, 10933, -70, 31758, 113466788817036974729578468346735566318, 1867157052])==[1867157052, 113466788817036974729578468346735566318, 31758, -70, 10933, 1867157052, -103] and reverse([-103, 113466788817036974729578468346735566318, 31758, 1867157052, 10933, -70, 1867157052])==[1867157052, -70, 10933, 1867157052, 31758, 113466788817036974729578468346735566318, -103] and reverse([1867157052, 1867157052, 10933, -70, 31758, 113466788817036974729578468346735566318, -103])==[-103, 113466788817036974729578468346735566318, 31758, -70, 10933, 1867157052, 1867157052] and reverse([1867157052, 113466788817036974729578468346735566318, 31758, -70, 10933, 1867157052, -103])==[-103, 1867157052, 10933, -70, 31758, 113466788817036974729578468346735566318, 1867157052] and reverse([-103, 113466788817036974729578468346735566318, 31758, -70, 10933, 1867157052, 1867157052])==[1867157052, 1867157052, 10933, -70, 31758, 113466788817036974729578468346735566318, -103] and reverse([[1]])==[[1]]"}
{"assignment_id":"func_bsearch.py_ca116_bsearch","func_name":"bsearch","reference_solution":"def bsearch(a, q):\n        low = 0\n        high = len(a)\n        while low < high:\n            mid = (low + high) \/\/ 2\n            if a[mid] < q:\n                low = mid + 1\n            else:\n                high = mid\n        return low","description":"Search for an element in an array using the binary search algorithm.","test":"assert bsearch([],12)==0 and bsearch([1, 2, 3, 4, 6, 7, 8],5)==4 and bsearch([1, 2, 3, 4, 6, 7, 8],1)==0 and bsearch([1, 2, 3, 4, 6, 7, 8],4)==3"}
{"assignment_id":"swap_42.py_ca117_swap_keys_values","func_name":"swap_keys_values","reference_solution":"def swap_keys_values(d):\n    return {v: k for (k, v) in list(d.items())}\n","description":"Swap keys with values.","test":"assert swap_keys_values({})=={} and swap_keys_values({'1': 'a'})=={'a': '1'} and swap_keys_values({'1': 'a', '2': 'b', '3': 'c', '4': 'd'})=={'a': '1', 'b': '2', 'c': '3', 'd': '4'} and swap_keys_values({'1': 'a', '2': 'b', '3': 'a'})=={'a': '3', 'b': '2'}"}
{"assignment_id":"swap_more_42.py_ca117_swap_unique_keys_values","func_name":"swap_unique_keys_values","reference_solution":"def swap_unique_keys_values(d):\n    # Have to explicitly convert dict_view type to list type in Python 3\n    return dict([(v, k) for (k, v) in list(d.items()) if list(d.values()).count(v) == 1])\n","description":"Swap keys with unique values.","test":"assert swap_unique_keys_values({})=={} and swap_unique_keys_values({'1': 'a'})=={'a': '1'} and swap_unique_keys_values({'1': 'a', '2': 'b', '3': 'c', '4': 'd'})=={'a': '1', 'b': '2', 'c': '3', 'd': '4'} and swap_unique_keys_values({'1': 'a', '2': 'b', '3': 'a'})=={'b': '2'}"}
{"assignment_id":"circle_62.py_ca117_overlap","func_name":"overlap","reference_solution":"def overlap(x1=0, y1=0, r1=1, x2=0, y2=0, r2=1):\n    d = ((x2-x1)**2 + (y2-y1)**2)**0.5\n    if d < r1 + r2:\n        return True\n    return False\n\n","description":"Test if two circles overlap.","test":"assert overlap(0,0,0,0,0,0)==False and overlap(-10127,-4523146639991987157,4984975518095343106,16069,-6161206257421287809,-21999)==True and overlap(28812,-95,-14565,-14231,-57,-13105)==False and overlap(29825,-1453,8499,60,-25950,-863536228)==False and overlap(4254,-23892,-290,8971,1739,-4934)==False and overlap(-632291906,26448,10662,-106,-158122742,24689)==False"}
{"assignment_id":"mutable_62.py_ca117_append2list","func_name":"append2list","reference_solution":"def append2list(l1, l2=None):\n    if l2 == None:\n        l2 = []\n    for i in l1:\n        l2.append(i)\n    return l2\n\n","description":"Append l1 to l2. If l2 not supplied default to empty list.","test":"assert append2list([0, 0],[0])==[0, 0, 0] and append2list([0, 0, 0, 0, 0],[0, 0, 0])==[0, 0, 0, 0, 0, 0, 0, 0] and append2list([0, 0],[0, 0, 0])==[0, 0, 0, 0, 0] and append2list([],[])==[] and append2list([0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0])==[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"}
{"assignment_id":"funcs.py_ca278_merge_lists","func_name":"merge_lists","reference_solution":"def merge_lists(l1,l2):\n  '''take two lists l1 and l2 and return a third list which contains \n  every second element of l1 and l2'''\n  l3 = []\n  i = 0\n  while i < len(l1):\n      l3.append(l1[i])\n      i += 2\n  i = 0\n  while i < len(l2):\n      l3.append(l2[i])\n      i += 2\n  return l3\n\n","description":"Obtain every second element of l1 and l2.","test":"assert merge_lists([],[])==[] and merge_lists([],[1, 2, 3, 4])==[1, 3] and merge_lists([1, 2, 3, 4],[])==[1, 3] and merge_lists([10, 20, 45, 2, -10987, 89165],[9, 0, -6754, 5625264, 8765])==[10, 45, -10987, 9, -6754, 8765]"}
{"assignment_id":"funcs.py_ca278_remove_zeros","func_name":"remove_zeros","reference_solution":"def remove_zeros(list):\n    '''take a list of numbers and remove zeros from the list'''\n    while 0 in list:\n        list.remove(0)\n\n","description":"Remove zeros from list.","test":"assert remove_zeros([])==None and remove_zeros([])==None and remove_zeros([1, 2, 3, 4, -12, -2])==None and remove_zeros([1, 2, 3, 4, -12, -2])==None and remove_zeros([0, 0, 0, 0, 0])==None and remove_zeros([0, 0, 0, 0, 0])==None and remove_zeros([1, 0, 3, 2, 1, 9, 0, 0, 0])==None and remove_zeros([1, 0, 3, 2, 1, 9, 0, 0, 0])==None"}
{"assignment_id":"funcs08.py_ca278_merge_lists","func_name":"merge_lists","reference_solution":"def merge_lists(l1,l2):\n  '''take two lists l1 and l2 and return a third list which contains \n  every second element of l1 and l2'''\n  l3 = []\n  i = 0\n  while i < len(l1):\n      l3.append(l1[i])\n      i += 2\n  i = 0\n  while i < len(l2):\n      l3.append(l2[i])\n      i += 2\n  return l3\n\n","description":"Obtain every second element of l1 and l2.","test":"assert merge_lists([],[])==[] and merge_lists([],[1, 2, 3, 4])==[1, 3] and merge_lists([1, 2, 3, 4],[])==[1, 3] and merge_lists([10, 20, 45, 2, -10987, 89165],[9, 0, -6754, 5625264, 8765])==[10, 45, -10987, 9, -6754, 8765]"}
{"assignment_id":"funcs08.py_ca278_remove_zeros","func_name":"remove_zeros","reference_solution":"def remove_zeros(list):\n    '''remove zeros from list'''\n    while 0 in list:\n        list.remove(0)\n\n","description":"Remove zeros from list.","test":"assert remove_zeros([])==None and remove_zeros([])==None and remove_zeros([1, 2, 3, 4, -12, -2])==None and remove_zeros([1, 2, 3, 4, -12, -2])==None and remove_zeros([0, 0, 0, 0, 0])==None and remove_zeros([0, 0, 0, 0, 0])==None and remove_zeros([1, 0, 3, 2, 1, 9, 0, 0, 0])==None and remove_zeros([1, 0, 3, 2, 1, 9, 0, 0, 0])==None"}
{"assignment_id":"funcs08.py_ca278_search","func_name":"search","reference_solution":"def search(str,sub):\n  '''returns the position of sub in str, -1 if it is not there'''\n  i = 0\n  while i < len(str):\n    if sub == str[i]:\n      return i\n    i = i + 1\n  return -1\n\n","description":"Return whether a letter is part of a string","test":"assert search('','0')==-1 and search('0','0')==0 and search('ERFE0Rfsef','0')==4 and search('ERFERfsef0','0')==9 and search('','a')==-1 and search('cbzeycuzbvyzuvb','a')==-1 and search('cbsducsvdbcyts','c')==0 and search('dbzeducvbzy','y')==10 and search('xqvsgxcutvy','u')==7"}
{"assignment_id":"funcs08.py_ca278_fibonacci","func_name":"fibonacci","reference_solution":"def fibonacci(n):\n  '''returns the value of the fibonnaci series at position n'''\n  if n == 0:\n    return 1\n  elif n == 1:\n    return 1\n  else:\n    fibN_2 = 1\n    fibN_1 = 1\n    i = 2\n    while i <=n:\n      fibN = fibN_2 + fibN_1\n      fibN_2 = fibN_1\n      fibN_1 = fibN\n      i = i + 1\n    return fibN\n\n","description":"Return the value of the fibonacci series at position n.","test":"assert fibonacci(0)==1 and fibonacci(1)==1 and fibonacci(2)==2 and fibonacci(10)==89 and fibonacci(23)==46368"}
{"assignment_id":"iterative08.py_ca278_search","func_name":"search","reference_solution":"def search(str,letter):\n  '''returns True if letter is in str, False if it is not there'''\n  i = 0\n  while i < len(str):\n    if letter == str[i]:\n      return True\n    i = i + 1\n  return False\n\n","description":"Return whether a letter is part of a string","test":"assert search('','0')==False and search('0','0')==True and search('ERFE0Rfsef','0')==True and search('ERFERfsef0','0')==True and search('','a')==False and search('cbzeycuzbvyzuvb','a')==False and search('cbsducsvdbcyts','c')==True and search('dbzeducvbzy','y')==True and search('xqvsgxcutvy','u')==True"}
{"assignment_id":"iterative08.py_ca278_index","func_name":"index","reference_solution":"def index(str,letter):\n  '''returns the position of letter in str, -1 if it is not there'''\n  i = 0\n  while i < len(str):\n    if letter == str[i]:\n      return i\n    i = i + 1\n  return -1\n\n\n","description":"Returns the position of letter in str, -1 if it is not there.","test":"assert index('','0')==-1 and index('0','0')==0 and index('9dK=1%s)a>PI5FmV\/dq,-=]N2MmOkB .hRL_P2','=')==3"}
{"assignment_id":"iterative08.py_ca278_fibonacci","func_name":"fibonacci","reference_solution":"def fibonacci(n):\n  '''returns the value of the fibonnaci series at position n'''\n  if n == 0 or n == 1:\n    return 1\n  else:\n    fibN_2 = 1\n    fibN_1 = 1\n    i = 2\n    while i <=n:\n      fibN = fibN_2 + fibN_1\n      fibN_2 = fibN_1\n      fibN_1 = fibN\n      i = i + 1\n    return fibN\n\n","description":"Return the value of the fibonacci series at position n.","test":"assert fibonacci(0)==1 and fibonacci(1)==1 and fibonacci(2)==2 and fibonacci(10)==89 and fibonacci(23)==46368"}
{"assignment_id":"recursive08.py_ca278_search","func_name":"search","reference_solution":"def search(str,letter):\n  '''returns True if letter is in str, False otherwise'''\n  if str == \"\":\n    return False\n  elif str[0] == letter:\n    return True\n  else:\n    #keep looking\n    return search(str[1:],letter)\n  \n\n","description":"Return whether a letter is part of a string","test":"assert search('','0')==False and search('0','0')==True and search('ERFE0Rfsef','0')==True and search('ERFERfsef0','0')==True and search('','a')==False and search('cbzeycuzbvyzuvb','a')==False and search('cbsducsvdbcyts','c')==True and search('dbzeducvbzy','y')==True and search('xqvsgxcutvy','u')==True"}
{"assignment_id":"recursive08.py_ca278_index","func_name":"index","reference_solution":"def index(str,letter,pos):\n  '''returns the position of letter in str, -1 if it is not there'''\n  if pos == len(str):\n    return -1\n  elif str[pos] == letter:\n    return pos\n  else:\n    return index(str,letter,pos+1)\n\n\n","description":"Return the position of letter in str, -1 if it is not there.","test":"assert index('','0',0)==-1 and index('dc8','s',0)==-1"}
{"assignment_id":"recursive08.py_ca278_fibonacci","func_name":"fibonacci","reference_solution":"def fibonacci(n):\n  '''returns the value of the fibonacci series at position n'''\n  if n == 0:\n    return 0\n  elif n == 1:\n    return 1\n  else:\n    return fibonacci(n-1) + fibonacci(n-2)\n\n","description":"Return the value of the fibonacci series at position n.","test":"assert fibonacci(0)==0 and fibonacci(1)==1 and fibonacci(2)==1 and fibonacci(10)==55 and fibonacci(23)==28657"}
{"assignment_id":"count_102.py_ca117_count_letters","func_name":"count_letters","reference_solution":"def count_letters(s):\n    if not s:\n        return 0\n    return 1 + count_letters(s[1:])\n","description":"Return the number of lettres in a string.","test":"assert count_letters('')==0 and count_letters('0')==1 and count_letters('##')==2 and count_letters('t')==1 and count_letters('fqfseqfseqsse')==13"}
{"assignment_id":"fibonacci_102.py_ca117_fibonacci","func_name":"fibonacci","reference_solution":"def fibonacci(n):\n    if n == 0:\n        return 1\n    if n == 1:\n        return 1\n    return fibonacci(n-1) + fibonacci(n-2)\n","description":"Return the value of the fibonacci series at position n.","test":"assert fibonacci(0)==1 and fibonacci(1)==1 and fibonacci(2)==2 and fibonacci(10)==89 and fibonacci(23)==46368"}
{"assignment_id":"maximum_102.py_ca117_maximum","func_name":"maximum","reference_solution":"def maximum(l):\n    if len(l) == 1:\n        return l[0]\n    tail_max = maximum(l[1:])\n    return l[0] if l[0] > tail_max else tail_max\n    # Could do this but ought not use max I suppose...\n    # return max(l[0], maximum(l[1:]))\n","description":"Return the maximum element in a list of numbers.","test":"assert maximum([0])==0 and maximum([67, 1, 2, -2, 0])==67 and maximum([0, -2, 2, 1, 67])==67 and maximum([-10, -23, -45, -9, -45617])==-9"}
{"assignment_id":"minimum_102.py_ca117_minimum","func_name":"minimum","reference_solution":"def minimum(l):\n    if len(l) == 1:\n        return l[0]\n    tail_min = minimum(l[1:])\n    return l[0] if l[0] < tail_min else tail_min\n    # Could do this but ought not use min I suppose...\n    # return min(l[0], rmin(l[1:]))\n","description":"Return the minimum element in a list of numbers.","test":"assert minimum([0])==0 and minimum([2, 10, 23, 9, 187])==2 and minimum([-2, -26, 7, -123, -1236521])==-1236521 and minimum([3896, 3673, 45, 16715, 23673])==45"}
{"assignment_id":"selectionsort_102.py_ca117_selectionsort","func_name":"selectionsort","reference_solution":"def selectionsort(A):\n\n    i = 0\n    while i < len(A):\n        min_index = i\n        j = i+1\n        while j < len(A):\n            if A[j] < A[min_index]:\n                min_index = j\n            j += 1\n        A[i], A[min_index] = A[min_index], A[i]\n        i += 1\n\n# repeatedly insert into sorted array\n","description":"Repeatedly move minimimum of remaining sublist to front.","test":"assert selectionsort([])==None and selectionsort([])==None and selectionsort([0])==None and selectionsort([0])==None"}
{"assignment_id":"quicksort_102.py_ca117_quicksort","func_name":"quicksort","reference_solution":"def partition(A, p, r):\n    q = j = p\n    while j < r:\n      if A[j] <= A[r]:\n        A[q], A[j] = A[j], A[q]\n        q = q + 1\n      j = j + 1\n    A[q], A[r] = A[r], A[q]\n    return q\n\n  \ndef quicksort(A, p, r):\n  \n  def partition(A, p, r):\n    q = j = p\n    while j < r:\n      if A[j] <= A[r]:\n        A[q], A[j] = A[j], A[q]\n        q = q + 1\n      j = j + 1\n    A[q], A[r] = A[r], A[q]\n    return q\n\n  if r <= p:\n    return\n  q = partition(A, p, r)\n  quicksort(A, p, q-1)\n  quicksort(A, q + 1, r)","description":"Recursively partition list until sorted.","test":"assert quicksort([],0,0)==None and quicksort([],0,0)==None and quicksort([5, 4, 3, 2, 1],0,4)==None and quicksort([5, 4, 3, 2, 1],0,4)==None and quicksort([5, 5, 5, 5],0,3)==None and quicksort([5, 5, 5, 5],0,3)==None and quicksort([5675, 564, 987, 154132, 8976, 0, -16221, 87526, 9826732986],0,8)==None and quicksort([5675, 564, 987, 154132, 8976, 0, -16221, 87526, 9826732986],0,8)==None"}
{"assignment_id":"reverse_102.py_ca117_reverse_list","func_name":"reverse_list","reference_solution":"def reverse_list(l):\n    if l == []:\n        return []\n    return reverse_list(l[1:]) + [l[0]]\n    # temp = reverse(l[1:])\n    # temp.append(l[0])\n    # return temp\n    # note we cannot use return reverse(l[1:]).append(l[0]) because append()\n    # does not return anything\n","description":"Reverse a list of elements.","test":"assert reverse_list([])==[] and reverse_list([0])==[0] and reverse_list([9650, 12103, -1947378123, -109345868112896702250879906349988323457, 85, -74, -10050, 787, -3])==[-3, 787, -10050, -74, 85, -109345868112896702250879906349988323457, -1947378123, 12103, 9650] and reverse_list([9650, 12103, -1947378123, -109345868112896702250879906349988323457])==[-109345868112896702250879906349988323457, -1947378123, 12103, 9650] and reverse_list([0, 0])==[0, 0] and reverse_list([85, -74, -10050, 787, 9650, 12103, -1947378123, -109345868112896702250879906349988323457, -3])==[-3, -109345868112896702250879906349988323457, -1947378123, 12103, 9650, 787, -10050, -74, 85] and reverse_list([85, -74, -10050, 787, -3, 9650, 12103, -1947378123, -109345868112896702250879906349988323457])==[-109345868112896702250879906349988323457, -1947378123, 12103, 9650, -3, 787, -10050, -74, 85]"}
{"assignment_id":"selection_sort.py_ca277_sort","func_name":"sort","reference_solution":"def swap(a, i, j):\n   tmp  = a[i]\n   a[i] = a[j]\n   a[j] = tmp\n\n\ndef find_position_of_smallest(a, i):\n p = i  \n while i < len(a):\n    if a[i]  < a[p]:\n       p = i\n    i = i + 1\n return p\t\n   \n\ndef sort(a):\n   i = 0\n   while i < len(a):\n      p = find_position_of_smallest(a,i)       \n      swap(a, i, p)\n      i = i + 1\n","description":"Repeatedly move minimimum of remaining sublist to front.","test":"assert sort([0])==None and sort([0])==None and sort([])==None and sort([])==None and sort([' '])==None and sort([' '])==None and sort([70, 339305549])==None and sort([70, 339305549])==None"}
{"assignment_id":"sets.py_ca277_union","func_name":"union","reference_solution":"def union(a, b):\n\tc=[]\n\ti = 0\n\twhile i < len(a):\n\t\tif a[i] not in c:\n\t\t\tc.append(a[i])\n\t\ti = i + 1\n\n\tp = 0\n\twhile p < len(b):\n\t\tif b[p] not in c:\n\t\t\tc.append(b[p])\n\t\tp = p + 1\n\n\treturn c\n\n","description":"Merge two lists into a single one.","test":"assert union([],[])==[] and union([25785],[25785])==[25785] and union([-90, 21135, 29310, -8059, 7114, -90, -5808, 1333, -18691, 7, -19450, 67745575129021321678860432754396203799, -9288, -161403197171354040520992237328119268342, 49, 120528818203287557, 133011337445419463191476642795673848676, 11952, 11996],[5, -9316, 7379237229304681733])==[-90, 21135, 29310, -8059, 7114, -5808, 1333, -18691, 7, -19450, 67745575129021321678860432754396203799, -9288, -161403197171354040520992237328119268342, 49, 120528818203287557, 133011337445419463191476642795673848676, 11952, 11996, 5, -9316, 7379237229304681733]"}
{"assignment_id":"sets.py_ca277_intersection","func_name":"intersection","reference_solution":"def intersection(a, b):\n\td=[]\n\ti = 0\n\twhile i < len(a):\n\t\tif a[i] in b and a[i] not in d:\n\t\t\td.append(a[i])\n\t\ti = i + 1\n\n\treturn d","description":"Return the elements which are in both lists.","test":"assert intersection([],[])==[] and intersection([20052, 20052, -9991],[102, 20052, -9991])==[20052, -9991] and intersection([38908273694008580353068229963801163341, 59, 38908273694008580353068229963801163341, -38, 28239, -2723, 24559, -5794],[38908273694008580353068229963801163341, 59, 38908273694008580353068229963801163341, -38, 28239, -2723, 24559, -5794])==[38908273694008580353068229963801163341, 59, -38, 28239, -2723, 24559, -5794]"}