Programming challenges thread

Programming challenges thread
Make the following in your language of choice:
A program that translates 24-hour time in the format "HH:mm" to words.

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

Attached: about thirty users fall f(...).jpg (667x645, 47K)

(MoonScript)
tests = {
"00:00"
"01:30"
"12:05"
"14:01"
}

translations =
[1]: "one", [2]: "two", [3]: "three", [4]: "four"
[5]: "five", [6]: "six", [7]: "seven", [8]: "eight", [9]: "nine"
[10]: "ten", [11]: "eleven", [12]: "twelve", [13]: "thirteen", [14]: "fourteen"
[15]: "fifteen", [16]: "sixteen", [17]: "seventeen", [18]: "eighteen", [19]: "nineteen"
[20]: "twenty", [30]: "thirty", [40]: "forty", [50]: "fifty"

translate = (t) ->
h = tonumber t\sub 1,2
m = tonumber t\sub 4,5

suffix = h < 12 and "am" or "pm"
h = if h == 0 then 12 else h
hFinal = translations[if h > 12 then h % 12 else h]

local mFinal
if m == 0
mFinal = "o'clock"
elseif m < 10
mFinal = "oh #{translations[m]}"
elseif m < 20 or m % 10 == 0
mFinal = translations[m]
else
mFirst = tonumber tostring(m)\sub 1,1
mLast = tonumber tostring(m)\sub 2,2
mFinal = translations[mFirst * 10] .. translations[mLast]

"It's #{hFinal} #{mFinal} #{suffix}"

print translate t for t in *tests

I would but it seems pretty easy.

"""challenge"""

Testing.

Okay, apparently my solution was so good I get a connection error when I attempt to post it.

>It's twelve o'clock pm

var dt = DateTime.ParseExact(Console.ReadLine(), "HH:mm", null, System.Globalization.DateTimeStyles.None);

var words = new Dictionary
{
{ 1, "one" },
{ 2, "two" },
{ 3, "three" },
{ 4, "four" },
{ 5, "five" },
{ 6, "six" },
{ 7, "seven" },
{ 8, "eight" },
{ 9, "nine" },
{ 10, "ten" },
{ 11, "eleven" },
{ 12, "twelve" },
{ 13, "thirteen" },
{ 14, "fourteen" },
{ 15, "fifteen" },
{ 16, "sixteen" },
{ 17, "seventeen" },
{ 18, "eighteen" },
{ 19, "nineteen" },
{ 20, "twenty" },
{ 30, "thirty" },
{ 40, "forty" },
{ 50, "fifty" },
};

var sb = new StringBuilder();
sb.Append("It's ");
sb.Append(words[dt.Hour % 12]);

if (dt.Minute > 0)
{
sb.Append($" oh ");

if (!words.ContainsKey(dt.Minute))
{
var d1 = dt.Minute % 10;
var d10 = (dt.Minute - d1);
sb.Append(words[d10]);
if (d1 > 0)
{
sb.Append($" {words[d1]}");
}
}
else sb.Append(words[dt.Minute]);
}

sb.Append(dt.Hour < 12 ? " am" : " pm");

Console.WriteLine(sb.ToString());

Looks to me like your one will append "oh" to every minute past 0.

>It's twelve oh fifty pm

var x = 5;
this is a test

