- Beautiful Days at the Movies

 


Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse. For instance, given the number , its reverse is . Their difference is . The number  reversed is , and their difference is .

She decides to apply her game to decision making. She will look at a numbered range of days and will only go to a movie on a beautiful day.

Given a range of numbered days,  and a number , determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where  is evenly divisible by . If a day's value is a beautiful number, it is a beautiful day. Return the number of beautiful days in the range.

Function Description

Complete the beautifulDays function in the editor below.

beautifulDays has the following parameter(s):

  • int i: the starting day number
  • int j: the ending day number
  • int k: the divisor

Returns

  • int: the number of beautiful days in the range

Input Format

A single line of three space-separated integers describing the respective values of , and .

Constraints

Sample Input

20 23 6

Sample Output

2

Explanation

Lily may go to the movies on days , and . We perform the following calculations to determine which days are beautiful:

  • Day  is beautiful because the following evaluates to a whole number: 
  • Day  is not beautiful because the following doesn't evaluate to a whole number: 
  • Day  is beautiful because the following evaluates to a whole number: 
  • Day  is not beautiful because the following doesn't evaluate to a whole number: 

Only two days,  and , in this interval are beautiful. Thus, we print  as our answer.

Program:

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the beautifulDays function below.
def beautifulDays(i, j, k):
    count=0
    for l in range(i,j+1):
        m=l
        rev=0
        while(m>0):
            rem=m%10
            rev=rev*10+rem
            m=m//10
        n=(abs(l-rev))
        if (n%k == 0):
            count+=1
    return count

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ijk = input().split()

    i = int(ijk[0])

    j = int(ijk[1])

    k = int(ijk[2])

    result = beautifulDays(i, j, k)

    fptr.write(str(result) + '\n')

    fptr.close()

Output:

Compiler Message
Success
Input (stdin)
  • 20 23 6
Expected Output
  • 2

Post a Comment

Previous Post Next Post