Table of contents
Howdy fellow programmers! ππ½
In this article, I'll be describing how I solved the
Beautiful Days at the Movies
Challenge on HackerRank using Python 3.
This challenge is part of the Implementation
challenges in the Algorithm
section on HackerRank.
If this is your first time reading my data structures and algorithms article, please consider reading more in my Algos in Plain English series.π
Problem π²
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 12, its reverse is 21. Their difference is 9. The number 120 reversed is 21, and their difference is 99.
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, [i...j]
and a number k, determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where |i - reverse(i)|
is evenly divisible by k. 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 i, j, and k.
Sample Input 0:
20 23 6
Sample Output 0:
2
Explanation:
Lily may go to the movies on days 20, 21, 22, and 23. We perform the following calculations to determine which days are beautiful:
- Day 20 is beautiful because the following evaluates to a whole number: $$ \left|\frac{20 - 02}{6}\right| = \frac{18}{6}\ = 3 $$
- Day 21 is not beautiful because the following doesn't evaluate to a whole number: $$ \left|\frac{21 - 12}{6}\right| = \frac{9}{6}\ = 1.5 $$
- Day 22 is beautiful because the following evaluates to a whole number: $$ \left|\frac{22 - 22}{6}\right| = \frac{0}{6}\ = 0 $$
- Day 23 is not beautiful because the following doesn't evaluate to a whole number: $$ \left|\frac{23 - 32}{6}\right| = \frac{9}{6}\ = 1.5 $$ Only two days, 20 and 23, in this interval are beautiful. Thus, we print 2 as our answer.
Solution π
In plain English, we are expected to get the number of beautiful day numbers from a list of numbered days. A number is beautiful if that number minus its reverse is evenly divisible by another number.
Seems like a modest problem this time so let's get to it.
Let's write down the steps we'll use to solve this problem.
# Loop through all numbers between the starting and ending day number.
# Reverse each day number.
# Check if it's a beautiful day number by applying the conditional logic.
# Store all beautiful day numbers in a beautiful_days array and return its length.
Step 1οΈβ£: Loop through all numbers between the starting and ending day numbers.
- First create an empty
beautiful_days
array to store the beautiful day numbers. - Then use a for loop to get all numbers between the starting day number and the ending day number.
def beautifulDays(i, j, k):
# Write your code here
# create an empty array to store the 'beautiful days'
beautiful_days = []
# Loop through all days between the starting day, i and the ending day, j
for day in range(i, j+1):
Step 2οΈβ£: Reverse each day number.
Here, we'll use slicing to return all elements in the array, reversed.
def beautifulDays(i, j, k):
# Write your code here
# create an empty array to store the 'beautiful days'
beautiful_days = []
# Loop through all days between the starting day, i and the ending day, j
for day in range(i, j+1):
# store a reversed day number in a reversed_day_number variable
# we reverse an integer by first converting it into a string,
# using reverse slicing to return the string of digits in reverse,
# and finally converting this reversed string of numbers back to an integer.
reversed_day_number = int(str(day)[::-1])
Step 3οΈβ£: Get the beautiful day numbers using the conditional logic from the problem statement.
Beautiful numbers are defined as numbers where |i - reverse(i)|
is evenly divisible by k, so we'll simply develop the conditional logic in this step.
For a number to be evenly divisible by a divisor, its remainder must be zero. That being said, the conditional logic goes like this:
def beautifulDays(i, j, k):
# Write your code here
# create an empty array to store the 'beautiful days'
beautiful_days = []
# Loop through all days between the starting day, i and the ending day, j
for day in range(i, j+1):
# store a reversed day number in a reversed_day_number variable
# we reverse an integer by first converting it into a string,
# and then using reverse slicing to return the string of digits in reverse,
# and finally convert these reversed string of numbers to an integer.
reversed_day_number = int(str(day)[::-1])
# from here on out, we can apply the condition to check if its a beautiful
# day or not, then add this day to our beautiful_days array.
if (day - reversed_day_number) % k == 0:
beautiful_days.append(day)
Step 4οΈβ£: Return the number of beautiful day numbers
def beautifulDays(i, j, k):
# Write your code here
# create an empty array to store the 'beautiful days'
beautiful_days = []
# Loop through all days between the starting day, i and the ending day, j
for day in range(i, j+1):
# store a reversed day number in a reversed_day_number variable
# we reverse an integer by first converting it into a string,
# and then using reverse slicing to return the string of digits in reverse,
# and finally convert these reversed string of numbers to an integer.
reversed_day_number = int(str(day)[::-1])
# from here on out, we can apply the condition to check if its a beautiful
# day or not, then add this day to our beautiful_days array.
if (day - reversed_day_number) % k == 0:
beautiful_days.append(day)
# Finally, return the number of 'beautiful days' in this array.
return len(beautiful_days)
beautifulDays(20, 23, 6)
# 2
That's it! Thanks for reading! π
Hope this article helped with understanding this challenge. Happy Coding!π¨π½βπ»
Nonsocchiπ₯·π½