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

Discussion Overview

The discussion revolves around developing an algorithm to determine the day of the week for a given date, using a specific starting point of January 1st, 2018, which is claimed to be a Saturday. Participants explore various algorithms, their limitations, and the implications of the incorrect starting day.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant questions the validity of using existing algorithms due to the incorrect assertion that January 1st, 2018 is a Saturday, suggesting that this necessitates modifications for the specific case of that year.
  • Another participant proposes that treating the incorrect day as a Saturday merely shifts the week by two days, implying a simpler adjustment.
  • A different participant suggests an algorithm based on an old HP calculator, detailing a method that involves adjusting the month and using a base date for calibration.
  • Concerns are raised about the algorithm's accuracy regarding leap years and the potential issues with floating-point arithmetic.
  • Some participants discuss the finite upper bounds of validity for any algorithm, emphasizing the need for range checks on input dates.
  • There is mention of historical context regarding the Gregorian calendar and its adoption, which could affect calculations over long periods.
  • One participant reflects on the purpose of the question as a test of the ability to handle invalid inputs and the importance of asking for clarification on requirements.
  • Another participant notes that the challenge of the question lies in modifying existing algorithms rather than simply copying them, suggesting that this tests deeper understanding.

Areas of Agreement / Disagreement

Participants express differing views on the implications of the incorrect starting day, the effectiveness of various algorithms, and the historical context of calendar systems. No consensus is reached on a single approach or solution.

Contextual Notes

Limitations include the potential inaccuracies in handling leap years, the reliance on floating-point arithmetic, and the historical complexities of calendar systems that may affect the validity of the algorithms over time.

  • #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
4K