Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on 12-hour time is weird

Post

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 through 11 AM, 12 PM, or 1 PM through 11 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Test cases (1 comment)
Test cases
Shaggy‭ wrote 3 months ago

Could you add some test cases and a worked example, please?