将时间转换为字符串


17

问题

有一天,你的老板走到你身边,告诉你他需要知道时间。在您指着您头顶正上方的时钟后,他仍然坚持,并承认他患有严重的运动失调症,这导致他甚至无法在时钟上看到数字。当您仍然想知道无法看到数字的人如何经营一家公司时,他会告诉您“为它做些事情”,无论如何。

现在,您的任务是创建一个程序或函数,给定输入时间的小时和分钟(秒是可选的)(小时是基于24的),输出一个可读的英语句子,其中不包含数字。

不需要输出秒。

例如08:45:15应该输出quarter to nine o'clock AM

测试用例

00:00:00 > twelve night
12:00:00 > twelve noon
06:00:00 > six o'clock AM
18:00:00 > six o'clock PM
06:15:00 > quarter past six o'clock AM
18:45:00 > quarter to seven o'clock PM
11:30:00 > half past eleven o'clock AM
13:22:00 > twentytwo minutes past one o'clock PM
17:43:00 > seventeen minutes to six o'clock PM
00:09:00 > nine minutes past twelve o'clock AM

规则

输出数字的形式可以是42或42,以您最喜欢的形式。不允许出现标准漏洞。尾随换行符是允许的,但是句子应该在1行上。

输入可以是任何你想要的格式,例如HH:MM:SSHH,MM,SSHHMMSSHH/MM/SS,或没有任何变异秒,但你应该做的输入格式在你的答案清除。不需要输出one minute代替one minutes

在0到30(包括0和30)范围内,应使用分钟past,对于其他,则应使用to

由于时间是相对的,因此以字节为单位的最短代码将获胜。


1
这看起来是一个不错的挑战,我强烈建议您删除奖金。除非您确切地知道自己在做什么,否则在编写挑战时应该避免
Denker

1
还有两点(次要的)要点:forty-five当测试用例清楚地表示quarter to为适当的输入格式时,为什么写怎么重要?(只需将其更改为四十二;))而且,kolmogorov-complexity不是合适的标记。
桑契斯'16

@sanchises已编辑。我误解了kolmogorov标记,并认为它与输出任何字符串而不是仅输出给定字符串有关的问题有关。
丹尼斯·范·吉尔斯

1
@TimmyD -70%的人是个玩笑,我没想到有人会尝试在代码高尔夫球中翻译法语。但是,我觉得负数是对完美的法国英语翻译的体面奖励
Dennis van Gils,2016年

看起来好多了,但是还需要一些格式说明。特别是关于在“过去”和“到”之间进行选择-从示例中看来,您似乎只想使用oneto twenty-ninequarter替换fifteenhalf表示的数字thirty,但这还不清楚。此外,对于以后的问题,我衷心建议您在发布之前先解决一下Sandbox的问题。
AdmBorkBork '16

Answers:


2

JavaScript中,384个 381字节

(h,m)=>(o='one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thir,,fif,,,eigh,'.split`,`,o=o.map((x,i)=>i>11?(x||o[i-10])+'teen':x),(a=m>30,t=12,m?(m-30?m%15?((m=a?59-m:m)>=(b=a?19:20)?'twenty'+((m%=b)?o[a?m:m-1]:''):o[a?m:m-1])+' minutes':'quarter':'half')+(a?' to ':' past '):'')+(h%t|m?o[(a?h:(h?h-1:11))%t]+` o'clock ${h>t?'P':'A'}M`:o[11]+(h-t?' night':' noon')))


1
工作正常,甚至使Perl胜4字节!FIF,eigh等顺便说一句。
丹尼斯·范·吉尔斯

4

LaTeX,466字节

