-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 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,29 @@ | ||
# Ranges | ||
###### Language Version (Python 3.6.0) | ||
|
||
First, we must set a couple of variable flags to keep track of the array | ||
sequence and initialize our ```ranges``` list. We'll choose ```begin``` and ```end``` | ||
as sufficient flag names. Next, we iterate through the input array and set the | ||
value of our flags to equal the first element. For every iteration after, if the | ||
next element is in sequence to the previous, we update our `end` flag to equal | ||
the current element. Once, we iterate to an element that breaks the sequence we | ||
append our flag values to our ```ranges``` list and reset flags to the current | ||
element. Finally, after the for loop is complete, make a final comparison of | ||
```begin``` and ```end``` to check if a final range sequence exists. | ||
|
||
### Compile | ||
Run ```tests.py``` to check against unit test | ||
``` | ||
...... | ||
---------------------------------------------------------------------- | ||
Ran 6 tests in 0.001s | ||
OK | ||
``` | ||
|
||
Run ```ranges.py``` to test your own implementation | ||
First input should be the size of your array. Subsequent inputs will be the values | ||
of the array. | ||
|
||
|
||
Unit Test written by: slandau3 |
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,48 @@ | ||
# Ranges | ||
|
||
def ranges(int_list): | ||
""" | ||
Given a sorted list of integers function will return | ||
an array of strings that represent the ranges | ||
""" | ||
begin = 0 | ||
end = 0 | ||
|
||
ranges = [] | ||
|
||
for i in int_list: | ||
# At the start of iteration set the value of | ||
# `begin` and `end` to equal the first element | ||
if begin == 0: | ||
begin = i | ||
end = i | ||
# Set the current element as the value of `end` | ||
# as long as the array is in sequence | ||
elif i-1 == end: | ||
end = i | ||
# Reset flags to current element when iterating through | ||
# multiple integers that are of broken sequence | ||
elif begin == end: | ||
begin = i | ||
end = i | ||
else: | ||
# Sequence of array has been broken, append current range | ||
# to `ranges` and set the value of `begin and `end` flags to | ||
# equal the current element | ||
ranges.append("{0}->{1}".format(begin, end)) | ||
begin = i | ||
end = i | ||
# Grab the last range from the array | ||
if begin != end: | ||
ranges.append("{0}->{1}".format(begin, end)) | ||
|
||
return ranges | ||
|
||
|
||
if __name__ == "__main__": | ||
# Test your own implementation | ||
# Input the size of the array | ||
# Then enter each integer value | ||
size = int(input()) | ||
ints = [int(input()) for _ in range(size)] | ||
print(ranges(ints)) |
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,34 @@ | ||
import unittest | ||
from ranges import ranges | ||
|
||
class TestRanges(unittest.TestCase): | ||
|
||
def test1(self): | ||
""" | ||
Tests a basic input that should only have 1 element in the list | ||
:return: | ||
""" | ||
self.assertEqual(ranges([1,2]), ['1->2']) | ||
|
||
def test2(self): | ||
""" | ||
Tests a case in which the return value should contain no elements | ||
:return: | ||
""" | ||
self.assertEqual(ranges([1]), []) | ||
|
||
def test3(self): | ||
self.assertEqual(ranges([1,2,3,4,5,8,9,10]), ['1->5', '8->10']) | ||
|
||
def test4(self): | ||
self.assertEqual(ranges([5,6,7,20,21,22,25]), ['5->7', '20->22']) | ||
|
||
def test5(self): | ||
self.assertEqual(ranges([5,6,9,10,12,13,15,16,18,19,25,30]), ['5->6', '9->10', '12->13', '15->16', '18->19']) | ||
|
||
def test6(self): | ||
self.assertEqual(ranges([5,6,7,10,15,18,20,21,22]), ['5->7', '20->22']) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |