Skip to content

Instantly share code, notes, and snippets.

@andyhd
Created September 24, 2010 08:39
Show Gist options
  • Save andyhd/595058 to your computer and use it in GitHub Desktop.
Save andyhd/595058 to your computer and use it in GitHub Desktop.
# a heavy int is one where the average of the digits is greater than 7
# eg: 8678 is heavy because (8 + 6 + 7 + 8) / 4 = 7.25
# 8677 is not heavy because ( 8 + 6 + 7 + 7) / 4 = 7
def num_heavy_ints(a, b):
"""
Return the number of heavy integers between a and b (inclusive).
Assume that a and b are positive integers.
>>> num_heavy_ints(8675, 8689)
5
"""
start, end = sorted((a, b))
count = 0
for i in range(start, end + 1):
number = i
dlen = 0
dsum = 0
while(number):
dlen += 1
dsum += number % 10
number //= 10
if dsum > dlen * 7:
count += 1
return count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment