Finding the day from a given date

  • Context: Python 
  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Word problem
Click For Summary
SUMMARY

The discussion centers on creating an algorithm to determine the day of the week for a given date in the format YYYYMMDD, starting from the incorrect premise that January 1, 2018, is a Saturday. Participants highlight the need to modify existing algorithms due to this error, emphasizing the importance of understanding the Gregorian calendar's leap year rules. A proposed algorithm involves adjusting the month and year, calculating days based on a base date, and using modular arithmetic to find the day of the week. The conversation also touches on the significance of handling invalid inputs and the implications of calendar changes over time.

PREREQUISITES
  • Understanding of the Gregorian calendar and its leap year rules
  • Familiarity with modular arithmetic
  • Basic algorithm design and implementation skills
  • Experience with date manipulation in programming languages
NEXT STEPS
  • Research "Gregorian calendar leap year rules" for accurate date calculations
  • Learn about "modular arithmetic" and its applications in algorithm design
  • Explore date manipulation libraries in programming languages like Python or Java
  • Study "error handling in algorithms" to manage invalid inputs effectively
USEFUL FOR

Software developers, algorithm designers, and anyone involved in date manipulation or calendar systems will benefit from this discussion.

  • #31
Mark44 said:
Does the notation a[0:4] represent the characters in the string from index 0 through index 4? If so, that's five characters. And similarly in a[6:8], which I believe would be the three characters at indexes 6, 7, and 8.

I believe this is what you want:
year = (int(a[0:3]))
month = (int((a[4:5])))
day = (int(a[6:7]))
The index slice is being used to read the year and month and day from the string as u mentioned. I am now slightly confused to how my slicing produces the desired result for the year as you are right the index [0:3] should read the first four characters of the string '2018' however when I run the index from [0:3] it printed:

Python:
# Input date from the user/ for now just string in put
a = '20180101'# Define variable for the year, month and day and converting integer values
year = (int(a[0:3]))
print(year)
print('')
y = (int(a[0:4]))
print(y)

[CODE title="Output"]201

2018[/CODE]

So now my question is why is it reading in 5 characters for 2018 and not 4?
 
Last edited by a moderator:
Technology news on Phys.org
  • #32
Taylor_1989 said:
The index slice is being used to read the year and month and day from the string as u mentioned. I am now slightly confused to how my slicing produces the desired result for the year as you are right the index [0:3] should read the first four characters of the string '2018' however when I run the index from [0:3] it printed:
That was my mistake in not remembering how Python does things with array slices. Your expression a[0:4] evaluates to the string from a[0] up to but not including a[4].
The portion of the string starting at a[4] up to but not including a[6] gives you the two characters at indexes 4 and 5. And similarly for the last two characters at indexes 6 and 7.

The following runs in Python for an array a with 8 characters:
Python:
year = (int(a[0:4]))
month = (int((a[4:6])))
day = (int(a[6:8]))
 
  • Like
Likes   Reactions: Taylor_1989

Similar threads

Replies
3
Views
2K
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 67 ·
3
Replies
67
Views
16K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
42
Views
8K
  • · Replies 65 ·
3
Replies
65
Views
11K
  • · Replies 4 ·
Replies
4
Views
3K