Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@evrom
Created May 30, 2017 20:30
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/3f0f4e8289af6230000cfbfeefed5826 to your computer and use it in GitHub Desktop.
Save evrom/3f0f4e8289af6230000cfbfeefed5826 to your computer and use it in GitHub Desktop.
"""
You must make two of the testcases pass to pass the test.
run tests with `python3 -m unittest exam1.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(name, exclamation='Hi'):
return "{e} {n}!".format(e=exclamation, n=name)
class HiTestCase(TestCase):
"""
Args:
name: a string
exclamation: optional, defaults to 'Hi'
Returns:
string with name prefixed by the
exclamation.
>>> test_function('Bob')
'Hi Bob!'
>>> test_function('Sally', exclamation='Bye')
'Bye Sally!'
"""
def setUp(self):
self.test_function = solution_1 # assign to your function
def test_ex1(self):
result = self.test_function('Kersti', 'Tere')
self.assertEqual(result, 'Tere Kersti!')
def test_ex2(self):
result = self.test_function('Taavi')
self.assertEqual(result, 'Hi Taavi!')
def solution_2(numbers):
for number in numbers:
if number % 2 != 0:
return True
return False
class IsOddTestCase(TestCase):
"""
Args:
numbers - a list of numbers
Returns:
Boolean
True if an odd number exists in the list of numbers
False if only even numbers exist in numbers.
>>> test_function([1,2])
# 1 is an odd number
True
>>> test_function([2,4])
# Both 2 and 4 are even numbers
False
"""
def setUp(self):
self.test_function = solution_2 # assign to your function
def test_ex1(self):
result = self.test_function([20, 44, 19])
self.assertEqual(result, True)
def test_ex2(self):
result = self.test_function([30, 100, 10])
self.assertEqual(result, False)
def solution_3(multiplier, numbers):
result = {}
for number in numbers:
result[number] = number * multiplier
return result
class MapProductsTestCase(TestCase):
"""
Args:
multiplier: a number to multiply numbers by
numbers: a list of numbers
Returns:
dict of numbers, mapping each number to the number multiplied by
the multiplier
>>> test_function(4, [1,2])
# 1*4 = 4, 2*4 = 8
{1: 3, 2: 8}
"""
def setUp(self):
self.test_function = solution_3 # assign to your function
def test_ex1(self):
result = self.test_function(2, [3, 4, 1])
self.assertEqual(result, {3: 6, 4: 8, 1: 2})
def test_ex2(self):
result = self.test_function(5, [5, 6])
self.assertEqual(result, {5: 25, 6: 30})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment