Alarm clock printing











up vote
5
down vote

favorite












I know there must be an easier way to write this but I'm stuck in over-complicating mindset instead of just following the Zen of Python. Please help me simplify this.




Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
a boolean indicating if we are on vacation, return a string of the
form "7:00" indicating when the alarm clock should ring. Weekdays, the
alarm should be "7:00" and on the weekend it should be "10:00". Unless
we are on vacation -- then on weekdays it should be "10:00" and
weekends it should be "off".



alarm_clock(1, False) → '7:00'
alarm_clock(5, False) → '7:00'
alarm_clock(0, False) → '10:00'



def alarm_clock(day, vacation):

weekend = "06"
weekdays = "12345"
if vacation:
if str(day) in weekend:
return "off"
else:
return "10:00"
else:
if str(day) in weekend:
return "10:00"
else:
return "7:00"









share|improve this question




























    up vote
    5
    down vote

    favorite












    I know there must be an easier way to write this but I'm stuck in over-complicating mindset instead of just following the Zen of Python. Please help me simplify this.




    Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
    a boolean indicating if we are on vacation, return a string of the
    form "7:00" indicating when the alarm clock should ring. Weekdays, the
    alarm should be "7:00" and on the weekend it should be "10:00". Unless
    we are on vacation -- then on weekdays it should be "10:00" and
    weekends it should be "off".



    alarm_clock(1, False) → '7:00'
    alarm_clock(5, False) → '7:00'
    alarm_clock(0, False) → '10:00'



    def alarm_clock(day, vacation):

    weekend = "06"
    weekdays = "12345"
    if vacation:
    if str(day) in weekend:
    return "off"
    else:
    return "10:00"
    else:
    if str(day) in weekend:
    return "10:00"
    else:
    return "7:00"









    share|improve this question


























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I know there must be an easier way to write this but I'm stuck in over-complicating mindset instead of just following the Zen of Python. Please help me simplify this.




      Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
      a boolean indicating if we are on vacation, return a string of the
      form "7:00" indicating when the alarm clock should ring. Weekdays, the
      alarm should be "7:00" and on the weekend it should be "10:00". Unless
      we are on vacation -- then on weekdays it should be "10:00" and
      weekends it should be "off".



      alarm_clock(1, False) → '7:00'
      alarm_clock(5, False) → '7:00'
      alarm_clock(0, False) → '10:00'



      def alarm_clock(day, vacation):

      weekend = "06"
      weekdays = "12345"
      if vacation:
      if str(day) in weekend:
      return "off"
      else:
      return "10:00"
      else:
      if str(day) in weekend:
      return "10:00"
      else:
      return "7:00"









      share|improve this question















      I know there must be an easier way to write this but I'm stuck in over-complicating mindset instead of just following the Zen of Python. Please help me simplify this.




      Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
      a boolean indicating if we are on vacation, return a string of the
      form "7:00" indicating when the alarm clock should ring. Weekdays, the
      alarm should be "7:00" and on the weekend it should be "10:00". Unless
      we are on vacation -- then on weekdays it should be "10:00" and
      weekends it should be "off".



      alarm_clock(1, False) → '7:00'
      alarm_clock(5, False) → '7:00'
      alarm_clock(0, False) → '10:00'



      def alarm_clock(day, vacation):

      weekend = "06"
      weekdays = "12345"
      if vacation:
      if str(day) in weekend:
      return "off"
      else:
      return "10:00"
      else:
      if str(day) in weekend:
      return "10:00"
      else:
      return "7:00"






      python datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 10 '15 at 15:30









      Jamal

      30.2k11115226




      30.2k11115226










      asked Jul 10 '15 at 14:33









      noob81

      73110




      73110






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          I don't think it can get much simpler than this (Pythonic, easy to read, performance great enough to never be a bottleneck):



          def alarm_clock(day, vacation):
          weekend = int(day) in (0, 6)
          if weekend and vacation:
          return 'off'
          elif weekend or vacation:
          return '10:00'
          return '7:00'




          I came up with this after creating a weekend boolean value and then checking the return values alarm_clock should have:



          return_values = {
          # (weekend, vacation): Return value,
          (True, True): 'off',
          (True, False): '10:00',
          (False, True): '10:00',
          (False, False): '7:00'
          }


          As you can see, if both are True (if weekend and vacation:), we should return 'off', and if one of them is True (if weekend or vacation:), we should return 10:00 regardless of which one. Else return 7:00






          share|improve this answer























          • I see you omitted the word "else", is that Pythonic?
            – noob81
            Jul 10 '15 at 16:43












          • @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
            – Markus Meskanen
            Jul 10 '15 at 16:46






          • 1




            @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
            – Markus Meskanen
            Jul 10 '15 at 17:25




















          up vote
          7
          down vote














          1. You don't use weekdays.

          2. You can have two return statements. (Shown below).


          This keeps the same logic, it just removes the need for so meany return statements.



          def alarm_clock(day, vacation):
          weekend = "06"
          if vacation:
          return "off" if str(day) in weekend else "10:00"
          else:
          return "10:00" if str(day) in weekend else "7:00"




          I would improve it further by adding a check, that you enter a number 0-6.



          if not (0 <= day <= 6):
          return "-:--"





          share|improve this answer




























            up vote
            2
            down vote













            What about:




            1. using 10:00 as default:

            2. only check for weekend

            3. you might replace (str(day) in weekend) by (0 == day %6) but it is harder to understand


            Code:



            def alarm_clock(day, vacation):
            weekend = "06"
            if vacation and (str(day) in weekend):
            return "off"
            else:
            if not (str(day) in weekend):
            return "7:00"
            return "10:00"


            The bit more cryptic version:



            def alarm_clock(day, vacation):
            if vacation and 0 == day % 6:
            return "off"
            else:
            if 0 != day % 6:
            return "7:00"
            return "10:00"





            share|improve this answer



















            • 1




              if str(day) not in weekend would be the preferred construction according to PEP8.
              – Jaime
              Jul 10 '15 at 15:54


















            up vote
            2
            down vote













            Building on @Joe Wallis' answer, I would shorten it as follows:



            def alarm_clock(day, vacation):
            weekend = "06"

            times = {"weekend": "10:00", "weekday": "7:00"}
            if vacation:
            times = {"weekend": "off", "weekday": "10:00"}

            return times['weekend'] if str(day) in weekend else times['weekday']


            Which could be further shortened to (detrimental to readability though):



            def alarm_clock(day, vacation):
            times = {"weekend": "off", "weekday": "10:00"} if vacation
            else {"weekend": "10:00", "weekday": "7:00"}

            return times['weekend'] if str(day) in "06" else times['weekday']


            The advantages are that you have a dict with the weekend/weekday times, so you only need one generic return statement. The magic/hardcoded string in the further shortened version is a no-no though. Furthermore, you could extend the function to allow for custom times to be passed in, as such:



            def alarm_clock(day, vacation, times={}):
            times = times.get('regular', {"weekend": "10:00", "weekday": "7:00"})
            if vacation:
            times = times.get('vacation', {"weekend": "off", "weekday": "10:00"})

            return times['weekend'] if str(day) in "06" else times['weekday']


            You can then call it as such:



            times = {'regular': {'weekend': "9:00", "weekday": "7:00"}, "vacation": {"weekend": "12:00", "weekday": "6:00"}}
            alarm_clock(2, False, times)





            share|improve this answer





















              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.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "196"
              };
              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%2fcodereview.stackexchange.com%2fquestions%2f96478%2falarm-clock-printing%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              I don't think it can get much simpler than this (Pythonic, easy to read, performance great enough to never be a bottleneck):



              def alarm_clock(day, vacation):
              weekend = int(day) in (0, 6)
              if weekend and vacation:
              return 'off'
              elif weekend or vacation:
              return '10:00'
              return '7:00'




              I came up with this after creating a weekend boolean value and then checking the return values alarm_clock should have:



              return_values = {
              # (weekend, vacation): Return value,
              (True, True): 'off',
              (True, False): '10:00',
              (False, True): '10:00',
              (False, False): '7:00'
              }


              As you can see, if both are True (if weekend and vacation:), we should return 'off', and if one of them is True (if weekend or vacation:), we should return 10:00 regardless of which one. Else return 7:00






              share|improve this answer























              • I see you omitted the word "else", is that Pythonic?
                – noob81
                Jul 10 '15 at 16:43












              • @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
                – Markus Meskanen
                Jul 10 '15 at 16:46






              • 1




                @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
                – Markus Meskanen
                Jul 10 '15 at 17:25

















              up vote
              4
              down vote



              accepted










              I don't think it can get much simpler than this (Pythonic, easy to read, performance great enough to never be a bottleneck):



              def alarm_clock(day, vacation):
              weekend = int(day) in (0, 6)
              if weekend and vacation:
              return 'off'
              elif weekend or vacation:
              return '10:00'
              return '7:00'




              I came up with this after creating a weekend boolean value and then checking the return values alarm_clock should have:



              return_values = {
              # (weekend, vacation): Return value,
              (True, True): 'off',
              (True, False): '10:00',
              (False, True): '10:00',
              (False, False): '7:00'
              }


              As you can see, if both are True (if weekend and vacation:), we should return 'off', and if one of them is True (if weekend or vacation:), we should return 10:00 regardless of which one. Else return 7:00






              share|improve this answer























              • I see you omitted the word "else", is that Pythonic?
                – noob81
                Jul 10 '15 at 16:43












              • @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
                – Markus Meskanen
                Jul 10 '15 at 16:46






              • 1




                @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
                – Markus Meskanen
                Jul 10 '15 at 17:25















              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              I don't think it can get much simpler than this (Pythonic, easy to read, performance great enough to never be a bottleneck):



              def alarm_clock(day, vacation):
              weekend = int(day) in (0, 6)
              if weekend and vacation:
              return 'off'
              elif weekend or vacation:
              return '10:00'
              return '7:00'




              I came up with this after creating a weekend boolean value and then checking the return values alarm_clock should have:



              return_values = {
              # (weekend, vacation): Return value,
              (True, True): 'off',
              (True, False): '10:00',
              (False, True): '10:00',
              (False, False): '7:00'
              }


              As you can see, if both are True (if weekend and vacation:), we should return 'off', and if one of them is True (if weekend or vacation:), we should return 10:00 regardless of which one. Else return 7:00






              share|improve this answer














              I don't think it can get much simpler than this (Pythonic, easy to read, performance great enough to never be a bottleneck):



              def alarm_clock(day, vacation):
              weekend = int(day) in (0, 6)
              if weekend and vacation:
              return 'off'
              elif weekend or vacation:
              return '10:00'
              return '7:00'




              I came up with this after creating a weekend boolean value and then checking the return values alarm_clock should have:



              return_values = {
              # (weekend, vacation): Return value,
              (True, True): 'off',
              (True, False): '10:00',
              (False, True): '10:00',
              (False, False): '7:00'
              }


              As you can see, if both are True (if weekend and vacation:), we should return 'off', and if one of them is True (if weekend or vacation:), we should return 10:00 regardless of which one. Else return 7:00







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 10 '15 at 17:21

























              answered Jul 10 '15 at 15:39









              Markus Meskanen

              210111




              210111












              • I see you omitted the word "else", is that Pythonic?
                – noob81
                Jul 10 '15 at 16:43












              • @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
                – Markus Meskanen
                Jul 10 '15 at 16:46






              • 1




                @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
                – Markus Meskanen
                Jul 10 '15 at 17:25




















              • I see you omitted the word "else", is that Pythonic?
                – noob81
                Jul 10 '15 at 16:43












              • @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
                – Markus Meskanen
                Jul 10 '15 at 16:46






              • 1




                @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
                – Markus Meskanen
                Jul 10 '15 at 17:25


















              I see you omitted the word "else", is that Pythonic?
              – noob81
              Jul 10 '15 at 16:43






              I see you omitted the word "else", is that Pythonic?
              – noob81
              Jul 10 '15 at 16:43














              @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
              – Markus Meskanen
              Jul 10 '15 at 16:46




              @noob81 That's everyone's personal preference, but it's often considered more pythonic to emit the "else"
              – Markus Meskanen
              Jul 10 '15 at 16:46




              1




              1




              @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
              – Markus Meskanen
              Jul 10 '15 at 17:25






              @noob81 Actually, I take that back. It's just everyone's personal preference, there's absolutely no guidelines to whether you should emit it or not. Often depends on the situation :)
              – Markus Meskanen
              Jul 10 '15 at 17:25














              up vote
              7
              down vote














              1. You don't use weekdays.

              2. You can have two return statements. (Shown below).


              This keeps the same logic, it just removes the need for so meany return statements.



              def alarm_clock(day, vacation):
              weekend = "06"
              if vacation:
              return "off" if str(day) in weekend else "10:00"
              else:
              return "10:00" if str(day) in weekend else "7:00"




              I would improve it further by adding a check, that you enter a number 0-6.



              if not (0 <= day <= 6):
              return "-:--"





              share|improve this answer

























                up vote
                7
                down vote














                1. You don't use weekdays.

                2. You can have two return statements. (Shown below).


                This keeps the same logic, it just removes the need for so meany return statements.



                def alarm_clock(day, vacation):
                weekend = "06"
                if vacation:
                return "off" if str(day) in weekend else "10:00"
                else:
                return "10:00" if str(day) in weekend else "7:00"




                I would improve it further by adding a check, that you enter a number 0-6.



                if not (0 <= day <= 6):
                return "-:--"





                share|improve this answer























                  up vote
                  7
                  down vote










                  up vote
                  7
                  down vote










                  1. You don't use weekdays.

                  2. You can have two return statements. (Shown below).


                  This keeps the same logic, it just removes the need for so meany return statements.



                  def alarm_clock(day, vacation):
                  weekend = "06"
                  if vacation:
                  return "off" if str(day) in weekend else "10:00"
                  else:
                  return "10:00" if str(day) in weekend else "7:00"




                  I would improve it further by adding a check, that you enter a number 0-6.



                  if not (0 <= day <= 6):
                  return "-:--"





                  share|improve this answer













                  1. You don't use weekdays.

                  2. You can have two return statements. (Shown below).


                  This keeps the same logic, it just removes the need for so meany return statements.



                  def alarm_clock(day, vacation):
                  weekend = "06"
                  if vacation:
                  return "off" if str(day) in weekend else "10:00"
                  else:
                  return "10:00" if str(day) in weekend else "7:00"




                  I would improve it further by adding a check, that you enter a number 0-6.



                  if not (0 <= day <= 6):
                  return "-:--"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 10 '15 at 15:04









                  Peilonrayz

                  25.2k336105




                  25.2k336105






















                      up vote
                      2
                      down vote













                      What about:




                      1. using 10:00 as default:

                      2. only check for weekend

                      3. you might replace (str(day) in weekend) by (0 == day %6) but it is harder to understand


                      Code:



                      def alarm_clock(day, vacation):
                      weekend = "06"
                      if vacation and (str(day) in weekend):
                      return "off"
                      else:
                      if not (str(day) in weekend):
                      return "7:00"
                      return "10:00"


                      The bit more cryptic version:



                      def alarm_clock(day, vacation):
                      if vacation and 0 == day % 6:
                      return "off"
                      else:
                      if 0 != day % 6:
                      return "7:00"
                      return "10:00"





                      share|improve this answer



















                      • 1




                        if str(day) not in weekend would be the preferred construction according to PEP8.
                        – Jaime
                        Jul 10 '15 at 15:54















                      up vote
                      2
                      down vote













                      What about:




                      1. using 10:00 as default:

                      2. only check for weekend

                      3. you might replace (str(day) in weekend) by (0 == day %6) but it is harder to understand


                      Code:



                      def alarm_clock(day, vacation):
                      weekend = "06"
                      if vacation and (str(day) in weekend):
                      return "off"
                      else:
                      if not (str(day) in weekend):
                      return "7:00"
                      return "10:00"


                      The bit more cryptic version:



                      def alarm_clock(day, vacation):
                      if vacation and 0 == day % 6:
                      return "off"
                      else:
                      if 0 != day % 6:
                      return "7:00"
                      return "10:00"





                      share|improve this answer



















                      • 1




                        if str(day) not in weekend would be the preferred construction according to PEP8.
                        – Jaime
                        Jul 10 '15 at 15:54













                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      What about:




                      1. using 10:00 as default:

                      2. only check for weekend

                      3. you might replace (str(day) in weekend) by (0 == day %6) but it is harder to understand


                      Code:



                      def alarm_clock(day, vacation):
                      weekend = "06"
                      if vacation and (str(day) in weekend):
                      return "off"
                      else:
                      if not (str(day) in weekend):
                      return "7:00"
                      return "10:00"


                      The bit more cryptic version:



                      def alarm_clock(day, vacation):
                      if vacation and 0 == day % 6:
                      return "off"
                      else:
                      if 0 != day % 6:
                      return "7:00"
                      return "10:00"





                      share|improve this answer














                      What about:




                      1. using 10:00 as default:

                      2. only check for weekend

                      3. you might replace (str(day) in weekend) by (0 == day %6) but it is harder to understand


                      Code:



                      def alarm_clock(day, vacation):
                      weekend = "06"
                      if vacation and (str(day) in weekend):
                      return "off"
                      else:
                      if not (str(day) in weekend):
                      return "7:00"
                      return "10:00"


                      The bit more cryptic version:



                      def alarm_clock(day, vacation):
                      if vacation and 0 == day % 6:
                      return "off"
                      else:
                      if 0 != day % 6:
                      return "7:00"
                      return "10:00"






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 10 '15 at 15:04

























                      answered Jul 10 '15 at 14:55









                      MrSmith42

                      1,978823




                      1,978823








                      • 1




                        if str(day) not in weekend would be the preferred construction according to PEP8.
                        – Jaime
                        Jul 10 '15 at 15:54














                      • 1




                        if str(day) not in weekend would be the preferred construction according to PEP8.
                        – Jaime
                        Jul 10 '15 at 15:54








                      1




                      1




                      if str(day) not in weekend would be the preferred construction according to PEP8.
                      – Jaime
                      Jul 10 '15 at 15:54




                      if str(day) not in weekend would be the preferred construction according to PEP8.
                      – Jaime
                      Jul 10 '15 at 15:54










                      up vote
                      2
                      down vote













                      Building on @Joe Wallis' answer, I would shorten it as follows:



                      def alarm_clock(day, vacation):
                      weekend = "06"

                      times = {"weekend": "10:00", "weekday": "7:00"}
                      if vacation:
                      times = {"weekend": "off", "weekday": "10:00"}

                      return times['weekend'] if str(day) in weekend else times['weekday']


                      Which could be further shortened to (detrimental to readability though):



                      def alarm_clock(day, vacation):
                      times = {"weekend": "off", "weekday": "10:00"} if vacation
                      else {"weekend": "10:00", "weekday": "7:00"}

                      return times['weekend'] if str(day) in "06" else times['weekday']


                      The advantages are that you have a dict with the weekend/weekday times, so you only need one generic return statement. The magic/hardcoded string in the further shortened version is a no-no though. Furthermore, you could extend the function to allow for custom times to be passed in, as such:



                      def alarm_clock(day, vacation, times={}):
                      times = times.get('regular', {"weekend": "10:00", "weekday": "7:00"})
                      if vacation:
                      times = times.get('vacation', {"weekend": "off", "weekday": "10:00"})

                      return times['weekend'] if str(day) in "06" else times['weekday']


                      You can then call it as such:



                      times = {'regular': {'weekend': "9:00", "weekday": "7:00"}, "vacation": {"weekend": "12:00", "weekday": "6:00"}}
                      alarm_clock(2, False, times)





                      share|improve this answer

























                        up vote
                        2
                        down vote













                        Building on @Joe Wallis' answer, I would shorten it as follows:



                        def alarm_clock(day, vacation):
                        weekend = "06"

                        times = {"weekend": "10:00", "weekday": "7:00"}
                        if vacation:
                        times = {"weekend": "off", "weekday": "10:00"}

                        return times['weekend'] if str(day) in weekend else times['weekday']


                        Which could be further shortened to (detrimental to readability though):



                        def alarm_clock(day, vacation):
                        times = {"weekend": "off", "weekday": "10:00"} if vacation
                        else {"weekend": "10:00", "weekday": "7:00"}

                        return times['weekend'] if str(day) in "06" else times['weekday']


                        The advantages are that you have a dict with the weekend/weekday times, so you only need one generic return statement. The magic/hardcoded string in the further shortened version is a no-no though. Furthermore, you could extend the function to allow for custom times to be passed in, as such:



                        def alarm_clock(day, vacation, times={}):
                        times = times.get('regular', {"weekend": "10:00", "weekday": "7:00"})
                        if vacation:
                        times = times.get('vacation', {"weekend": "off", "weekday": "10:00"})

                        return times['weekend'] if str(day) in "06" else times['weekday']


                        You can then call it as such:



                        times = {'regular': {'weekend': "9:00", "weekday": "7:00"}, "vacation": {"weekend": "12:00", "weekday": "6:00"}}
                        alarm_clock(2, False, times)





                        share|improve this answer























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          Building on @Joe Wallis' answer, I would shorten it as follows:



                          def alarm_clock(day, vacation):
                          weekend = "06"

                          times = {"weekend": "10:00", "weekday": "7:00"}
                          if vacation:
                          times = {"weekend": "off", "weekday": "10:00"}

                          return times['weekend'] if str(day) in weekend else times['weekday']


                          Which could be further shortened to (detrimental to readability though):



                          def alarm_clock(day, vacation):
                          times = {"weekend": "off", "weekday": "10:00"} if vacation
                          else {"weekend": "10:00", "weekday": "7:00"}

                          return times['weekend'] if str(day) in "06" else times['weekday']


                          The advantages are that you have a dict with the weekend/weekday times, so you only need one generic return statement. The magic/hardcoded string in the further shortened version is a no-no though. Furthermore, you could extend the function to allow for custom times to be passed in, as such:



                          def alarm_clock(day, vacation, times={}):
                          times = times.get('regular', {"weekend": "10:00", "weekday": "7:00"})
                          if vacation:
                          times = times.get('vacation', {"weekend": "off", "weekday": "10:00"})

                          return times['weekend'] if str(day) in "06" else times['weekday']


                          You can then call it as such:



                          times = {'regular': {'weekend': "9:00", "weekday": "7:00"}, "vacation": {"weekend": "12:00", "weekday": "6:00"}}
                          alarm_clock(2, False, times)





                          share|improve this answer












                          Building on @Joe Wallis' answer, I would shorten it as follows:



                          def alarm_clock(day, vacation):
                          weekend = "06"

                          times = {"weekend": "10:00", "weekday": "7:00"}
                          if vacation:
                          times = {"weekend": "off", "weekday": "10:00"}

                          return times['weekend'] if str(day) in weekend else times['weekday']


                          Which could be further shortened to (detrimental to readability though):



                          def alarm_clock(day, vacation):
                          times = {"weekend": "off", "weekday": "10:00"} if vacation
                          else {"weekend": "10:00", "weekday": "7:00"}

                          return times['weekend'] if str(day) in "06" else times['weekday']


                          The advantages are that you have a dict with the weekend/weekday times, so you only need one generic return statement. The magic/hardcoded string in the further shortened version is a no-no though. Furthermore, you could extend the function to allow for custom times to be passed in, as such:



                          def alarm_clock(day, vacation, times={}):
                          times = times.get('regular', {"weekend": "10:00", "weekday": "7:00"})
                          if vacation:
                          times = times.get('vacation', {"weekend": "off", "weekday": "10:00"})

                          return times['weekend'] if str(day) in "06" else times['weekday']


                          You can then call it as such:



                          times = {'regular': {'weekend': "9:00", "weekday": "7:00"}, "vacation": {"weekend": "12:00", "weekday": "6:00"}}
                          alarm_clock(2, False, times)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 10 '15 at 15:42









                          jsanc623

                          2,511720




                          2,511720






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Code Review 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%2fcodereview.stackexchange.com%2fquestions%2f96478%2falarm-clock-printing%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

                              Quarter-circle Tiles

                              build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                              Mont Emei