num2word = {0: 'oh', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', 11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', 15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', 19:'nineteen', 20:'twenty', 30:'thirty', 40:'fourty', 50:'fifty'}


def time2word(h,m):
whatnoon='am'
if h>12:
whatnoon='pm'
h=h%12
if m == 0:
min= ''
elif m

Who the fuck says "oh" for zero

burgerstan

At 3:05, you just say "three five"?

I say "it is eleven twelfth of an hour to four"

that's super fucking unweildy

How about finding the longest word in a string?

Your program should perform the following routine on the number: Arrange the digits in descending order and in ascending order (adding zeroes to fit it to a 4-digit number), and subtract the smaller number from the bigger number. Then repeat the previous step. Performing this routine will always cause you to reach a fixed number: 6174. Then performing the routine on 6174 will always give you 6174 (7641 - 1467 = 6174). Your program should return the number of times this routine must be performed until 6174 is reached. For example: if num is 3524 your program should return 3 because of the following steps: (1) 5432 - 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174.

Input:2111
Output:5

Input:9831
Output:7

not doing your homework

#include
#include
#include
using namespace std;

void getNext(int& n)
{
vector v;
for(int i=0;i

var OP = "stupid KEK"
println("I will not do your homework you " + OP)

>using namespace std;
>int

Attached: moode.png (1137x571, 234K)

Yes, so?

in what region do people say this?

>Performing this routine will always cause you to reach a fixed number: 6174.
8888-8888=0
>0-0=0
0-0=0
>0-0=0
0-0=0
>0-0=0
0-0=0
>0-0=0
0-0=0

Write a program that efficiently lists all duplicate files in a given folder (including all subfolders).

define "duplicate files"

Files that have identical contents are considered duplicates. File names and other file meta-data are not relevant for the purpose of this exercise

oh, for the purpose of this exercise, don't worry about soft or hard links

const arrange = (num) => {
let count = 0, max, min, str, desc, asc;
while(num!=6174) {
desc = Number(String(num).split('').sort((a,b)=>b-a).join(''));
asc = Number(String(num).split('').sort((a,b)=>a-b).join(''));

if(desc>=asc) {
max = desc;
min = asc;
} else {
min = desc;
max = asc;
}

num = max - min;

while(String(num).length

kek

#lang racket

(require racket/match)
(require rackunit)

(define translations
'("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"
"ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen"
"sixteen" "seventeen" "eighteen" "nineteen"))
(define translations-tens
'("" "" "twenty" "thirty" "fourty" "fifty"))

(define (challenge hhmm)
(match (map string->number (string-split hhmm ":"))
[(list hh mm)
(string-join
`("It's"
,(list-ref translations (add1 (modulo (sub1 hh) 12)))
,@(cond
[(= mm 0) '()]
[(< mm 10) `("oh" ,(list-ref translations mm))]
[(< mm 20) `(,(list-ref translations mm))]
[else `(,(list-ref translations-tens (floor (/ mm 10)))
,@(if (zero? (modulo mm 10)) '()
'((list-ref translations (modulo mm 10)))))])
,(if (< hh 12) "am" "pm")))]))

(module+ test
(test-equal? "Test 1" (challenge "00:00") "It's twelve am")
(test-equal? "Test 2" (challenge "01:30") "It's one thirty am")
(test-equal? "Test 3" (challenge "12:05") "It's twelve oh five pm")
(test-equal? "Test 4" (challenge "14:01") "It's two oh one pm"))

Attached: racket-logo.png (512x512, 20K)

#lang racket

(define-match-expander regexp
(syntax-rules ()
[(_ re . pats)
(app (λ (x) (regexp-match re x)) (list _ . pats))]))

(define-match-expander div-mod
(syntax-rules ()
[(_ n q-pat r-pat)
(app (λ (x) (quotient/remainder x n)) q-pat r-pat)]))

(define-match-expander ref
(syntax-rules ()
[(_ dict pat)
(app (λ (key) (dict-ref dict key #f))
(and (not #f) pat))]))

;; ------------------------------------------------

(define SMALL-DIGITS
(vector #f "one" "two" "three" "four" "five" "six" "seven"
"eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen"
"fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))

(define TENS
(vector #f #f "twenty" "thirty" "fourty" "fifty"))

(define integer->word
(match-lambda
[0 (integer->word 12)] ;; dumb hack
[(ref SMALL-DIGITS x) x]
[(div-mod 10 (ref TENS x) 0) x]
[(div-mod 10 (ref TENS x) (ref SMALL-DIGITS y)) (format "~a ~a" x y)]))

;; ------------------------------------------------

(define challenge
(match-lambda
[(regexp #px"(..):(..)"
(app string->number (div-mod 12 HH hh))
(app string->number mm))
(define hh/str (integer->word hh))
(define mm/str (integer->word mm))
(define tod
(cond
[(= mm 0) hh/str]
[(< mm 12) (format "~a oh ~a" hh/str mm/str)]
[else (format "~a ~a" hh/str mm/str)]))
(define am/pm
(if (zero? HH) "am" "pm"))
(format "It's ~a ~a" tod am/pm)]))

;; ------------------------------------------------

(module+ test
(require rackunit)
(check string=? (challenge "00:00") "It's twelve am")
(check string=? (challenge "01:30") "It's one thirty am")
(check string=? (challenge "12:05") "It's twelve oh five pm")
(check string=? (challenge "14:01") "It's two oh one pm")
)

Attached: backet.png (700x700, 163K)

I think this is from Jow Forumsdailyprogrammer

Even worse.

...

everywhere except the us lol

I haven't heard anyone say it like that and I have never been to the US.
Wait, that's wrong. I have heard Japanese English teachers say it like that, but as they aren't native speakers I don't think that should count.