python
import re def sorted_blocks(input_str): str_usage = {} arr_list = {} upper_case = re.findall('[A-Z]', input_str) lower_case = re.findall('[a-z]', input_str) numbers = re.findall('[0-9]', input_str) upper_case.sort() lower_case.sort() numbers.sort() for dataset in [lower_case, upper_case, numbers]: for item in dataset: if str_usage.get(item) is not None: str_usage[item] += 1 else: str_usage[item] = 1 index = str_usage[item] arr_list.setdefault(index, '') arr_list[index] += item print 'input : "' + input_str + '"' print 'block(s) : "' + '-'.join(arr_list.values()) + '"' print ' ' # tests sorted_blocks('21AxBz') sorted_blocks('21BxAzAasfdaXZas') sorted_blocks('abacad') sorted_blocks('') # output # input : "21AxBz" # block(s) : "xzAB12" # # input : "21BxAzAasfdaXZas" # block(s) : "adfsxzABXZ12-asA-a" # # input : "abacad" # block(s) : "abcd-a-a" # # input : "" # block(s) : ""
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
python