\usepackage{fmtcount,etoolbox}\def\x{\the\numexpr}\def\n[#1]{\numberstringnum{#1}}\def\h[#1]{\ifnumequal{#1}{0}{twelve night}{\ifnumequal{#1}{12}{twelve noon}{\ifnumless{#1}{13}{\n[#1] o'clock AM}{\n[\x#1-12\relax] o'clock PM}}}}\def\m[#1]{\ifnumequal{#1}{15}{quarter}{\ifnumequal{#1}{30}{half}{\n[#1] minutes}}}\newcounter{c}\def\f[#1]#2{\ifnumequal{#2}{0}{\h[#1]}{\ifnumless{#2}{31}{\m[#2] past \h[#1]}{\setcounter{c}{\x60-#2\relax}\m[\thec] to \h[\x#1+1\relax]}}}

只需将宏\f称为\f[hour]{minutes},这里进行一些测试:

\begin{document}

\noindent
\textbf{Test cases:} \\ \\
00:00 = \f[00]{00} \\
12:00 = \f[12]{00} \\
06:00 = \f[6]{00} \\
18:00 = \f[18]{00} \\
06:15 = \f[6]{15} \\
18:45 = \f[18]{45} \\
11:30 = \f[11]{30} \\
13:22 = \f[13]{22} \\
17:43 = \f[17]{43} \\

\noindent
\textbf{More test cases:} \\ \\
00:13 = \f[00]{13} \\
12:12 = \f[12]{12} \\
12:15 = \f[12]{15} \\
11:45 = \f[11]{45} \\
11:41 = \f[11]{41} \\

\end{document}

您知道我可以在哪里以及在哪里尝试吗?我试过蛛网膜纸巾,但没有用。
丹尼斯·范·吉尔斯

@DennisvanGils我在纸莎草纸上尝试过pastebin.com/kdpSQHSb,它确实有效。
鲍勃,

使用pastebin代码对其进行了尝试,并且奏效了,因为我没有,所以猜测它不起作用\documentclass[a4paper,12pt]{article}
Dennis van Gils

3

Python 2,498字节

输入作为函数参数。前一个小时,然后是分钟,两者均为整数。结果被打印到屏幕上。

def g(h,m):
 a=lambda x:"twelve one two three four five six seven eigth nine ten eleven".split()[x%12];b=lambda x:a(x)+" o'clock "+["AM","PM"][x>=12]
 if m:
    z=m if m<30else 60-m;print(a(z)if z<13else("twenty"+(a(z-20)if z-20else"")if z>19else"thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split()[z-13])if m%15else"quarter"if m-30else"half")+(" minutes"if m-30and m%15else"")+[" past "+b(h)," to "+b(h+1)][m>30]
 else:print"twelve "+("noon"if h else "night")if h in[0,12]else b(h)

在线尝试! (带有稍微扩展的测试用例)

那是一场非常有趣的高尔夫。尽管所有这些嵌套的三元组使我有点疯狂^^
我计划在此之后在Pyth进行此操作,但是我认为目前(或以后)我在精神上无法做到这一点。

取消高尔夫:

def ungolfed(h,m):
    toStr=lambda x:"twelve one two three four five six seven eigth nine ten eleven".split()[x%12]
    hour=lambda x:toStr(x)+" o'clock "+["AM","PM"][x>=12]
    minute=lambda x:"twenty"+(toStr(x-20)if x-20else"")if x>19else"thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split()[x-13]

    if m:
        s=""
        if m==30:
            s+="half"
        else:
            if m%15==0:
                s+="quarter"
            else:
                z=m if m<30 else 60-m
                if z<13:
                    s+=toStr(z)
                else:
                    s+=minute(z)
        print s+(" minutes"if m-30and m%15else "")+[" past "+hour(h)," to "+hour(h+1)][m>30]
    else:
        if h in[0,12]:
            print"twelve "+("noon"if h else "night")
        else:
            print hour(h)

[x>12]应该是[x>=12]12:01 PM是不是AM
丹尼斯面包车GILS

1
@DennisvanGils很好,谢谢!固定它。:)我们德国人对整个上午/下午的事情并不满意:P
Denker

我是荷兰人,所以我也必须亲自检查一下以确保。
丹尼斯·范·吉尔斯

1

Perl的5,367 361个 385字节

乱码

use integer;$_=<>;~/(\d+):(\d+)/;@b=(one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve); @x=(@b,thirteen,(map{/fi/?quarter:$_.teen}@b[3..8]),(map{twenty.$_}'',@b[0..8]),half);@y=map{/^q|h/?$_:$_.' minutes'}@x;$s=1-$2/31;$y=abs((-29,0)[$s]+$2%31)-1;$h=$1-$s;print((+-$2?@y[$y].' '.(to,past)[$s].' ':'').@b[$h++%12]." o' clock ".($h%12?(AM,PM)[$h/12]:(night,noon)[$h/12%2]))

格式化和测试用例:

use integer;
map{
    # capture (implicit $_)
    ~/(\d+):(\d+)/;
    # bare words
    @b=(one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve);
    # construct up to 30
    @x=(@b,thirteen,
       (map{/fi/?quarter:$_.teen}@b[3..8]),
       (map{twenty.$_}'',@b[0..8]),half);
    # quarter/half business
    @y=map{/^q|h/?$_:$_.' minutes'}@x;
    # whether we are to/past
    $s=1-$2/31;
    # num minutes wrapped around
    $y=abs((-29,0)[$s]+$2%31)-1;
    # num hours
    $h=$1-$s;
    #print('$s='.$s."\th=".$h."\th%12=".($h%12)."\n");
    print($_.' > ');
    #minute component
    print((+-$2
          ?@y[$y].' '.(to,past)[$s].' '
          :'')
        # hours
        .@b[$h++%12]
        ." o' clock "
        # Night/Noon
        .($h%12
          ?(AM,PM)[$h/12]
          :(night,noon)[$h/12%2]));
    print "\n"
}
('00:00:00',
 '00:01:00',
 '11:59:00',
 '12:00:00',
 '12:01:00',
 '06:00:00', 
 '18:00:00', 
 '06:15:00', 
 '18:45:00', 
 '11:30:00', 
 '13:22:00', 
 '17:43:00',
 '23:59:59')

这似乎给one minutes to one o' clock night作为输出为00:01,而不是one minutes past twelve o'clock AMone minutes past twelve night
丹尼斯面包车GILS

你是对的。我将测试案例添加了大约12次。
walpen '16

1

C,478字节

*w(i){char*p,*n[]={"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};return i>19?asprintf(&p,"twenty%s",n[i%20]),p:n[i];}main(m,h){scanf("%d%d",&h,&m);m>30?h++:0;printf("%s%s%s%s%s %s\n",m%15?m>30?w(60-m):w(m):m^30?m?"quarter":"":"half",m%15?" minutes":"",m?m>30?" to ":" past ":"",w((h+11)%12+1),h%12||m?" o'clock":"",h%12||m?h>11?"PM":"AM":h?"noon":"night");}

测试用例

$ echo "0 0" | ./a.out
twelve night
$ echo "12 0" | ./a.out
twelve noon
$ echo "6 0" | ./a.out
six o'clock AM
$ echo "18 0" | ./a.out
six o'clock PM
$ echo "6 15" | ./a.out
quarter past six o'clock AM
$ echo "18 45" | ./a.out
quarter to seven o'clock PM
$ echo "11 30" | ./a.out
half past eleven o'clock AM
$ echo "13 22" | ./a.out
twentytwo minutes past one o'clock PM
$ echo "17 43" | ./a.out
seventeen minutes to six o'clock PM
$ echo "0 9" | ./a.out
nine minutes past twelve o'clock AM

在这里尝试。


看起来不错 同样有趣的是如何00 00给出正确的输出,而00:00给出给出one minutes past twelve o'clock AM,尽管我确定这与您解析输入的方式有关。
丹尼斯·范·吉尔斯

是的,它需要两个整数(小时和分钟)。scanf可能不会像人们希望的那样解析“ 00:00” :)
科尔·卡梅隆

0

分批,779个 774 752 749字节

@echo off
set t=%1
set/ah=%t:~0,2%,m=%t:~3,2%
set p=past
if %m% gtr 30 set p=to&set/am=60-m,h+=1
for %%a in ("one minute.1" quarter.15 half.30)do if %%~xa==.%m% set o=%%~na&goto g
set o=
if %m% gtr 20 set o=twenty-&set/am-=20
for %%a in (one.1 two.2 three.3 four.4 five.5 six.6 seven.7 eight.8 nine.9 ten.10 eleven.11 twelve.12 thirten.13 fourteen.14 sixteen.16 seventeen.17 eighteen.18 nineteen.19 twenty.20)do if %%~xa==.%m% set o=%o%%%~na minutes
:g
if not %m%==0 set o=%o% %p% 
set a= AM
if %h% gtr 12 set a= PM&set/ah%%=12
if %h%==0 (echo %o%twelve night)else if %h%==12 (echo %o%twelve noon)else for %%a in (one.1 two.2 three.3 four.4 five.5 six.6 seven.7 eight.8 nine.9 ten.10 eleven.11)do if %%~xa==.%h% echo %o%%%~na o'clock%a%

set t=%1%time%如果未提供时间,则将第二行更改为默认为说出当前时间。

编辑:通过优化我的set/a语句节省了22个字节。感谢@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ,节省了3个字节。

当我写原始版本时,我犯了一个错误,就是说的时间与我实际说的一样,即仅在确切的时间,午夜和正午而不是十二点和正午十二点:

@echo off
set t=%1
set/a h=%t:~0,2%
set/a m=%t:~3,2%
set p=past
if %m% gtr 30 set p=to&set/a m=60-m&set/a h=h+1
for %%a in ("one minute.1" quarter.15 half.30) do if %%~xa==.%m% set o=%%~na&goto g
set o=
if %m% gtr 20 set o=twenty-&set/a m=%m%-20
for %%a in (one.1 two.2 three.3 four.4 five.5 six.6 seven.7 eight.8 nine.9 ten.10 eleven.11 twelve.12 thirten.13 fourteen.14 sixteen.16 seventeen.17 eighteen.18 nineteen.19 twenty.20) do if %%~xa==.%m% set o=%o%%%~na minutes
:g
set o=%o% %p% 
set a= AM
if %h% gtr 12 set a= PM&set/a h=h%%12
if %m%==0 set a= o'clock%a%&set o=
if %h%==0 (echo %o%midnight)else if %h%==12 (echo %o%midday)else for %%a in (one.1 two.2 three.3 four.4 five.5 six.6 seven.7 eight.8 nine.9 ten.10 eleven.11) do if %%~xa==.%h% echo %o%%%~na%a%

您的12:00和00:00测试无效,它们仅回显midnight和,midday而不是twelve nighttwelve noon。我真的很喜欢您如何在for循环中使用假扩展名和名称。
丹尼斯·范·吉尔斯

2
@DennisvanGils可能还有其他测试失败。稍后再修复。
尼尔

@DennisvanGils我认为它现在正在工作,并且方便地它也更短!
尼尔
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.