Count divisibles in rangedef count_divisibles_in_range(start, end, n):Let us take a breather by tackling a problem simple enough that its solution needs only a couple ofconditional statements and some arithmetic, but not even one loop or anything even more fancy.The difficulty is coming up with the conditions that cover all possible cases of this problem exactlyright, including all of the potentially tricksyedge and corner cases,without beingoff-by-one.Given three integers start, end and n so that start <= end,="" count="" how="" many="" integers="">=>start and end, inclusive, are divisible by n. Sure, you could solve this problem with the listcomprehension one-linerreturn len([x for x in range(start, end+1) if x % n == 0])but of course the automated tester is designed so that anybody trying to solve this problem in sucha blunt fashion will only find themselves running out of both time and space! Your code should haveno loops at all, but use only integer arithmetic and conditional statements to get to the truth. Also,be careful with various edge cases and off-by-one pitfalls lurking in the bushes. Note that eitherstart or end can be negative or zero, but n is guaranteed to be greater than zero.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here