Efficient way to join elements under a conditional












2












$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}]}]









share|improve this question









$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
















2












$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}]}]









share|improve this question









$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














2












2








2





$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}]}]









share|improve this question









$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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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










2 Answers
2






active

oldest

votes


















4












$begingroup$

Here is an alternate solution not using If:



Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]





share|improve this answer









$endgroup$





















    2












    $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}]}
    ]





    share|improve this answer









    $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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    4












    $begingroup$

    Here is an alternate solution not using If:



    Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]





    share|improve this answer









    $endgroup$


















      4












      $begingroup$

      Here is an alternate solution not using If:



      Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]





      share|improve this answer









      $endgroup$
















        4












        4








        4





        $begingroup$

        Here is an alternate solution not using If:



        Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]





        share|improve this answer









        $endgroup$



        Here is an alternate solution not using If:



        Table[ DayName[ {year, 1, x} ], {x, 1 + Boole[ LeapYearQ[{year} ]]} ]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 3 '18 at 21:01









        sakrasakra

        2,4931428




        2,4931428























            2












            $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}]}
            ]





            share|improve this answer









            $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
















            2












            $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}]}
            ]





            share|improve this answer









            $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














            2












            2








            2





            $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}]}
            ]





            share|improve this answer









            $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}]}
            ]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • $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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Probability when a professor distributes a quiz and homework assignment to a class of n students.

            Aardman Animations

            Are they similar matrix