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.
[Python]Challenge_6(Reviewed) (YearOfProgramming#425)
* Challenge_3 in Python * Challenge_3 in Python * Started challenge_4 * Challenge_4 in Python * Challenge_5 in Python * Challenge 6 in Python * Fixed loop to prevent missing 0 value * Added Python version number to README
- Loading branch information
Showing
4 changed files
with
79 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
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,41 @@ | ||
ARROW = '->' | ||
|
||
|
||
def outputRange(inputList): | ||
# Helper Vars | ||
outputList = [] | ||
rangeStart = None | ||
rangeEnd = None | ||
|
||
for i in inputList: | ||
# Detect the beginning of the list | ||
if rangeStart is None: | ||
rangeStart = i | ||
rangeEnd = i | ||
# Check for invalid sequence | ||
elif rangeEnd is not i-1: | ||
# If vars are equal, no sequence is being terminated | ||
if rangeStart is rangeEnd: | ||
rangeStart, rangeEnd = i, i | ||
# If they're not, add the terminated sequence to the list | ||
# and continue | ||
else: | ||
outputList.append('{}{}{}'.format(rangeStart, ARROW, rangeEnd)) | ||
rangeStart, rangeEnd = i, i | ||
# If none of the above, continue the current sequence | ||
else: | ||
rangeEnd = i | ||
|
||
# Check to see if a sequence is in progress at the end of the loop | ||
if rangeStart is not rangeEnd: | ||
outputList.append('{}{}{}'.format(rangeStart, ARROW, rangeEnd)) | ||
return outputList | ||
|
||
|
||
def main(): | ||
print outputRange([1, 2, 3, 4, 8, 9, 10, 12, 13, 14]) | ||
print outputRange([1, 2, 3, 4, 5, 8, 9, 10]) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,20 @@ | ||
from IntRange import outputRange | ||
import unittest | ||
|
||
|
||
class outputRangeTests(unittest.TestCase): | ||
"""test outputRange Function""" | ||
def test1(self): | ||
self.assertEqual(outputRange([1, 2, 3, 4, 8, 9, 10, 12, 13, 14]), | ||
["1->4", "8->10", "12->14"]) | ||
|
||
def test2(self): | ||
self.assertEqual(outputRange([1, 2, 3, 4, 5, 8, 9, 10]), | ||
["1->5", "8->10"]) | ||
|
||
def test3(self): | ||
self.assertEqual(outputRange([]), | ||
[]) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,16 @@ | ||
# Challenge 6 - Integer Range | ||
|
||
*Python Version:* 2.7 | ||
|
||
Finds ranges of integers from an ordered integer list. | ||
|
||
## Functions | ||
|
||
### outputRange | ||
|
||
**Input:** list(integer) | ||
**Output:**list(string) | ||
|
||
Checks the list for sequences. | ||
|
||
Starts by maintaining the current sequence. If there is a continuation found, store the new end of the sequence. Add the sequence to the output list after continuation breaks, skipping cases where the current sequence is length 1. Return the list of sequences, or an empty list if no sequences exist. |