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









share|improve this question






















  • As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
    – Lonidard
    5 hours ago















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









share|improve this question






















  • As an aside: leap years are not years which are multiples of 4, the definition is slightly more complex
    – Lonidard
    5 hours ago













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









share|improve this question













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


















  • 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










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





share|improve this answer




























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





    share|improve this answer





















    • 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











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


    }
    });














    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








    up vote
    4
    down vote













    Here is an alternate solution not using If:



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





    share|improve this answer

























      up vote
      4
      down vote













      Here is an alternate solution not using If:



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





      share|improve this answer























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





        share|improve this answer












        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 7 hours ago









        sakra

        2,4381328




        2,4381328






















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





            share|improve this answer





















            • 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















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





            share|improve this answer





















            • 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













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





            share|improve this answer












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


















            • 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


















            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.





            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.




            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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks