9/28/2020

Python expiation

 Hello!

Today's question is this!

The 2nd of February 2020 is a palindromic date in both dd/mm/yyyy and mm/dd/yyyy format (02/02/2020). Given a date in dd/mm/yyyy format, return True if the date is palindromicin both date formats, otherwise return False.

From : Palindromic Dates // Edabit 

The code is this!

def palindromic_date(date: str) -> bool:
no_slash = date.replace("/", "")
reversed_string = ""
splitted_date = date.split("/")
rearrenged_date = splitted_date[1] + splitted_date[0] + splitted_date[2]
for s in no_slash:
reversed_string = s + reversed_string
if no_slash == reversed_string and rearrenged_date == reversed_string:
return True
else:
return False

So, first, let's see the no_slash variable.

This variable replaces slash(/) to an empty string.

Then, I made a variable reversed string and make a for loop.

In the for loop, we will reverse the date and appending to reversed string.

And then, look at it if the reversed string is same with no slash.

Then, this is the British date but the American date is mm/dd/yyyy format.

I splited the date when the / is not gone.

And we will rearrenge date(which means make it to mm/dd/yyyy format.)

Lastly, we will see if the rearrenged date is same with the reversed string.

Easy code!


So, See ya!




0 件のコメント:

Super Leo 4!

  Leo : Let's do this! Reimu : I'll kill those monsters, so you go! Leo : Yeah! I'll go to the goal!  Leo : Let's go〜! ? Wai...