Skip to content

Instantly share code, notes, and snippets.

@wchan2
Last active August 29, 2015 14:16
Show Gist options
  • Save wchan2/089f1ed59370f980b1ae to your computer and use it in GitHub Desktop.
Save wchan2/089f1ed59370f980b1ae to your computer and use it in GitHub Desktop.
Simple Python Exercises

Simple Python Exercises

A series of simple exercises to help beginner's who are new to Python to practice their Python skills. Run the tests to find out whether your implementation is correct.

Steps

  1. Implement a function with an empty body in exercise.py
  2. Runs the tests using the command in the terminal python tests.py. At the end of the output it should indicate the number of failed or successful tests ie. FAILED (failures=11).
  3. Rinse and repeat steps 1 and 2 until all the tests show up as successful
# define a function that returns True if the year that is passed to it is a leap year and False otherwise
# a leap year is:
# - a year divisible by 4
# - a year that is divisible by 100 and 400
def is_leap_year(year):
pass
# define a function that returns True if the string parameter passed is a palindrome and False otherwise
# the definition of a palindrome is a word is when is the same when you switch the order ie. madam is a palindrome
def is_palindrome(some_string):
pass
# define a function that takes a list of integers and returns the largest of all the values
def mode(values):
pass
def sort_nums(values):
pass
# define a function that takes a list of integers and return the median of the values
# - the median is the middle element when the integers are in sorted order
# - if there is an even number of integers, it is the sum of the 2 middle numbers divided by 2
def median(values):
pass
import exercise
import unittest
class TestIsLeapYear(unittest.TestCase):
def test_2000_is_a_leap_year(self):
self.assertTrue(exercise.is_leap_year(2000), '2000 is a leap year')
def test_1800_is_not_a_leap_year(self):
self.assertIsNotNone(exercise.is_leap_year(1800), 'is_leap_year does not return None')
self.assertFalse(exercise.is_leap_year(1800), '1800 is not a leap year')
def test_2012_is_a_leap_year(self):
self.assertTrue(exercise.is_leap_year(2012), 'is_leap_year does not return None')
self.assertIsNotNone(exercise.is_leap_year(2012), '2012 is a leap year')
class TestIsPalindrome(unittest.TestCase):
def test_madam_is_a_palindrome(self):
self.assertTrue(exercise.is_palindrome('madam'), 'madam which has an odd number of letters is a palindrome')
def test_anna_is_a_palindrome(self):
self.assertTrue(exercise.is_palindrome('anna'), 'anna which has an even number of letters is a palindrome')
def test_hello_is_not_a_palindrome(self):
self.assertIsNotNone(exercise.is_palindrome('hello'), 'is_leap_year does not return None')
self.assertFalse(exercise.is_palindrome('hello'), 'hello is not a palindrome')
class TestDescriptiveStats(unittest.TestCase):
def test_mode_returns_the_largest_value_at_the_end_of_a_list(self):
self.assertEqual(exercise.mode([10, 203, 434]), 434, 'the mode of [10, 203, 434] is 434')
def test_mode_returns_the_largest_value_at_the_start_of_a_list(self):
self.assertEqual(exercise.mode([434, 203, 10]), 434, 'the mode of [434, 203, 10] is 434')
def test_mode_returns_the_largest_value_at_the_middle_of_the_list(self):
self.assertEqual(exercise.mode([434, 203, 505, 23, 100, 10]), 505, 'the mode of [434, 203, 505, 23, 100, 10] is 434')
def test_median_for_list_with_an_even_length(self):
self.assertEqual(exercise.median([434, 200, 505, 23, 100, 10]), 150, 'the median of [434, 200, 505, 23, 100, 10] is 150')
def test_median_for_list_with_an_odd_length(self):
self.assertEqual(exercise.median([434, 200, 505, 100, 10]), 200, 'the median of [434, 200, 505, 100, 10] is 200')
class TestSort(unittest.TestCase):
def test_sort_nums_returns_a_sorted_list(self):
self.assertEqual(
exercise.sort_nums([434, 200, 505, 100, 10]),
[10, 100, 200, 434, 500],
'the sorted list of [434, 200, 505, 100, 10] is [10, 100, 200, 434, 500]'
)
if __name__ == '__main__':
test_loader = unittest.TestLoader()
suites = unittest.TestSuite(map(
lambda test_case: test_loader.loadTestsFromTestCase(test_case),
[TestIsLeapYear, TestIsPalindrome, TestDescriptiveStats])
)
unittest.TextTestRunner().run(suites)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment