Efficient way to join elements under a conditional
up vote
2
down vote
favorite
I am solving a certain challenge given by my friend.
I want to print the first day of the year if it is not a leap year, while I want both first and second days if it is a leap year.
I want to know if there is an efficient way to rewrite this piece of code:
If[Mod[year,4]==0,DayName/@{{year,1,1},{year,1,2}},{DayName[{year,1,1}]}]
list-manipulation
add a comment |
up vote
2
down vote
favorite
I am solving a certain challenge given by my friend.
I want to print the first day of the year if it is not a leap year, while I want both first and second days if it is a leap year.
I want to know if there is an efficient way to rewrite this piece of code:
If[Mod[year,4]==0,DayName/@{{year,1,1},{year,1,2}},{DayName[{year,1,1}]}]
list-manipulation
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
5 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am solving a certain challenge given by my friend.
I want to print the first day of the year if it is not a leap year, while I want both first and second days if it is a leap year.
I want to know if there is an efficient way to rewrite this piece of code:
If[Mod[year,4]==0,DayName/@{{year,1,1},{year,1,2}},{DayName[{year,1,1}]}]
list-manipulation
I am solving a certain challenge given by my friend.
I want to print the first day of the year if it is not a leap year, while I want both first and second days if it is a leap year.
I want to know if there is an efficient way to rewrite this piece of code:
If[Mod[year,4]==0,DayName/@{{year,1,1},{year,1,2}},{DayName[{year,1,1}]}]
list-manipulation
list-manipulation
asked 8 hours ago
JustCurious
1264
1264
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
5 hours ago
add a comment |
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
5 hours ago
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
5 hours ago
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
5 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
add a comment |
up vote
2
down vote
LeapYearQ
should be more reliable, in particular since special rules apply if the year is divisible by 100 or 400.
f = year [Function] If[
LeapYearQ[{year, 1, 1}],
DayName /@ {{year, 1, 1}, {year, 1, 2}},
{DayName[{year, 1, 1}]}
]
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
add a comment |
up vote
4
down vote
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
add a comment |
up vote
4
down vote
up vote
4
down vote
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
answered 7 hours ago
sakra
2,4381328
2,4381328
add a comment |
add a comment |
up vote
2
down vote
LeapYearQ
should be more reliable, in particular since special rules apply if the year is divisible by 100 or 400.
f = year [Function] If[
LeapYearQ[{year, 1, 1}],
DayName /@ {{year, 1, 1}, {year, 1, 2}},
{DayName[{year, 1, 1}]}
]
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
add a comment |
up vote
2
down vote
LeapYearQ
should be more reliable, in particular since special rules apply if the year is divisible by 100 or 400.
f = year [Function] If[
LeapYearQ[{year, 1, 1}],
DayName /@ {{year, 1, 1}, {year, 1, 2}},
{DayName[{year, 1, 1}]}
]
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
add a comment |
up vote
2
down vote
up vote
2
down vote
LeapYearQ
should be more reliable, in particular since special rules apply if the year is divisible by 100 or 400.
f = year [Function] If[
LeapYearQ[{year, 1, 1}],
DayName /@ {{year, 1, 1}, {year, 1, 2}},
{DayName[{year, 1, 1}]}
]
LeapYearQ
should be more reliable, in particular since special rules apply if the year is divisible by 100 or 400.
f = year [Function] If[
LeapYearQ[{year, 1, 1}],
DayName /@ {{year, 1, 1}, {year, 1, 2}},
{DayName[{year, 1, 1}]}
]
answered 7 hours ago
Henrik Schumacher
46.4k466133
46.4k466133
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
add a comment |
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– JustCurious
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
I'm concerned about efficiency since it's a challenge. Nothing much.
– JustCurious
7 hours ago
add a comment |
Thanks for contributing an answer to Mathematica 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fmathematica.stackexchange.com%2fquestions%2f187250%2fefficient-way-to-join-elements-under-a-conditional%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
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
5 hours ago