Skip to content

Commit

Permalink
Merge pull request YearOfProgramming#360 from sarcodian/python_6 revi…
Browse files Browse the repository at this point in the history
…ewed by ningyuansg

[Python] Challenge 6 (reviewed and merged)
  • Loading branch information
Steven Landau committed Jan 23, 2017
2 parents 0009821 + 671eea0 commit cfff9aa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions challenge_6/python/sarcodian/src/challenge_6.py
Original file line number Diff line number Diff line change
@@ -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])
28 changes: 28 additions & 0 deletions challenge_6/python/sarcodian/src/challenge_6_tests.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit cfff9aa

Please sign in to comment.