Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # remove_duplicates.py by gauravssnl
- # this script removes duplicate characters from string and sort characters and count number of duplicates.For example : remove_duplicates( "aaabbbac") = ( "abc" , 5)
- def remove_duplicates( InputStr ) :
- # stores duplicates count in dictionary repDict where character will be key and its repitition count will be value
- repDict = { }
- for ch in InputStr :
- # when character ch has been already read & stored in dictionary and is being repeated ,we increase its repititon count by 1
- if repDict.has_key( ch ) :
- repDict[ ch ] +=1
- # when character ch is read for first time , we set its repetition count to 0
- else :
- repDict[ch] = 0
- # print(repDict)
- # use repDict to return sorted string and total number of duplicate characters
- rep_count = sum( repDict.values( ) )
- result_keys = repDict.keys( )
- # sort result_key to sort characters
- result_keys.sort()
- result = ''
- for key in result_keys :
- result += key
- # print(rep_count)
- # print(result)
- return ( result , rep_count)
- if __name__ == "__main__" :
- print (remove_duplicates( 'aaabbbac' ) )
- print (remove_duplicates( 'ccbbaabaabbcef' ) )
- print( remove_duplicates( '' ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement