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

Comments on 12-hour time is weird [finalized]

Post

12-hour time is weird [finalized]

+3
−0

Posted: https://codegolf.codidact.com/posts/291018

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.

1 comment thread

Is `12 PM - 1 AM = (-1)` correct? From the spec, I expected `12 PM - 1 AM = (-11)` . (Or maybe you ... (2 comments)
Is `12 PM - 1 AM = (-1)` correct? From the spec, I expected `12 PM - 1 AM = (-11)` . (Or maybe you ...
Monica Cellio‭ wrote 9 months ago

Is 12 PM - 1 AM = (-1) correct? From the spec, I expected 12 PM - 1 AM = (-11) . (Or maybe you meant 11 AM instead of 1 AM?)

honnza‭ wrote 9 months ago

You're right, good catch. Fixed.