Change language

Python program to find your date of birth on the same day you were born

Examples :

Input: 1996-11-12
Output: [ ’1996-11-12’, ’2002-11-12’, ’2013-11-12’, ’2019-11-12’, ’2024-11-12’, ’2030-11-12’, ’2041 -11-12 ’,’ 2047-11-12 ’]

Input: 1992-11-2
Output: [’ 1992-11-2 ’,’ 1998-11-2 ’,’ 2009-11-2 ’,’ 2015-11-2 ’,’ 2020-11-2 ’,’ 2026-11-2 ’,’ 2037- 11-2 ’,’ 2043-11-2 ’,’ 2048-11-2 ’]

Recommended: please , try your approach on first before moving on to the solution.

Functions generated:

  • split_date (birthdate): this function splits the user-supplied date into year, month and day.
  • get_birthday (birthdate): this the function is used to return the day of the week the user was born.
  • true_birthdays (date of birth): this function is used to return a list of dates that have the same day of the week the user was born.

There are three methods to find birth dates that have the same day the user was born above. First, the user split_date () is the date, and split_date () will split the date into year, month and day. The get_birthday () function will then be used to find the day of the week for that particular date. Finally, true_birthdays () will be used to find a list of all dates that have the same day of the week. Inside this function, the for loop will iterate from year of birth to a specific year and will check if the date of birth is in any specific year with the same day of the week or not. If the day of the week is the same, then that date will be added to the list of dates.

Below is the implementation.

import datetime

import calendar

 

weekdays = [ "Monday" , "Tuesday" , "Wednesday"

"Thursday" , "Friday" , "Saturday" , "Sunday" ]

 

  
# get_birth day

def split_date ( birthday):

 

# Divide it by year, month and day

year, month, day = birthday.split ( ’-’

return year, month, day

  

  

def get_birthday (birthday):

 

 

year, month, day = split_date (birthday)

 

# Get the object’s birthday date

bdate = datetime.datetime ( int (year), int (month), int (day))

 

# Get the whole day of the week for the birthday

  weekday = bdate .weekday ()

 

# Tell the user

day = weekdays [weekday]

 

return day 

 

 

def listToString (x):

 

  # initialize empty string

  String = " " 

  < / p>

# returned string

return (String.join (x))

 

  

def true_birthdays (birthdate):

year, month, day = split_date (birthdate)

 

# get the year from birthday

  year = birthdate [: 4 ]. split ( ’-’ )

 

# convert list to string

year = listToString (year)

 

# get weekday birthday

d_day = get_birthday (birthdate) 

 

# list of true date of birth [birthday with the same

# weekday as your birthday]

true_BD = [] 

 

j = 0

  

  for i in range ( int (year), 2050 ):

 

# add + j to the year of birth

new_year = int (year) +

 

  # build a new birthday

new_birthday = str ( str (new_year) + "-" + month + "-" + day) 

 

# get weekday new birthday

new_d_day = get_birthday (new_birthday)

 

# if birthday has a day of the week

# like the day you were born

if d_day = = new_d_day: 

 

  # add true date of birth to list

true_BD.append (new_birthday)

else :

pass

j + = 1

 

return true_BD

  

  

def main ():

 

# Get date of birth

birthdate = "1996-11-12"

 

# year_limit = input (& quot; search limit since your birthday- & quot;)

  dates < code class = "keyword"> = true_birthdays (birthdate) 

 

print (dates)

 

 
# Code driver
main ()

Exit:

[’1996-11-12’, ’2002-11-12’, ’2013-11-12 ’,’ 2019-11-12 ’,’ 2024-11-12 ’,’ 2030-11-12 ’,’ 2041-11-12 ’,’ 2047-11-12 ’]

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically