reference_solution:
def square_perimeter(side):
    return 4 * side

description: Returns the perimeter of a square.

test: assert square_perimeter(0)==0 and square_perimeter(-12684)==-50736


reference_solution:
def circle_area(r):
    return  r * r * 3.14

description: Return the area of a circle.


reference_solution:
def circle_circumference(r):
    return r * 2 * 3.14

description: Return the circumference of a circle.

test: assert circle_circumference(0)==0.0 and circle_circumference(25619)==160887.32


reference_solution:
def rectangle_perimeter(l, w):
    return 2 * (l + w)

description: Return the perimeter of a rectangle with the given coordinates.


reference_solution:
def square_area(l):
   return l * l

description: Return the area of a square.

test: assert square_area(0)==0 and square_area(21138)==446815044


func_name: reverse

reference_solution:
def reverse(a):
   b=[]
   i=0
   while i < len(a):
      b.append(a[len(a)-1-i])
      i = i + 1
   return b

description: Iteratively reverse a list of elements.


assignment_id: area

func_name: area

reference_solution:
def area (a):
	x= 3.141 *a**2
	return x

description: Return the area of a circle with the given coordinates.


assignment_id: circumference

func_name: circumference

reference_solution:
def circumference(r):
	y=3.141 * 2 *r
	return y

description: Return the circumference of a circle.

test: assert circumference(0)==0.0 and circumference(-23091)==-145057.662


assignment_id: double

func_name: double

reference_solution:
def double(n):
	y=2*n
	return y

description: Double a number or a string.

test: assert double(0)==0 and double(-7601)==-15202


reference_solution:
def selection_sort(a):
	i=0
	while i<len(a):
		p=i
		j=i+1
		while j < len(a):
			if a[j] < a[p] :
				p=j
			j=j+1
		tmp=a[p]
		a[p]=a[i]
		a[i]=tmp
		i=i+1
	return a

description: Sort a list by repeatedly move minimimum of remaining sublist to front.


description: Reverse a list of elements by swapping its elements.


assignment_id: bsearch

func_name: bsearch

reference_solution:
def bsearch(a, q):
        low = 0
        high = len(a)
        while low < high:
            mid = (low + high) // 2
            if a[mid] < q:
                low = mid + 1
            else:
                high = mid
        return low

description: Search for element q in the sorted array a.


reference_solution:
def print_keys(dict):
    '''print all the keys in a dictionary, one per line'''
    for key in dict:
        print(key)

description: Print all the keys in a dictionary, one per line.

test: assert print_keys({})==None and print_keys({})==None and print_keys({'1': 'a'})==None and print_keys({'1': 'a'})==None and print_keys({'1': 'a', '2': 'b', '3': 'c', '4': 'd'})==None and print_keys({'1': 'a', '2': 'b', '3': 'c', '4': 'd'})==None and print_keys({'key1': 'a', 'key2': 'b', 'key3': 'a'})==None and print_keys({'key1': 'a', 'key2': 'b', 'key3': 'a'})==None


reference_solution:
def count_letters(s):
    if not s:
        return 0
    return 1 + count_letters(s[1:])

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: maximum

func_name: maximum

reference_solution:
def maximum(l):
    if len(l) == 1:
        return l[0]
    tail_max = maximum(l[1:])
    return l[0] if l[0] > tail_max else tail_max
    # Could do this but ought not use max I suppose...
    # return max(l[0], maximum(l[1:]))

description: Return the maximum element in a list of numbers.


assignment_id: minimum

func_name: minimum

reference_solution:
def minimum(l):
    if len(l) == 1:
        return l[0]
    tail_min = minimum(l[1:])
    return l[0] if l[0] < tail_min else tail_min
    # Could do this but ought not use min I suppose...
    # return min(l[0], rmin(l[1:]))

description: Return the minimum element in a list of numbers.


assignment_id: selectionsort

func_name: selectionsort

reference_solution:
def selectionsort(A):

    i = 0
    while i < len(A):
        min_index = i
        j = i+1
        while j < len(A):
            if A[j] < A[min_index]:
                min_index = j
            j += 1
        A[i], A[min_index] = A[min_index], A[i]
        i += 1

# repeatedly insert into sorted array

test: assert selectionsort([])==None and selectionsort([])==None and selectionsort([0])==None and selectionsort([0])==None


assignment_id: power

func_name: power

reference_solution:
def power(m, n):
    if not n:
        return 1
    return m * power(m, n-1)

description: Raise m to the power of n.


assignment_id: quicksort

func_name: quicksort

reference_solution:
def partition(A, p, r):
    q = j = p
    while j < r:
      if A[j] <= A[r]:
        A[q], A[j] = A[j], A[q]
        q = q + 1
      j = j + 1
    A[q], A[r] = A[r], A[q]
    return q

def quicksort(A, p, r):

  def partition(A, p, r):
    q = j = p
    while j < r:
      if A[j] <= A[r]:
        A[q], A[j] = A[j], A[q]
        q = q + 1
      j = j + 1
    A[q], A[r] = A[r], A[q]
    return q

  if r <= p:
    return
  q = partition(A, p, r)
  quicksort(A, p, q-1)
  quicksort(A, q + 1, r)

description: Sort a list by recursively partitionioning list until sorted.


reference_solution:
def reverse_list(l):
    if l == []:
        return []
    return reverse_list(l[1:]) + [l[0]]
    # temp = reverse(l[1:])
    # temp.append(l[0])
    # return temp
    # note we cannot use return reverse(l[1:]).append(l[0]) because append()
    # does not return anything

