# 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'))