defblocks(str):# const - reference for extracting blocks
order="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"# count occurences of characters in str
mapping={}# build mapping
forsinstr:ifsnotinmapping:mapping[s]=0mapping[s]+=1# iterate through mapping to create each block, decrementing the counts
# with each pass. when the count is 0, delete the key,value pair.
# stop iterating when there are no more values to create blocks from
blocks=[]whilemapping.values():block=[]foroinorder:ifoinmapping:block.append(o)mapping[o]-=1ifmapping[o]==0:delmapping[o]blocks.append(''.join(block))return'-'.join(blocks)print(blocks("21AxBz"),"xzAB12")print(blocks("abacad"),"abcd-a-a")print(blocks(""),"")print(blocks("21BxAzAasfdaXZas"),"adfsxzABXZ12-asA-a")
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.
in python