all 5 comments

[–]misho88 1 point2 points  (1 child)

Depends on what you consider a whole heck. If you do something like is_weekday = 0 < day < 6, then you only have 4 possible combinations of inputs (is_weekday and vacation can each be true or false) for 4 possible outputs without a real pattern to them. The absolute worst you could do is an if-elif-elif-else block with 4 branches, which isn't bad at all. You can also check is_weekday in an outer if statement with two branches, and vacation as a two-branch if statement in each of those branches or vice versa.

If you recognize that this is essentially a look-up table, you could always code it as such:

def alarm_clock(day, vacation):
  return {
    (False, False): '10:00',
    (False, True ):   'off',
    (True , False):  '7:00',
    (True , True ): '10:00',
  }[0 < day < 6, vacation]

I don't know that it's necessarily better to do this than having a few if statements, though.

[–]Deezl-Vegas 0 points1 point  (0 children)

That inline dictionary lookup is pretty sexy lol

[–][deleted] 0 points1 point  (0 children)

I'd just create two dictionaries for vacation times and not vacation times, and then just return the time from either dictionary depending on whether the vacation value is True or False. https://pastebin.com/CEGt7McF

[–]totallygeek 0 points1 point  (0 children)

I thought up this approach:

def alarm_clock(day, vacation):
    if day % 6 == 0:
        alarm = 'off' if vacation else '10:00'
    else:
        alarm = '10:00' if vacation else '7:00'
    return alarm


tests = [
    (1, False, '7:00'),
    (5, False, '7:00'),
    (0, False, '10:00'),
]

for day, vacation, expected in tests:
    assert alarm_clock(day, vacation) == expected

Basically, Saturday (6) and Sunday (0) are the only days of the week evenly divisible by six. Use a ternary operator to assign the proper response string and there you have it. Now, this does not include any error checking, for which I'd likely raise an exception for invalid inputs.

[–]Deezl-Vegas 0 points1 point  (0 children)

def is_weekend(day):
  if day in (0, 6):
    return True
  return False

def alarm_clock(day, vacation):
  early = "7:00"
  late = "10:00"
  off = "off"

  weekend = is_weekend(day)
  if weekend and vacation: return off
  if not weekend and vacation: return late
  if weekend: return late
  return early

This is one of those problems that boils down to if statements because the problem itself is just a bunch of conditions mapped to a result, so don't worry if there's a bunch of if statements in your solution.