description: Recursively reverse a list of elements.


assignment_id: sort

func_name: sort

reference_solution:
def swap(a, i, j):
   tmp  = a[i]
   a[i] = a[j]
   a[j] = tmp

def find_position_of_smallest(a, i):
 p = i
 while i < len(a):
    if a[i]  < a[p]:
       p = i
    i = i + 1
 return p

def sort(a):
   i = 0
   while i < len(a):
      p = find_position_of_smallest(a,i)
      swap(a, i, p)
      i = i + 1

description: Sort a list by repeatedly moving the minimimum of the remaining sublist to the 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: union

func_name: union

reference_solution:
def union(a, b):
	c=[]
	i = 0
	while i < len(a):
		if a[i] not in c:
			c.append(a[i])
		i = i + 1

	p = 0
	while p < len(b):
		if b[p] not in c:
			c.append(b[p])
		p = p + 1

	return c

description: Merge two lists into a single one.


assignment_id: intersection

func_name: intersection

reference_solution:
def intersection(a, b):
	d=[]
	i = 0
	while i < len(a):
		if a[i] in b and a[i] not in d:
			d.append(a[i])
		i = i + 1

	return d

description: Return the intersection between two lists.


reference_solution:
def swap_keys_values(d):
    return dict([(v, k) for (k, v) in list(d.items())])

description: Swap the keys of a dictionary with its values.


reference_solution:
def swap_unique_keys_values(d):
    # Have to explicitly convert dict_view type to list type in Python 3
    return dict([(v, k) for (k, v) in list(d.items()) if list(d.values()).count(v) == 1])

description: Swap the keys of a dictionary with its unique values.


reference_solution:
def merge_lists(l1,l2):
  '''take two lists l1 and l2 and return a third list which contains
  every second element of l1 and l2'''
  l3 = []
  i = 0
  while i < len(l1):
      l3.append(l1[i])
      i += 2
  i = 0
  while i < len(l2):
      l3.append(l2[i])
      i += 2
  return l3

description: Take two lists l1 and l2 and return a third list which contains every second element of l1 and l2.


reference_solution:
def weird_case(some_str):
    '''takes a string some_str and contains a new string which contains
every second letter in some_str'''
    new_str = ""
    i = 0
    lc = 0 # separate count for the letters
    while i < len(some_str):
        if some_str[i].isalpha():
            if lc % 2 == 0:
                new_str += some_str[i].upper()
            else:
                new_str += some_str[i].lower()
            lc += 1
        else:
          new_str += some_str[i]
        i = i + 1
    return new_str

description: Takes a string some_str and contains a new string which is the same as some_str but the case alternates between upper and lower.

test: assert weird_case('au0x6')==Au0X6 and weird_case('F:BnJ')==F:bNj


reference_solution:
def remove_zeros(list):
    '''take a list of numbers and remove zeros from the list'''
    while 0 in list:
        list.remove(0)

description: Take a list of numbers and removes the zeros from it.

test: assert remove_zeros([])==None and remove_zeros([])==None and remove_zeros([5202, -24344, -6528])==None and remove_zeros([5202, -24344, -6528])==None


assignment_id: overlap

func_name: overlap

description: Test if two circles overlap.


reference_solution:
def append2list(l1, l2=None):
    if l2 == None:
        l2 = []
    for i in l1:
        l2.append(i)
    return l2

description: Appends elements of a list l1 at the end of the list l2. If l2 not supplied default to empty list.


func_name: search

reference_solution:
def search(str,letter):
  '''returns True if letter is in str, False if it is not there'''
  i = 0
  while i < len(str):
    if letter == str[i]:
      return True
    i = i + 1
  return False

description: Iteratively search for a letter in 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


func_name: index

reference_solution:
def index(str,letter):
  '''returns the position of letter in str, -1 if it is not there'''
  i = 0
  while i < len(str):
    if letter == str[i]:
      return i
    i = i + 1
  return -1

description: Iteratively search for the position of letter in str, return -1 if it is not there.


func_name: fibonacci

reference_solution:
def fibonacci(n):
  '''returns the value of the fibonacci series at position n'''
  if n == 0:
    return 0
  if n == 1:
    return 1
  else:
    fibN_2 = 0
    fibN_1 = 1
    i = 2
    while i <=n:
      fibN = fibN_2 + fibN_1
      fibN_2 = fibN_1
      fibN_1 = fibN
      i = i + 1
    return fibN

description: Iteratively compute the value of the fibonacci series at position n.


reference_solution:
def search(str,letter):
  '''returns True if letter is in str, False otherwise'''
  if str == "":
    return False
  elif str[0] == letter:
    return True
  else:
    #keep looking
    return search(str[1:],letter)

description: Recursively search for a letter in a string


reference_solution:
def index(str,letter,pos):
  '''returns the position of letter in str, -1 if it is not there'''
  if pos == len(str):
    return -1
  elif str[pos] == letter:
    return pos
  else:
    return index(str,letter,pos+1)

description: Recursively search for the position of letter in str, return -1 if it is not there.


reference_solution:
def fibonacci(n):
  '''returns the value of the fibonacci series at position n'''
  if n == 0:
    return 0
  elif n == 1:
    return 1
  else:
    return fibonacci(n-1) + fibonacci(n-2)

description: Recursively compute 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