From 3236b39bbedfe5389c5d97b0b1df0071b9eaf91b Mon Sep 17 00:00:00 2001 From: Alex Botello Date: Mon, 23 Jan 2017 17:46:24 -0600 Subject: [PATCH] [Python] Challenge 6 --- challenge_6/python/alexbotello/README.md | 29 ++++++++++++ challenge_6/python/alexbotello/src/ranges.py | 48 ++++++++++++++++++++ challenge_6/python/alexbotello/src/tests.py | 34 ++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 challenge_6/python/alexbotello/README.md create mode 100644 challenge_6/python/alexbotello/src/ranges.py create mode 100644 challenge_6/python/alexbotello/src/tests.py diff --git a/challenge_6/python/alexbotello/README.md b/challenge_6/python/alexbotello/README.md new file mode 100644 index 000000000..9c363c378 --- /dev/null +++ b/challenge_6/python/alexbotello/README.md @@ -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 diff --git a/challenge_6/python/alexbotello/src/ranges.py b/challenge_6/python/alexbotello/src/ranges.py new file mode 100644 index 000000000..fd0dd9be1 --- /dev/null +++ b/challenge_6/python/alexbotello/src/ranges.py @@ -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)) diff --git a/challenge_6/python/alexbotello/src/tests.py b/challenge_6/python/alexbotello/src/tests.py new file mode 100644 index 000000000..8f36c2592 --- /dev/null +++ b/challenge_6/python/alexbotello/src/tests.py @@ -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()