12-hour time is weird
+3
−0
Your mission for today, should you choose to accept it, is to write a simple 12-hour clock calculator. Your input will always be in the form of arg op arg
, where op
is either +
or -
, and arg
is either:
- Absolute hour, which is one of, in sequence,
12 AM
,1 AM
through11 AM
,12 PM
, or1 PM
through11 PM
. The sequence then repeats cyclically. - Time offset, which goes from
(-11)
through(+0)
to(+12)
inclusive.(-0)
is not a permitted notation.
In either case, a leading zero is not permitted.
Your output shall be:
- If the input is in the form offset + offset or offset - offset, add/subtract the offsets as indicated. If the value exceeds
(+12)
, subtract 24 from the offset. If the value is lower than(-11)
, add 24 to the offset.
E.g.(-7) + (-5) = (+12)
,(-7) - (-5) = (-2)
- If the input is in the form absolute + offset, absolute - offset or offset + absolute, output the corresponding absolute hour.
E.g.7 AM + (-5) = (-5) + 7 AM = 2 AM
,7 AM - (-5) = 7 AM + (+5) = 12 PM
- If the input is in the form absolute - absolute, output the unique offset such that right + offset = left.
E.g.12 PM - 1 PM = (-1)
,11 PM - 1 PM = (+10)
- If the input is in the form offset - absolute or absolute + absolute, output
TE
(type error).
You may assume that the input follows the syntax and range limits defined above. Your output is required to follow the same syntax and range limits that apply to the input arguments.
1 comment thread