Beautiful Days at the Movies : HackerRank Solution in Python

Solution in Python 3

🔗BDAM (github.com)

def beautifulDays(i, j, k):
beautifulDays = 0
for day in range(i, j+1):
rev = int(str(day)[::-1])
if abs(rev-day)%k==0:
beautifulDays+=1

return beautifulDays

Explanation

Firstly we declare a variable called beautifulDays and initialize it to 0.

beautifulDays = 0

Then, we use a for loop and iterate from i i.e the starting the day number to j i.e the ending day number using a variable called day.

for day in range(i, j+1):

Inside the for loop, We first find the reverse of the number, We do so by converting day which is an integer into a string and then by indexing we reverse the string and convert it back to an integer. We store the reverse of day in a variable called rev.

rev = int(str(day)[::-1])

def beautifulDays(i, j, k):
beautifulDays = 0
for day in range(i, j+1):
rev = int(str(day)[::-1])

Then, we use an if block to check if the subtraction of day and rev is evenly divisible by k.

If the modulus of absolute value of (day-rev) and k is equal to 0 then it is a beautiful day and thus we increment beautifulDays by 1.

if abs(rev-day)%k==0:
beautifulDays+=1

At the end of our program, we return beautifulDays.

return beautifulDays

--

--

--

pretending to teach is one of the best way to learn.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A journey from callback hell to Kotlin coroutines, Episode 1

Enterprise Software: What Does A Client Value Most

ChatOps with Python and Microsoft Teams

My Journey in DLithe Bootcamp .NET Full Stack.Week 1

AITrading weekly updates | 2

Character Encodings

PDF Download> Introduction to Programming with C++ (3rd Edition) (Myprogramminglab) Read @book…

Cloud-Native vs Cloud-Enabled? Why Cloud-Native Applications are the Future?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shounak Lohokare

Shounak Lohokare

pretending to teach is one of the best way to learn.

More from Medium

My Experience as an international master student in Indiana University — After 1 year.

Creative coding: how to fall in love with IT

A sunset in the style of caspar david friedrich, but with weird symmetrical features known from AI generated art.

HOWTO: Remove diacritics (accents) from a text in Python

How Web Scraping is Used to Extract Amazon Prime Data Using Selenium And Beautifulsoup?