diff --git a/challenge_6/python/sarcodian/src/challenge_6.py b/challenge_6/python/sarcodian/src/challenge_6.py new file mode 100644 index 000000000..7474b9657 --- /dev/null +++ b/challenge_6/python/sarcodian/src/challenge_6.py @@ -0,0 +1,32 @@ +def ranges(int_list): + ''' + int_list: list, containing intergers sorted from low to high + returns: list, each element is the first and last digit of a run + of consecutive numbers with the middle digits replaced by an + arrow. + ''' + temp_list = [] + final_list = [] + + if len(int_list) < 2: + return [] + + for i in range(len(int_list)): + if i == len(int_list)-1: + if int_list[i-1] == int_list[i]-1: + temp_list.append(int_list[i]) + final_list.append(print_range(temp_list)) + elif int_list[i] == int_list[i+1]-1: + temp_list.append(int_list[i]) + else: + temp_list.append(int_list[i]) + final_list.append(print_range(temp_list)) + temp_list.clear() + return final_list + +def print_range(range_list): + ''' + range_list: list, a list of consecutive integers + returns: string, first and last integer with arrow in the middle + ''' + return '{}->{}'.format(range_list[0], range_list[-1]) diff --git a/challenge_6/python/sarcodian/src/challenge_6_tests.py b/challenge_6/python/sarcodian/src/challenge_6_tests.py new file mode 100644 index 000000000..445ec7273 --- /dev/null +++ b/challenge_6/python/sarcodian/src/challenge_6_tests.py @@ -0,0 +1,28 @@ +# we start by importing the unittest module +import unittest + +# Next lets import our function that we intend to do testing on +# +# **We could also import all functions by using * or just import the module +# itself but for now lets just import the function +from challenge_6 import ranges, print_range + +# lets define our suite of tests as a class and lets inherit from unittest.TestCase +class TestBinaryMethods(unittest.TestCase): + + # All tests must begin with the test_* naming scheme otherwise unittest won't find it + def test_positiveIntegers(self): + """ Tests from the READ.me """ + # Testing input with only 1 return value + self.assertEqual(ranges([1, 2]), ["1->2"]) + +# Testing input with no return value + self.assertEqual(ranges([1]), []) + +# Testing input with multiple return values + self.assertEqual(ranges([1,2,3,4,5,8,9,10]), ["1->5", "8->10"]) + +# if the python file is ran by itself run unittest +# This allows us to import the members of the file without running main if we need to +if __name__ == '__main__': + unittest.main() \ No newline at end of file