In [44]:
def dct_make(n):
"""Takes a string and loops through each character, counting and adding them to a dictionary
Parameters
----------
Input:
n: Str
A sequence of characters
Output:
dct: Dictionary
A dictionary of letters and their counts
"""
dct = {}
for elem in n:
dct[elem] = dct.get(elem,0) + 1 #Check each character, if it isnt in the dictionary then create a key and count 1
return dct
def conv_int(dct):
"""Takes a dictionary of characters and counts and converts the counts(integers) into strings
Parameters
----------
Input:
dct: Dictionary
A dictionary of string letter keys ('a','b','c') and integer values (1,2,3)
Output:
dct1: Dictionary
A dictionary of string letter keys and string values e.g. {'a':'1','c':'7','b':'23'}
"""
dct1 = {k:str(v) for k,v in dct.items()} #Loop through the values associated with each Key,val pair and convert them to integers
return dct1
def str_prep(dct1):
"""Take an empty string and appends to it keys and values from a dictionary
Parameters
----------
Input:
dct1: Dictionary
A dictionary of string characters and numbers
Output:
final_string: Str
A sequence of alternating alphabetic characters followwed by their corresponding numerical counts
e.g. 'a4b1f3'
"""
final_string = ""
for elem in sorted(dct1):
final_string += '%s%s' % (elem, dct1[elem]) #Loop through elements in a sorted list made from
return final_string #the dictionary and append the keys followed by values to an empty string
def compress(n):
"""Takes a string and returns the letters found in it and their counts if the length of the outputted string is
less than the length of the original string
e.g. 'aaabbbccc' ---> 'a3b3c3'
'aabb' ---> 'aabb'
Parameters
----------
Input:
n:Str
A sequence of characters
Output:
final_string: Str
A sequence of characters composed of letters and numerical counts
"""
dct = dct_make(n)
dct1 = conv_int(dct)
final_string = str_prep(dct1)
if len(final_string) < len(n):
return final_string
else:
return n
In [45]:
compress('aaabbbbcc')
Out[45]:
In [50]:
#assert compress(None) == None
assert compress('aabb') == 'aabb'
assert compress('aaaaagggggggeeeee') == 'a5e5g7'
print('Compress tests passed')
In [ ]:
In [ ]: