There is this "Don't give me five!" kata on codewars. (https://www.codewars.com/kata/dont-give-me-five/train/python).
Here's what you gotta do: "In this kata you get the start number and the end number of a region and should return the count of all numbers except numbers with a 5 in it. The start and the end number are both inclusive!".
I've solved it in an ugly way, which I'm not posting here. What I'm showing is the solution of one of the users. It looks smooth, but I do not understand why it works.
def dont_give_me_five(start,end):
return sum('5' not in str(i) for i in range(start, end + 1))
I always thought that sum()
sums the values, not the indexes. And if I'm right, "i" should be a value, not an index. But if it is, why does this solution work?