How to know if today is second or fourth SaturdayTrying to use global variables for referencing directories...
Why avoid shared user accounts?
Nested word series [humans only]
How to deal with an incendiary email that was recalled
Traveling through the asteriod belt?
use of 4/2 chord more compelling than root position?
LuaTex and em dashes
Why did Luke use his left hand to shoot?
Consequences of lack of rigour
What is 6÷2×(1+2) =?
What is the most fuel efficient way out of the Solar System?
Why would space fleets be aligned?
Why are the books in the Game of Thrones citadel library shelved spine inwards?
Quickly creating a sparse array
How to prevent users from executing commands through browser URL
Can we harness gravitational potential energy?
Removing disk while game is suspended
Am I a Rude Number?
Can a Pact of the Blade warlock use the correct existing pact magic weapon so it functions as a "Returning" weapon?
Story about a person invited to join council of intellects
Difference between i++ and (i)++ in C
Why did the villain in the first Men in Black movie care about Earth's Cockroaches?
How does Leonard in "Memento" remember reading and writing?
Use two 8s and two 3s to make the number 24
Can you tell from a blurry photo if focus was too close or too far?
How to know if today is second or fourth Saturday
Trying to use global variables for referencing directories in Python 2.5Finding the date from “2nd Friday of X month”-style inputTDD Hackerrank: Library FinesOrdering an un-ambiguous scrambled dateStart and end times of recurring eventsReading a repetitive file with repetitive codePython library for calculating next and previous time of the cron-like scheduled taskWeb scraping news articles with Beautiful SoupCleaning up date strings in PythonProgram to check if a date is valid or not
$begingroup$
This is working. Is it ok?
import calendar
from datetime import datetime
def second_fourth_saturday(year):
holidays = {}
for month in range(1, 13):
holidays[month] = []
cal = calendar.monthcalendar(year, month)
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
return holidays
if __name__ == "__main__":
today = datetime.today().day
tomonth = datetime.today().month
if today in second_fourth_saturday(2019)[tomonth]:
print("Enjoy")
else:
print("Start Working")
python python-3.x
$endgroup$
add a comment |
$begingroup$
This is working. Is it ok?
import calendar
from datetime import datetime
def second_fourth_saturday(year):
holidays = {}
for month in range(1, 13):
holidays[month] = []
cal = calendar.monthcalendar(year, month)
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
return holidays
if __name__ == "__main__":
today = datetime.today().day
tomonth = datetime.today().month
if today in second_fourth_saturday(2019)[tomonth]:
print("Enjoy")
else:
print("Start Working")
python python-3.x
$endgroup$
$begingroup$
Do you mean that your program takes the current day and returns whether that current day happens to be the second or fourth Saturday of the current month?
$endgroup$
– okcapp
4 hours ago
$begingroup$
Today is the first Saturday of the current month iff it is Saturday and the day is between 1 and 7, inclusive. It is the second Saturday iff it is Saturday and the day is between 8 and 14, inckusive. It is the third Saturday iff it is Saturday and the day is between 15 and 21, inckusive. It is the fourth Saturday iff it is Saturday and the day is between 22 and 28, inckusive.
$endgroup$
– Hagen von Eitzen
55 secs ago
add a comment |
$begingroup$
This is working. Is it ok?
import calendar
from datetime import datetime
def second_fourth_saturday(year):
holidays = {}
for month in range(1, 13):
holidays[month] = []
cal = calendar.monthcalendar(year, month)
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
return holidays
if __name__ == "__main__":
today = datetime.today().day
tomonth = datetime.today().month
if today in second_fourth_saturday(2019)[tomonth]:
print("Enjoy")
else:
print("Start Working")
python python-3.x
$endgroup$
This is working. Is it ok?
import calendar
from datetime import datetime
def second_fourth_saturday(year):
holidays = {}
for month in range(1, 13):
holidays[month] = []
cal = calendar.monthcalendar(year, month)
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
return holidays
if __name__ == "__main__":
today = datetime.today().day
tomonth = datetime.today().month
if today in second_fourth_saturday(2019)[tomonth]:
print("Enjoy")
else:
print("Start Working")
python python-3.x
python python-3.x
edited 4 hours ago
Rahul Patel
asked 5 hours ago
Rahul PatelRahul Patel
232413
232413
$begingroup$
Do you mean that your program takes the current day and returns whether that current day happens to be the second or fourth Saturday of the current month?
$endgroup$
– okcapp
4 hours ago
$begingroup$
Today is the first Saturday of the current month iff it is Saturday and the day is between 1 and 7, inclusive. It is the second Saturday iff it is Saturday and the day is between 8 and 14, inckusive. It is the third Saturday iff it is Saturday and the day is between 15 and 21, inckusive. It is the fourth Saturday iff it is Saturday and the day is between 22 and 28, inckusive.
$endgroup$
– Hagen von Eitzen
55 secs ago
add a comment |
$begingroup$
Do you mean that your program takes the current day and returns whether that current day happens to be the second or fourth Saturday of the current month?
$endgroup$
– okcapp
4 hours ago
$begingroup$
Today is the first Saturday of the current month iff it is Saturday and the day is between 1 and 7, inclusive. It is the second Saturday iff it is Saturday and the day is between 8 and 14, inckusive. It is the third Saturday iff it is Saturday and the day is between 15 and 21, inckusive. It is the fourth Saturday iff it is Saturday and the day is between 22 and 28, inckusive.
$endgroup$
– Hagen von Eitzen
55 secs ago
$begingroup$
Do you mean that your program takes the current day and returns whether that current day happens to be the second or fourth Saturday of the current month?
$endgroup$
– okcapp
4 hours ago
$begingroup$
Do you mean that your program takes the current day and returns whether that current day happens to be the second or fourth Saturday of the current month?
$endgroup$
– okcapp
4 hours ago
$begingroup$
Today is the first Saturday of the current month iff it is Saturday and the day is between 1 and 7, inclusive. It is the second Saturday iff it is Saturday and the day is between 8 and 14, inckusive. It is the third Saturday iff it is Saturday and the day is between 15 and 21, inckusive. It is the fourth Saturday iff it is Saturday and the day is between 22 and 28, inckusive.
$endgroup$
– Hagen von Eitzen
55 secs ago
$begingroup$
Today is the first Saturday of the current month iff it is Saturday and the day is between 1 and 7, inclusive. It is the second Saturday iff it is Saturday and the day is between 8 and 14, inckusive. It is the third Saturday iff it is Saturday and the day is between 15 and 21, inckusive. It is the fourth Saturday iff it is Saturday and the day is between 22 and 28, inckusive.
$endgroup$
– Hagen von Eitzen
55 secs ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
A few suggestions
collections.defaultdict
holidays = {}
for month in range(1, 13):
holidays[month] = []
...
There is a module for dictionaries starting with a basic datatype
from collections import defaultdict
holidays = defaultdict(tuple)
Secondly you first init it as a list, and afterwards you make it a tuple this is odd. Pick one and stick with it
You don't have to calculate all the months only the specific month
Since you already know which month it is now, just calculate only the month you are interested in
To do this you would have to give the month as a second parameter
def second_fourth_saturday(year, month):
cal = calendar.monthcalendar(year, month)
...
Don't Repeat Yourself
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
If you calculate the weeks beforehand you don't have to repeat yourself
second_fourth_saturday = (1, 3) if cal[0][calendar.SATURDAY] else (2, 4)
Return what is asked
Instead of return a dict of month with second/fourth saturdays, I think it would be more clear if the function returns a boolean value if the day is a second or fourth saturday
Code
from calendar import monthcalendar, SATURDAY
from datetime import datetime
def second_fourth_saturday(date):
month_calender = monthcalendar(date.year, date.month)
second_fourth_saturday = (1, 3) if month_calender[0][SATURDAY] else (2, 4)
return any(date.day == month_calender[i][SATURDAY] for i in second_fourth_saturday)
if __name__ == "__main__":
is_second_fourth_saturday = second_fourth_saturday(datetime.today())
print("Enjoy" if is_second_fourth_saturday else "Start working")
$endgroup$
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214443%2fhow-to-know-if-today-is-second-or-fourth-saturday%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
A few suggestions
collections.defaultdict
holidays = {}
for month in range(1, 13):
holidays[month] = []
...
There is a module for dictionaries starting with a basic datatype
from collections import defaultdict
holidays = defaultdict(tuple)
Secondly you first init it as a list, and afterwards you make it a tuple this is odd. Pick one and stick with it
You don't have to calculate all the months only the specific month
Since you already know which month it is now, just calculate only the month you are interested in
To do this you would have to give the month as a second parameter
def second_fourth_saturday(year, month):
cal = calendar.monthcalendar(year, month)
...
Don't Repeat Yourself
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
If you calculate the weeks beforehand you don't have to repeat yourself
second_fourth_saturday = (1, 3) if cal[0][calendar.SATURDAY] else (2, 4)
Return what is asked
Instead of return a dict of month with second/fourth saturdays, I think it would be more clear if the function returns a boolean value if the day is a second or fourth saturday
Code
from calendar import monthcalendar, SATURDAY
from datetime import datetime
def second_fourth_saturday(date):
month_calender = monthcalendar(date.year, date.month)
second_fourth_saturday = (1, 3) if month_calender[0][SATURDAY] else (2, 4)
return any(date.day == month_calender[i][SATURDAY] for i in second_fourth_saturday)
if __name__ == "__main__":
is_second_fourth_saturday = second_fourth_saturday(datetime.today())
print("Enjoy" if is_second_fourth_saturday else "Start working")
$endgroup$
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
add a comment |
$begingroup$
A few suggestions
collections.defaultdict
holidays = {}
for month in range(1, 13):
holidays[month] = []
...
There is a module for dictionaries starting with a basic datatype
from collections import defaultdict
holidays = defaultdict(tuple)
Secondly you first init it as a list, and afterwards you make it a tuple this is odd. Pick one and stick with it
You don't have to calculate all the months only the specific month
Since you already know which month it is now, just calculate only the month you are interested in
To do this you would have to give the month as a second parameter
def second_fourth_saturday(year, month):
cal = calendar.monthcalendar(year, month)
...
Don't Repeat Yourself
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
If you calculate the weeks beforehand you don't have to repeat yourself
second_fourth_saturday = (1, 3) if cal[0][calendar.SATURDAY] else (2, 4)
Return what is asked
Instead of return a dict of month with second/fourth saturdays, I think it would be more clear if the function returns a boolean value if the day is a second or fourth saturday
Code
from calendar import monthcalendar, SATURDAY
from datetime import datetime
def second_fourth_saturday(date):
month_calender = monthcalendar(date.year, date.month)
second_fourth_saturday = (1, 3) if month_calender[0][SATURDAY] else (2, 4)
return any(date.day == month_calender[i][SATURDAY] for i in second_fourth_saturday)
if __name__ == "__main__":
is_second_fourth_saturday = second_fourth_saturday(datetime.today())
print("Enjoy" if is_second_fourth_saturday else "Start working")
$endgroup$
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
add a comment |
$begingroup$
A few suggestions
collections.defaultdict
holidays = {}
for month in range(1, 13):
holidays[month] = []
...
There is a module for dictionaries starting with a basic datatype
from collections import defaultdict
holidays = defaultdict(tuple)
Secondly you first init it as a list, and afterwards you make it a tuple this is odd. Pick one and stick with it
You don't have to calculate all the months only the specific month
Since you already know which month it is now, just calculate only the month you are interested in
To do this you would have to give the month as a second parameter
def second_fourth_saturday(year, month):
cal = calendar.monthcalendar(year, month)
...
Don't Repeat Yourself
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
If you calculate the weeks beforehand you don't have to repeat yourself
second_fourth_saturday = (1, 3) if cal[0][calendar.SATURDAY] else (2, 4)
Return what is asked
Instead of return a dict of month with second/fourth saturdays, I think it would be more clear if the function returns a boolean value if the day is a second or fourth saturday
Code
from calendar import monthcalendar, SATURDAY
from datetime import datetime
def second_fourth_saturday(date):
month_calender = monthcalendar(date.year, date.month)
second_fourth_saturday = (1, 3) if month_calender[0][SATURDAY] else (2, 4)
return any(date.day == month_calender[i][SATURDAY] for i in second_fourth_saturday)
if __name__ == "__main__":
is_second_fourth_saturday = second_fourth_saturday(datetime.today())
print("Enjoy" if is_second_fourth_saturday else "Start working")
$endgroup$
A few suggestions
collections.defaultdict
holidays = {}
for month in range(1, 13):
holidays[month] = []
...
There is a module for dictionaries starting with a basic datatype
from collections import defaultdict
holidays = defaultdict(tuple)
Secondly you first init it as a list, and afterwards you make it a tuple this is odd. Pick one and stick with it
You don't have to calculate all the months only the specific month
Since you already know which month it is now, just calculate only the month you are interested in
To do this you would have to give the month as a second parameter
def second_fourth_saturday(year, month):
cal = calendar.monthcalendar(year, month)
...
Don't Repeat Yourself
if cal[0][calendar.SATURDAY]:
holidays[month] = (
cal[1][calendar.SATURDAY],
cal[3][calendar.SATURDAY]
)
else:
holidays[month] = (
cal[2][calendar.SATURDAY],
cal[4][calendar.SATURDAY]
)
If you calculate the weeks beforehand you don't have to repeat yourself
second_fourth_saturday = (1, 3) if cal[0][calendar.SATURDAY] else (2, 4)
Return what is asked
Instead of return a dict of month with second/fourth saturdays, I think it would be more clear if the function returns a boolean value if the day is a second or fourth saturday
Code
from calendar import monthcalendar, SATURDAY
from datetime import datetime
def second_fourth_saturday(date):
month_calender = monthcalendar(date.year, date.month)
second_fourth_saturday = (1, 3) if month_calender[0][SATURDAY] else (2, 4)
return any(date.day == month_calender[i][SATURDAY] for i in second_fourth_saturday)
if __name__ == "__main__":
is_second_fourth_saturday = second_fourth_saturday(datetime.today())
print("Enjoy" if is_second_fourth_saturday else "Start working")
answered 2 hours ago
LudisposedLudisposed
8,19222161
8,19222161
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
add a comment |
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
$begingroup$
Really useful. Thanks.
$endgroup$
– Rahul Patel
1 hour ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214443%2fhow-to-know-if-today-is-second-or-fourth-saturday%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Do you mean that your program takes the current day and returns whether that current day happens to be the second or fourth Saturday of the current month?
$endgroup$
– okcapp
4 hours ago
$begingroup$
Today is the first Saturday of the current month iff it is Saturday and the day is between 1 and 7, inclusive. It is the second Saturday iff it is Saturday and the day is between 8 and 14, inckusive. It is the third Saturday iff it is Saturday and the day is between 15 and 21, inckusive. It is the fourth Saturday iff it is Saturday and the day is between 22 and 28, inckusive.
$endgroup$
– Hagen von Eitzen
55 secs ago