Created
October 20, 2018 07:36
-
-
Save KaiserKatze/9140eb4637dfdf689a949159abb66b84 to your computer and use it in GitHub Desktop.
Simple digest algorithm demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import itertools | |
| def simple_digest(input_string): | |
| chars = {} | |
| for c in input_string: | |
| chars[c] = chars.get(c, 0) + 1 | |
| items = iter(sorted(chars.items())) | |
| pairs = map(lambda pair: [str(pair[1]), pair[0],], items) | |
| result = itertools.chain.from_iterable(pairs) | |
| result = ''.join(result) | |
| return result | |
| if __name__ == '__main__': | |
| input_string = 'KNYSDFDJBYSDFDJYQ' | |
| result = simple_digest(input_string) | |
| print(result) | |
| # Expected output: | |
| assert(result == '1B4D2F2J1K1N1Q2S3Y') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment