Last active
May 22, 2016 05:45
-
-
Save louisnow/89b03e2a078b7f1b38beea8e5baf4dc4 to your computer and use it in GitHub Desktop.
Rise Hackathon Week 4 Challenge
This file contains hidden or 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
# Author : Louis Christopher | |
# LICENSE : https://goo.gl/48c5PX | |
# | |
# | |
# | |
# Program to sort a string delimited by any character | |
# Advantage: | |
# - Highly portable, tested on Python 2.7 and 3.5 | |
# - Highly customizable and easy to use. | |
# Instantiate the class with your own defaults | |
# | |
# Example: | |
# | |
# fixlist = sortByTokens(False, ',') | |
# print( fixlist( 'B , b , a , A' ) ) | |
# | |
# ['A', 'B', 'a', 'b'] | |
class sortByTokens(): | |
""" | |
ReturnType : List | |
sortByTokens class takes two arguments during instantiation | |
ignoreCase, set to True by default | |
delimter, set to '>' by default. Must be a single character | |
Usage : sortByTokens()(<string>) | |
""" | |
def __init__(self, ignoreCase=True, delimiter='>'): | |
if type(ignoreCase) is bool: | |
self.ignoreCase = ignoreCase | |
else: | |
raise ValueError('ignoreCase must be a Boolean') | |
if len(delimiter) == 1: | |
self.delimiter = delimiter | |
else: | |
raise ValueError('delimiter must be a single character') | |
def __call__(self, inputString): | |
tokens = inputString.split(self.delimiter) | |
tokens = [x.lower().strip() if self.ignoreCase else x.strip() | |
for x in tokens] | |
uniqueTokens = set(tokens) | |
return sorted(uniqueTokens) | |
fix = sortByTokens() | |
print(fix('a > b > c > d > c > e > f > b')) | |
print(fix('vivek > sid > vignesh > nik > smriti > vivek > prateek > nik')) | |
print(fix('B > b > a > A')) | |
fixlist = sortByTokens(False, ',') | |
print(fixlist('B , b , a , A')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment