Efficient way to join elements under a conditional
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
1
$begingroup$
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
$endgroup$
– Lonidard
Dec 3 '18 at 22:27
add a comment |
$begingroup$
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
$endgroup$
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 '18 at 20:02
Exp ikxExp ikx
1486
1486
1
$begingroup$
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
$endgroup$
– Lonidard
Dec 3 '18 at 22:27
add a comment |
1
$begingroup$
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
$endgroup$
– Lonidard
Dec 3 '18 at 22:27
1
1
$begingroup$
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
$endgroup$
– Lonidard
Dec 3 '18 at 22:27
$begingroup$
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
$endgroup$
– Lonidard
Dec 3 '18 at 22:27
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
$endgroup$
add a comment |
$begingroup$
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}]}
]
$endgroup$
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 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',
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%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
$begingroup$
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
$endgroup$
add a comment |
$begingroup$
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
$endgroup$
add a comment |
$begingroup$
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
$endgroup$
Here is an alternate solution not using If
:
Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]
answered Dec 3 '18 at 21:01
sakrasakra
2,4931428
2,4931428
add a comment |
add a comment |
$begingroup$
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}]}
]
$endgroup$
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:16
add a comment |
$begingroup$
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}]}
]
$endgroup$
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:16
add a comment |
$begingroup$
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}]}
]
$endgroup$
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 '18 at 20:07
Henrik SchumacherHenrik Schumacher
50.7k469145
50.7k469145
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:16
add a comment |
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:16
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Thanks for that tip. But is there still a better way to write the rest of the code? I mean specifically for DayName part.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:12
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
Hm. What does "better" mean? Are you concerned about efficiency? Why? How many years do you want to test this way?
$endgroup$
– Henrik Schumacher
Dec 3 '18 at 20:14
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 at 20:16
$begingroup$
I'm concerned about efficiency since it's a challenge. Nothing much.
$endgroup$
– Exp ikx
Dec 3 '18 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.
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
$begingroup$
As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
$endgroup$
– Lonidard
Dec 3 '18 at 22:27