代码高尔夫:电话号码到文字
我们都见过用文字表达的电话号码:1-800-BUY-更多等等。
您可以编写的最短代码量是多少,可以生成 7 位数美国电话号码的所有可能的单词组合。
输入将是一个七位整数(或字符串,如果更简单的话),并假设输入格式正确。
输出将是一个由七个字符组成的字符串列表,
例如,数字 428-5246 将生成
GATJAGM
加特加恩
嘎贾戈
加特贾姆
加特扬
嘎贾戈
加特贾姆
加特詹
加特加奥 等等......
获胜标准将是来自任何语言的代码,其中字符最少,可产生所有可能的字母组合。
补充说明:
- 为了让它更有趣,文字 只能通过使用来形成 北美经典的字母 带有三个字母的键盘电话 每个数字如此处定义。这意味着 Z 和 Q被排除。
- 对于数字“1”,请输入一个空格。
- 对于数字“0”,输入连字符“-”
将输出识别为真正的英语单词即可获得奖励积分。好吧,其实不是。 ;-)
添加: 好的,让我们使用“Nick's Modified North American Classic Key Pad”,它的 6 键上有一个“O”(哦,不是零)。
Guidelines for code-golf on SO
We've all seen phone numbers that are put into words: 1-800-BUY-MORE, etc.
What is the shortest amount of code you can write that will produce all the possible combinations of words for a 7 digit US phone number.
Input will be a seven-digit integer (or string, if that is simpler), and assume that the input is properly formed.
Output will be a list of seven-character strings that
For instance, the number 428-5246 would produce
GATJAGM
GATJAGN
GATJAGO
GATJAHM
GATJAHN
GATJAGO
GATJAIM
GATJAIN
GATJAIO
and so on.....
Winning criterion will be code from any language with the fewest characters that produces every possible letter combination.
Additional Notes:
- To make it more interesting, words
can be formed only by using the
letters on a North American Classic
Key Pad phone with three letters
per number as defined here.That means that Z and Q are excluded. - For the number '1', put a space.
- For the number '0', put a hyphen '-'
Bonus points awarded for recognizing output as real English words. Okay, not really. ;-)
Added: Okay, lets go with "Nick's Modified North American Classic Key Pad" which has an 'O' (oh, not zero) on the 6 key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Python,107 个字符
Python,122 个字符(带有reduce 的旧版本)
ps。给我一些“竖起大拇指”,新来的,我需要选票示例
使用:
你可能会注意到我是学校的0->'O',1->'1'。如果这不让您满意,请更改您的字面意思。
Python, 107 chars
Python, 122 chars (older version with reduce)
ps. Gimme some "thumbs up", new here and i need the votes
Example use:
you may notice i am of the school 0->'O', 1->'1'. If that does not please you, change your literal to taste.
Mathematica,87 个字符 -
第 3 版中的奖励轨道(生成有效的英语单词)
此版本包括当前列出的 Q、连字符和空格规则。
Mathematica:98 个字符
请注意冗长的名称
用法
Edit>
它仅输出独特的情况。
要计算唯一序列:
编辑
版本 2:96 个字符
版本 3:附赠曲目 - 生成有效的英语单词
我认为 Mathematica 非常强大,但对于速记比赛来说太冗长了。无论如何,在鞠躬并离开“比赛”之前,我想再展示一个功能,只是为了娱乐,并检查语句 “因将输出识别为真正的英语单词而获得奖励积分。好吧,不是真的。; -)” 是真是假。
代码是:
当用 with
调用时,结果是
当用 with 调用时
,结果是
当用 with 调用时,
结果
很好,不是吗?
Mathematica, 87 characters-
Bonus Track below in version 3 (Generating Valid English Words)
This version includes the Q, hyphen, and space rules, as currently listed.
Mathematica: 98 chars
Please note the loooong names
Usage
Edit>
It outputs only unique cases.
To count the unique sequences:
Edit
Version 2: 96 Chars
Version 3: a Bonus track - Generating Valid English Words
I decided that Mathematica is very powerful but too verbose for a shorthand competition. Anyway, before bowing and leaving the "contest" I want to show up one more function, just for amusement, and for checking whether the statement "Bonus points awarded for recognizing output as real English words. Okay, not really. ;-)" was true or not.
The code is:
When called with
The result is
When called with
The result is
And when called with
The result is
Nice, isn't it?
q,
5239 个字符(cross/)(3 cut"--- ",.QA except"QZ")
q 向左求值的权利。
.QA 包含向量“ABC...XYZ”
,但返回 x 排除 y 中的值
将“---”连接到修改后的字符向量,
切成 3 个
交叉返回所有可能的组合,并应用于所有块
q,
5239 chars(cross/)(3 cut"--- ",.Q.A except"QZ")
q is evaluated left of right.
.Q.A contains the vector "ABC...XYZ"
except returns x excluding values in y
join "--- " to the modified char vector
cut into chunks of 3
cross returns all possible combinations, and is applied across all chunks
J,60
626567个字符使用 Nick 的改良北美经典键盘。
J, 60
626567charactersUses Nick's Modified North American Classic Key Pad.
Haskell:
只是一个纯函数(69
78字符)::用法:
作为一个完整的程序(93
102字符)::用法:
替换
[... ,"PRS","TUV","WXY"]
与[...,"PQRS","TUV","WXYZ"]
获取国际键盘,+2字符。将
print
替换为putStrLn
以去除输出中的引号,+3 个字符。在符号和单词之间添加换行符和空格只是为了删除水平滚动条。
Haskell:
Just a pure function (69
78chars)::Usage:
As a full program (93
102chars)::Usage:
Replace
[...,"PRS","TUV","WXY"]
with[...,"PQRS","TUV","WXYZ"]
to get the international keypad, +2 chars.Replace
print
withputStrLn
to strip the quotation marks in output, +3 chars.Newlines and whitespaces between symbol and words are added only to remove the horizontal scrollbar.
Perl,101 个字符:
这滥用了 glob 运算符
<{a,b,c}{d,e,f}>
来计算笛卡尔积:作为程序(111 个字符):
编辑:合并 mobrule 的建议
Perl, 101 chars:
This abuses the glob operator
<{a,b,c}{d,e,f}>
to calculate the cartesian product:as a program (111 chars):
Edit: incorporated mobrule's suggestions
Python,
157 139119 个字符:制表符和换行符计数...;)
现在使用“尼克的改良键盘”
用法:(+12c)
Python,
157 139119 chars:Tabs and newlines counted... ;)
Now using "Nick's Modified Keypad"
Usage: (+12c)
Ruby,109 个字符
用法:
“2345678”的结果 - 长度 1458
Ruby, 109 characters
Usage:
Result for "2345678" - length 1458
Python 243 个字符
Python 243 chars