代码高尔夫:钢琴
挑战
按字符数计算的最短代码,用于输出从给定长度的输入音符开始的钢琴键盘的一部分。
输入将由开始打印键盘的注释 ([ACDFG]#|[AG]
) 和表示要打印的键长度(包括第一个注释)的正数组成。
第一个键应该完整打印 - 如果它有左锐键,它将被剪切,同样,当开始键是锐键时,左键也会被剪切。
夏普键不计算在内,只计算白键。
测试用例
Input
C 14
Output
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
|____|____|____|____|____|____|____|____|____|____|____|____|____|____|
Input
D# 1
Output
### |
### |
### |
### |
### |
| |
| |
| |
_|____|
Input
A 7
Output
## ### | ### ### | ### ##
## ### | ### ### | ### ##
## ### | ### ### | ### ##
## ### | ### ### | ### ##
## ### | ### ### | ### ##
| | | | | | | |
| | | | | | | |
| | | | | | | |
|____|____|____|____|____|____|____|
代码计数包括输入/输出(即完整程序)。
The challenge
The shortest code by character count to output a part of a piano keyboard starting from input note in a given length.
Input will be composed of a note ([ACDFG]#|[A-G]
) to start printing the keyboard from and a positive number representing length of keys to print including the first note.
The first key should be printed in full - if it has a left sharp key, it will be cut, same for when the start key is sharp, the left key will be cut as well.
A Sharp key is not counted, only white keys.
Test cases
Input
C 14
Output
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| ### ### | ### ### ### | ### ### | ### ### ### |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
|____|____|____|____|____|____|____|____|____|____|____|____|____|____|
Input
D# 1
Output
### |
### |
### |
### |
### |
| |
| |
| |
_|____|
Input
A 7
Output
## ### | ### ### | ### ##
## ### | ### ### | ### ##
## ### | ### ### | ### ##
## ### | ### ### | ### ##
## ### | ### ### | ### ##
| | | | | | | |
| | | | | | | |
| | | | | | | |
|____|____|____|____|____|____|____|
Code count includes input/output (i.e full program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
Perl,133 (129) 字符
Perl,
167160156147142< Strike>133 哦!147144137134133 个字符这是一个 129 个字符的解决方案,借鉴了 molf 的 Ruby 解决方案的几个想法
:来自 Acme::AsciiArtinator 的帮助:
Perl, 133 (129) chars
Perl,
167160156147142133D'oh!147144137134133 charsHere's a 129 char solution, borrowing several ideas from molf's Ruby solution:
and with help from the Acme::AsciiArtinator:
Ruby - 125 个字符
146144140137134126Strike> 125 个字符(第二个换行符不是必需的,添加只是为了避免 SO 上出现滚动条。如果需要,可以用换行符替换分号。)
Ruby 1.9 版本不同,但长度相同(将
a[0]
替换为a.ord
并将"_"
替换为?_
):调用
Ruby - 125 chars
146144140137134126125 chars(The second newline is not necessary and added only to avoid a scrollbar on SO. Semi-colons can be replaced by newlines if desired.)
The Ruby 1.9 version is different but of equal length (replacing
a[0]
bya.ord
and"_"
by?_
):Call with
LilyPond,285
288291310315330333340350个字符为了与音乐主题保持一致,这里有一个用于排版乐谱的语言 LilyPond 的解决方案:
用法:
$ lilypond thisfile.lyoutput.out 2>/dev/null
LilyPond, 285
288291310315330333340350charactersIn keeping with the music theme, here's a solution in a language designed for typesetting music scores, LilyPond:
Usage:
$ lilypond thisfile.ly <input.in >output.out 2>/dev/null
GolfScript - 80 个字符
适合一行 #SO,无需滚动条:)
GolfScript - 81 个字符
格式良好的版本 (27 x 3)
GolfScript - 82 个字符
GolfScript - 85 个字符
格式良好的版本 (17 x 5)
GolfScript - 94 个字符
GolfScript - 98 个字符
GolfScript - 101 个字符
< strong>GolfScript - 109 个字符
GolfScript - 120 个字符
GolfScript - 127 个字符
GolfScript - 80 Characters
Fits in one line of #SO without the scroll bar :)
GolfScript - 81 Characters
Nicely formatted version (27 x 3)
GolfScript - 82 Characters
GolfScript - 85 Characters
Nicely formatted version (17 x 5)
GolfScript - 94 Characters
GolfScript - 98 Characters
GolfScript - 101 Characters
GolfScript - 109 Characters
GolfScript - 120 Characters
GolfScript - 127 Characters
RetroGolf - Applesoft BASIC:236
239245249257245267285为了便于阅读,以多行显示,但应该是单行:
可以使用此 Javascript 中的 Applesoft BASIC 解释器或模拟器。
RetroGolf - Applesoft BASIC: 236
239245249257245267285Shown in multiple lines for readability, but should be a single line:
Can be tested with this Applesoft BASIC Interpreter in Javascript or an emulator.
C# - 315
我坚持用 C# 打高尔夫球,尽管它不是一种非常简洁的语言......
C# - 315
I persist to golf in C# although it's not a very terse language...
蟒蛇 - 164
Python - 164
八度,153
154155158159162172 <罢工>180 <罢工>186 <罢工>185 <罢工>188 <罢工>197 <罢工>199罢工> <罢工>200 <罢工>206 <罢工>207 <罢工>209 <罢工>212 <罢工>214215219240244268个字符为什么只使用 C 或当您可以使用完整的 Octave 进行编程时,使用 C# 或 F#(或 B 或 D)?
(为了清楚起见,每 60 个字符换行一次)
是的......这个解决方案确实计算了字符串的复共轭转置。
用法:$octave -q thisfile.moutput.out
Octave, 153
154155158159162172180186185188197199200206207209212214215219240244268charactersWhy use just C or C# or F# (or B or D) when you can program with a full Octave?
(wrapped every 60 chars for clarity)
Yes ... this solution really does compute the complex conjugate transpose of a string.
Usage:
$ octave -q thisfile.m <input.in >output.out
C — 197
203207216224232240> 字符这个等效的 194 字符版本假定缓冲区溢出没有问题。
C — 197
203207216224232240charactersThis equivalent 194-character version assumes buffer overflow is fine.
PostScript:239
245293312(常规); 219224225231(ASCII85)通过 ASCII85 编码将二进制版本扩展为仅包含 ASCII 可打印字符的 219 字符程序:
用法:$ gs -q -dNOPROMPT -dNODISPLAY -dBATCH thisfile.psoutput.out
PostScript: 239
245293312(regular); 219224225231(ASCII85)Binary version expanded through ASCII85 encoding into a 219-character program with only ASCII printable characters:
Usage:
$ gs -q -dNOPROMPT -dNODISPLAY -dBATCH thisfile.ps <input.in >output.out
F#: 224
225, 226, 248, 252, 270, 276, 299, 306字符我使用 2 的模块来检测空格或井号。
' ' 为 32 % 2 = 0
“#”为 35% 2 = 1
由于我的条件返回 false 零,所以我只是将模块结果相乘。
使用了<|运算符删除一个空格字符。
使用运算符重载来保存另一个字符。
原来的
F#: 224
225, 226, 248, 252, 270, 276, 299, 306CharsI used modules of 2 to detect for a space or pound.
' ' is 32 % 2 = 0
'#' is 35 % 2 = 1
and since my conditional returned zeros for false I just multiplied the modules result.
Used the <| operator to shave off one space char.
Used operator overloading to save another char.
original
sed, 231
235234235237238244 <罢工>268 <罢工>269 <罢工>270 <罢工>276 <罢工>279 <罢工>280罢工> <罢工>282 <罢工>287 <罢工>300 <罢工>307 <罢工>314 <罢工>329338个字符适用于最多 99 个键。标准钢琴有 52 个白键,所以这应该足够了。
示例:
最后一个示例打印标准键盘,以及两端的假想黑键。
sed, 231
235234235237238244268269270276279280282287300307314329338charactersWorks for up to 99 keys. The standard piano has 52 white keys, so this should be sufficient.
Examples:
The last example prints the standard keyboard, along with imaginary black keys on either end.
PianoScript - 2 个字符
这是一行:
用法:
输出:
有关 PianoScript 语言的更多信息可以在 此处。
PianoScript - 2 characters
It's a one-liner:
Usage:
Output:
More information on the PianoScript language can be found here.
JavaScript - 195 个字符
嘿,高尔夫是一项只能与自己竞争的游戏,对吗? :)
gnarf 的解决方案;由 KirarinSnow 移植到 Rhino(进行了微小的修复和格式更改);进一步被粗锉削掉; KirarinSnow 纠正了错误。缓存
k[1]
by cwallenpoole用法:
$ cp input.in 0; rhino thisfile.js
快速 HTML 演示版本:
高尔夫测试 -
添加
readFile=prompt;print=function(a) {document.write("
JavaScript - 195 chars
Hey, golf is a game where you only compete against yourself right? :)
Solution by gnarf; ported to Rhino (with a minor fix and formatting changes) by KirarinSnow; further chipped away by gnarf; error corrected by KirarinSnow. cache
k[1]
by cwallenpooleUsage:
$ cp input.in 0; rhino thisfile.js
Quick HTML Demo Version:
Golf Test -
Adds
readFile=prompt;print=function(a) {document.write("<pre>"+a);}
Python3 - 158
在
input
与raw_input
上保存。在()
上输给print
Python3 - 158
Save on
input
vsraw_input
. Lose on()
forprint
F#:355 个重要字符
全部在一行:
扩展:
F#: 355 significant chars
All on one line:
Expanded:
SETL
165 个字符;翻译 gribblers Python 解决方案。
SETL
165 characters; Translation of gribblers Python solution.
D2(模板):331
370400+ 17 个字符(基于 Ruby 解决方案。)
压缩:
解释:
因为我们无法从
dmd< 传递参数/code> 输入必须在代码中完成。仅支持最多 99 个键。
D2 (templates): 331
370400+ 17 characters(based on the Ruby solution.)
Compressed:
Explained:
Since we can't pass parameters from
dmd
the input must be done in the code. Supports only up to 99 keys.Haskell:
212211208 个字符它仍然假定与 ascii 兼容的字母(特别是序列“@ABCDEFG”),但不再需要 Char.ord
Haskell:
212211208 charactersIt still assumes ascii-compatible letters (specifically, the sequence "@ABCDEFG"), but no longer requires Char.ord
Ruby - 113 个字符
使用命令行参数运行
Ruby - 118 个字符
Ruby - 113 chars
Runs with command line arguments
Ruby - 118 chars
PHP - 208 个字符
必须改进。
输入必须在名为 PIANO 的常量中传递。
PHP - 208 chars
Shall have to be improved.
The input has to be delivered in the constant named PIANO.
F#
414386372 重要字符:哦,还有一个好处,这个脚本实际上可以正确处理“F# 372” - 我不会惹恼不过,您可以将其粘贴到此处...
System.Console.ReadLine() 真是太糟糕了...
F#
414386372 significant characters:Oh, and one bonus, this script will actually handle "F# 372" correctly - I won't annoy you by pasting it here though...
System.Console.ReadLine() is such a bummer...