2
\$\begingroup\$

Write a program which takes the current time of the system clock, rounds to the nearest minute (you may choose how to round halves) and displays it formatted as the following:

A quarter past one in the morning.
A quarter to two in the afternoon.
Two o'clock in the afternoon.
Twenty-five minutes to three in the morning.
Twenty-seven minutes to seven in the evening.
One minute past twelve in the afternoon.
Twelve o'clock noon.
Twelve o'clock midnight.

Rules:

  • 12:01am to 11:59am is deemed as morning.
  • 12:01pm to 5:59pm is deemed as afternoon.
  • 6:00pm to 11:59pm is deemed as evening.
  • 15 minutes past the hour must be expressed as A quarter past the hour
  • 30 minutes past the hour must be expressed as Half past the hour
  • 45 minutes past the hour must be expressed as A quarter to the upcoming hour
  • On-the-hour times must be expressed as the hour o'clock, with the addition of noon and midnight being expressed with the addition of noon or midnight respectively.
  • Other times in hh:01 to hh:29 must be expressed as (number of minutes) past the hour.
  • Other times in hh:31 to hh:59 must be expressed as (number of minutes) to the upcoming hour.
  • Twenty-one to twenty-nine must be hyphenated.
  • The first letter of the output must be capitalised. The other letters must be lower-case.

It would be useful to provide a testing version of your code so that it can be tested without manipulating the system clock.

\$\endgroup\$
  • 2
    \$\begingroup\$ Let's see what you got... - The specification is still a bit vague. E.g. what about Fifteen minutes past one or Fourty-five minutes past one for the first two examples? \$\endgroup\$ – Howard Dec 10 '13 at 7:57
  • 1
    \$\begingroup\$ I agree with Howard: there's a lot of dialectal variation here. To ensure that the contest is about golfing you need a complete spec. You also need to address rounding because the time is rarely an integer number of minutes past midnight. \$\endgroup\$ – Peter Taylor Dec 10 '13 at 8:39
  • \$\begingroup\$ What? Noon is noon, and midnight is midnight. I suggest those times read "Twelve o'clock noon" and "Twelve o'clock midnight". Noon is not afternoon, or 0=1. \$\endgroup\$ – boothby Dec 10 '13 at 9:21
  • \$\begingroup\$ Okay, so what do I print at 3:30 PM? Do I have to say "half past three in the afternoon"? \$\endgroup\$ – Doorknob Dec 10 '13 at 13:15
  • \$\begingroup\$ I'm voting to close until the spec is clarified to avoid more people posting solutions which might have to be withdrawn for not meeting the clarified spec. \$\endgroup\$ – Peter Taylor Dec 10 '13 at 14:23
0
\$\begingroup\$

Python 2.7, 159 162 156 161

from datetime import*
n=datetime.now()
if n.hour()<12:
 t="morning"
elif n.hour()<18:
 t="afternoon"
else:
 t="evening"
print"%i in the %s."%(n.hour(),t)

Edit #1 - Added a period at the end of the output.

Edit #2 - Shortened the imports, lengthened code to correct syntax.

\$\endgroup\$
  • \$\begingroup\$ Why the verbosity in the print statement? You're calling str on... a string... anyway, much shorter: print"%i in the %s."%(n.hour,t) (and then you can remove the period from the other strings, saving even more chars \$\endgroup\$ – Doorknob Dec 10 '13 at 13:16
  • 1
    \$\begingroup\$ Attempting to test on ideone gives syntax error. (I was attempted to test because on inspection this doesn't seem to meet the spec, regardless of how the current ambiguities are resolved). \$\endgroup\$ – Peter Taylor Dec 10 '13 at 14:22
  • \$\begingroup\$ Would be helpful to show an example with input and output. Looks to me like numbers are rendered as digits rather than letter strings. \$\endgroup\$ – DavidC Dec 10 '13 at 16:07
  • \$\begingroup\$ @DavidCarraher They are \$\endgroup\$ – Timtech Dec 10 '13 at 16:15
  • \$\begingroup\$ @Tim, Did you read the spec? It needs to output the time in a written format as I mentioned above... \$\endgroup\$ – WallyWest Dec 10 '13 at 23:33
2
\$\begingroup\$

Mathematica 450

Date[] calls for the current date and time. [[{4,5}]] returns the hour and minutes

f[]:=f[Date[][[{4,5}]]];
f[{h_,m_}]:=Module[{p,w,s},
p@h1_:=Which[0<h<12," in the morning.",12<h<18," in the afternoon.",18<h<24, " in the evening."];
s[t_]:=WolframAlpha["spell "<>ToString@t,{{"Result",1},"Plaintext"}];
w=s[h~Mod~12]<>p@h;
Which[
m==0, Switch[h,12, "noon", 0|24,"midnight",_,s[h~Mod~12]<>p@h],
m==15,"a quarter past "<>w,
m==30,"half past "<>w,
m==45,"a quarter to "<>s[h~Mod~12+1]<>p@h,
m<30,s@m<>" minutes after "<>w,
m>29,s[60-m]<>" minutes before "<>s[h~Mod~12+1]<>p@h]]

Current Time

f[]

"ten minutes past three in the afternoon."

Specific times

f[{4, 45}]
f[{10, 30}]
f[{11, 0}]
f[{12, 0}]
f[{13, 15}]
f[{15, 0}]
f[{22, 17}]
f[{24, 0}]

"a quarter to five in the morning."
"half past ten in the morning."
"eleven in the morning."
"noon"
"a quarter past one in the afternoon."
"three in the afternoon."
"seventeen minutes past ten in the evening."
"midnight"

\$\endgroup\$
  • \$\begingroup\$ I noticed I still need to implement "minutes before midnight/noon" and "n minutes after midnight/noon". Will get to that later. \$\endgroup\$ – DavidC Dec 10 '13 at 20:24
  • \$\begingroup\$ You can save a few characters and get in spec by using "to" rather than "before" and "past" rather than "after". \$\endgroup\$ – Peter Taylor Dec 12 '13 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.