Skip to content

Commit

Permalink
[Python] Challenge 6 (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Botello authored and MJUIUC committed Jan 25, 2017
1 parent 6b923f6 commit b69d6ca
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
29 changes: 29 additions & 0 deletions challenge_6/python/alexbotello/README.md
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
48 changes: 48 additions & 0 deletions challenge_6/python/alexbotello/src/ranges.py
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))
34 changes: 34 additions & 0 deletions challenge_6/python/alexbotello/src/tests.py
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()

0 comments on commit b69d6ca

Please sign in to comment.