Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Usage: python digitcount.py <argument>
- # This program counts the number of digits in <argument>, per digit, and then spits out the results sorted by digit, then by frequency
- from sys import argv
- def aswap(array, a, b):
- temp = array[a]
- array[a] = array[b]
- array[b] = temp
- def sync_sort(array, array2):
- alen = len(array)
- for i in range(alen):
- swapped = False
- for j in range(alen - 1, i, -1):
- if array[j] < array[j - 1]:
- aswap(array, j, j-1)
- aswap(array2, j, j-1)
- swapped = True
- if not swapped:
- break
- return array
- def count(string):
- digits = [0 for i in range(10)]
- for char in string:
- digits[int(char)] += 1
- return digits
- def report(digits, digit_nums):
- for i in range(10):
- print "%i: %i" % (digit_nums[i], digits[i])
- digits = count(argv[1])
- digit_nums = [i for i in range(10)]
- print "By digit:"
- report(digits, digit_nums)
- print "By frequency:"
- sync_sort(digits, digit_nums)
- digits.reverse()
- digit_nums.reverse()
- report(digits, digit_nums)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement