27
votes
Programming Challenge: Translate 24-hour time into words
This is an adapted version of Talking Clock from /r/DailyProgrammer.
The point of this thread is to post your code for solving the task. Other will comment with feedback and new ideas. Post what language (and version, if relevant) your code is written in.
Task description
Your task is to translate a 24-hour formatted time to words.
Input description
An hour between 00 and 23 followed by a colon followed by a minute between 0 and 59.
Output description
The time expressed in words, in 12-hour format followed by "am" or "pm".
Sample input
00:00
01:30
12:05
14:01
Sample output
It's twelve am
It's one thirty am
It's twelve oh five pm
It's two oh one pm
I like python
That's a beautiful monstrosity, good job :D Did you run it through some kind of minifier?
You're missing an extra
)
in your first test case :) and you're printing an extra space inone thirty am
.Ah, not sure how that
)
got lost, I've added it back.No tooling was used, just moving code around until it was one expression.
This makes me feel dirty.
Perfect.
MoonScript:
Implemented in GNU Awk. I hacked this together in a few minutes, it's not the best, but I believe it does the job.
EDIT: I just noticed this program doesn't work with minutes 13-19. Oh well, better luck next time.
Rust (1.28, nightly): Takes input as the first argument to the program.
Some quick and dirty JS. It could be better, but it's 4am and I spent the daylight hours programming at work, so I'll worry about quality some other time :)
Stuff that bothers me right off the bat:
No comments.
Logic is too tightly entangled.
Too many assumptions.
No error handling.
Naming could be better.
Some of this stuff could be separated out quite a bit better.
Python. Some might say it's over engineered, I like writing easy to read code. I'm glad I made a function for most of the steps. I'm a tad annoyed with
minute_text()
because of how it turns numbers above 20 into text. Any suggestions about theminute_text()
function are very welcome as long as they are easy to read.Also: Woho! Let the coding challenges begin!
Edit: I wish I had planned this program. Even though it's small it would have benefited from more though. Just shows that to make a program readable all the way through you need to plan.
I believe I saw a discussion yesterday about how important naming is in code. Assuming you don't have plans to revisit this project in two weeks, I admire your variable names. I'm guilty of using one to two letter variable names for coding challenge type questions.
I see that you and other commenters hard-coded the teens, specifically 14-19. I wonder how you could translate 0-20 with the fewest duplicated string literals without becoming a mess of if-else/switch-cases and the cleanest way to substitute similar spoken languages (such as Spanish and Portuguese).
you could
Is the time/energy saved from typing worth making this extra line? Is it neccessary writing fewer string literals? Does it improve the code?
What do you mean with the below?
Doesn't work for fifteen, unless people started calling it fiveteen when I wasn't looking.
Thanks.
I meant exactly what you did. Python is full of crazy looking stuff that fits the bill "is the time/energy saved ... worth making this extra line?" Readable or not, there's always some kooky solution built in to this language.
Wait. Why do you need numbers from 14-19? Am I missing something?
EDIT: nvm, I'm stupid. My solution doesn't even work with those numbers.
I used python. It's probablsy not the best solution but i guess it works ¯\_(ツ)_/¯
My Haskell implementation. I'm sure there room for improvement, I don't have a ton of Haskell experience.
You could replace a lot of parens with the $ operator.
Perl 6
Tests
Source
Did it in C#. Mostly works, it has some issues with a few numbers (apparently 11-19 returns nothing and when testing I also saw 00:59 return nothing) that I should fix, but I'm already running late.
Did it in Kotlin, didn't feel like jumping out of the comfort zone today:
can run it here
Here's my implementation in rust
Here's my (probably too verbose/abuse of STL) semi-noob version in C++: