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
1
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
Dec 3 at 22:27
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 Dec 3 at 20:02
Exp ikx
1436
1436
1
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
Dec 3 at 22:27
add a comment |
1
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
Dec 3 at 22:27
1
1
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
Dec 3 at 22:27
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
Dec 3 at 22:27
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
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.
– Exp ikx
Dec 3 at 20:12
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
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.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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',
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%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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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
accepted
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
accepted
up vote
4
down vote
accepted
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 Dec 3 at 21:01
sakra
2,4781328
2,4781328
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.
– Exp ikx
Dec 3 at 20:12
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
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.
– Exp ikx
Dec 3 at 20:12
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
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 Dec 3 at 20:07
Henrik Schumacher
47.4k466134
47.4k466134
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– Exp ikx
Dec 3 at 20:12
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
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.
– Exp ikx
Dec 3 at 20:12
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– Exp ikx
Dec 3 at 20:12
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
– Exp ikx
Dec 3 at 20:12
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
– Henrik Schumacher
Dec 3 at 20:14
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
I'm concerned about efficiency since it's a challenge. Nothing much.
– Exp ikx
Dec 3 at 20:16
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
1
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
– Lonidard
Dec 3 at 22:27