Skip to content

Instantly share code, notes, and snippets.

@evrom
Created May 30, 2017 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrom/740f3d8bf98469aebffc0d25cc8db856 to your computer and use it in GitHub Desktop.
Save evrom/740f3d8bf98469aebffc0d25cc8db856 to your computer and use it in GitHub Desktop.
"""
Make two test cases pass to pass the class.
Run with "python3 -m unittest exam2.py"
NOTE: Every problem has a one line solution. These solutions
are just one way to solve these problems.
"""
from unittest import TestCase
def solution_1(a, b):
# NOTE: Testcase description includes '.' at end,
# but testcases do not have '.'
# this is my error in writing this test.
return '{a} times {b} is {c}'.format(a=a, b=b, c=a*b)
class ProductTestCase(TestCase):
"""
Args:
a - a number
b - another number
Returns:
a string with the format
"$A times $B is $C.", where
$A is the first argument, $B is a the second argument,
and $C is the $A*$B.
>>> test_function(3, 4)
"3 times 4 is 12."
"""
def setUp(self):
self.test_function = solution_1 # assign to your function
def test_ex1(self):
result = self.test_function(2, 3)
self.assertEqual(result, '2 times 3 is 6')
def test_ex2(self):
result = self.test_function(5, 4)
self.assertEqual(result, '5 times 4 is 20')
def solution_2(english_words, estonian_words):
result = {}
for i in range(len(english_words)):
result[english_words[i]] = estonian_words[i]
return result
class MakeDictionaryTestCase(TestCase):
"""
Args:
english_words - a list of english words
estonian_words - a list of estonian words
Returns:
a dictionary where the keys are english words and the values are
estonian words. The first word in english_words is mapped to the
first word in estonian_words
>>> test_function(['cat', 'dog'], ['kass', 'koer'])
{'cat': 'kass', 'dog': 'koer'}
"""
def setUp(self):
self.test_function = solution_2 # assign to your function
def test_ex1(self):
english_words = ['dandelion', 'calendula', 'echinacea']
estonian_words = ['võilill', 'saialill', 'päevakübar']
expected_result = {'dandelion': 'võilill', 'calendula': 'saialill',
'echinacea': 'päevakübar'}
result = self.test_function(english_words, estonian_words)
self.assertEqual(result, expected_result)
def test_ex2(self):
english_words = ['millet', 'buckwheat']
estonian_words = ['hirss', 'tatar']
expected_result = {'millet': 'hirss', 'buckwheat': 'tatar'}
result = self.test_function(english_words, estonian_words)
self.assertEqual(result, expected_result)
def solution_3(names):
last_element_index = (len(names) - 1)
result = 'Hello'
for index, name in enumerate(names):
result = result + ' ' + name
if index != last_element_index:
result = result + ','
result = result + '.'
return result
class GreetManyTestCase(TestCase):
"""
Args:
names - a list of names
Returns:
a string starting with 'Hello ', followed by the names separated by
commas, and ending with a '.'
Hint:
There a multiple ways to do this. The last name in the list does not
have ', ' following it.
you may need to check if a name is the last element of the list.
>>> test_function(['Donald', 'Kersti'])
'Hello Donald, Kersti.'
"""
def setUp(self):
self.test_function = solution_3 # assign to your function
def test_ex1(self):
names = ['Bob']
result = self.test_function(names)
self.assertEqual(result, 'Hello Bob.')
def test_ex2(self):
names = ['Taavet', 'Kalev', 'Maarja']
result = self.test_function(names)
self.assertEqual(result, 'Hello Taavet, Kalev, Maarja.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment