Skip to content

Instantly share code, notes, and snippets.

@evrom

evrom/exam3.py Secret

Created June 1, 2017 05:12
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/4bd7d28bceeb0ef939b5d6392b1e50ab to your computer and use it in GitHub Desktop.
Save evrom/4bd7d28bceeb0ef939b5d6392b1e50ab 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`
"""
from unittest import TestCase
class EasyTestCase(TestCase):
"""
Arguments:
a - a number
b - a number
Returns:
A string with format "$A minus $B is $C."
Where "$A" is `a`, "$B" is `b`, and "$C" is `a - b`
>>> test_function(3, 1)
'3 minus 1 is 2.'
"""
def setUp(self):
self.test_function = None # Assign to your function
def test_ex1(self):
result = self.test_function(1, 3)
expected = '1 minus 3 is -2.'
self.assertEqual(result, expected)
def test_ex2(self):
result = self.test_function(5, 2)
expected = '5 minus 2 is 3.'
self.assertEqual(result, expected)
class AreNumbersTestCase(TestCase):
"""
Arguments:
l: a list of strings
Returns:
a dictionary mapping strings to a boolean value.
if all the characters in an element of `l` is are numbers,
that element is mapped to `True`.
If any character in an element of `l` is not a number,
that element is mapped to `False`
>>> test_function(['1', '2b', 'a'])
# '1' is only a number, '2b' and 'a' have characters that are not numbers
{'1': True, '2b': False, 'a': False}
HINT:
to determine if a string only has numbers, use the method `isdigits`
>>> my_string = '1'
>>> my_string.isdigit()
True
"""
def setUp(self):
self.test_function = None # Assign to your function
def test_ex1(self):
result = self.test_function(['5', 'g'])
expected = {'5': True, 'g': False}
self.assertEqual(result, expected)
def test_ex2(self):
result = self.test_function(['6', '7', 'u'])
expected = {'6': True, '7': True, 'u': False}
self.assertEqual(result, expected)
class CountdownTestCase(TestCase):
"""
Arguments:
t: number to start from
Returns:
A list of numbers starting with `t`. Each following
element is (the previous element) - 1. this continues
until the last element is `1`.
>>> test_function(3)
[3, 2, 1]
"""
def setUp(self):
self.test_function = None # Assign to your function
def test_ex1(self):
result = self.test_function(5)
expected = [5, 4, 3, 2, 1]
self.assertEqual(result, expected)
def test_ex2(self):
result = self.test_function(2)
expected = [2, 1]
self.assertEqual(result, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment