forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#360 from sarcodian/python_6 revi…
…ewed by ningyuansg [Python] Challenge 6 (reviewed and merged)
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |