Skip to content

Commit

Permalink
[Python]Challenge_6(Reviewed) (YearOfProgramming#425)
Browse files Browse the repository at this point in the history
* 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
ajschrier authored and MJUIUC committed Jan 23, 2017
1 parent 98b2a2c commit bea7910
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions challenge_5/python/ajschrier/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Challenge 5 - Find the Difference

*Python Version:* 2.7

Counts all the characters in a string and finds the added letter

## Functions
Expand Down
41 changes: 41 additions & 0 deletions challenge_6/python/ajschrier/IntRange.py
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()
20 changes: 20 additions & 0 deletions challenge_6/python/ajschrier/IntRangeTests.py
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()
16 changes: 16 additions & 0 deletions challenge_6/python/ajschrier/README.md
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.

0 comments on commit bea7910

Please sign in to comment.