Lir*_*una 36 language-agnostic code-golf rosetta-stone
按字符数计算的最短代码,将根据用户输入输出乐谱.
输入将由一系列字母和数字组成 - 字母将代表注释的名称,数字将代表注释的长度.注释由4个垂直列组成.音符的头部将是一个大写字O杆,如果存在的话将是3行高,由管道字符制成|,并且旗帜将由反斜线制成\.
有效音符长度为无,音符的1/4,音符的1/8,音符的1/16和音符的1/32.
| |\ |\ |\
| | |\ |\
| | | |\
O O O O O
1 1/4 1/8 1/16 1/32
Run Code Online (Sandbox Code Playgroud)
根据他们的笔记名称,笔记是工作人员的地方:
----
D ----
C
B ----
A
G ----
F
E ----
Run Code Online (Sandbox Code Playgroud)
可以假设所有输入都是有效且没有错误 - 每个音符在一行上用空格分隔,至少有一个有效音符.
Input:
B B/4 B/8 B/16 B/32 G/4 D/8 C/16 D B/16
Output:
|\
--------------------------|---|\--------
| |\ |\ |\ | |\ |\
------|---|---|\--|\-----O----|--O----|\
| | | |\ | O |
-O---O---O---O---O----|--------------O--
|
---------------------O------------------
----------------------------------------
Run Code Online (Sandbox Code Playgroud)
Input:
E/4 F/8 G/16 A/32 E/4 F/8 G/16 A/32
Output:
--------------------------------
--------------|\--------------|\
|\ |\ |\ |\
------|\--|\--|\------|\--|\--|\
| | | O | | | O
--|---|--O--------|---|--O------
| O | O
-O---------------O--------------
Run Code Online (Sandbox Code Playgroud)
Input:
C E/32 B/8 A/4 B F/32 B C/16
Output:
------------------------------|\
|\ |\
----------|---|---------------|-
O | | O
---------O----|--O----|\-O------
|\ O |\
------|\--------------|\--------
|\ O
-----O--------------------------
Run Code Online (Sandbox Code Playgroud)
代码计数包括输入/输出(即完整程序).
str*_*ger 23
' '%:A;10,{):y;A{2/.0~|1=~:r;0=0=5\- 7%
4y@--:q' '' O'if-4q&!q*r*{16q/r<'|\\'
'| 'if}' 'if+{.32=y~&{;45}*}%}%n}%
Run Code Online (Sandbox Code Playgroud)
mob*_*mob 22
Perl in 239 226 218 216 183 180 178 172 157 142 136 133 129 128 126 chars
这个126字符的Perl解决方案是我和A. Rex之间长期合作的结果.
@o=($/)x10;$/=$";map{m[/];$p=4+(5-ord)%7;
$_.=--$p?!($p&~3)*$'?16<$p*$'?" |\\":" | ":$/x4:" O ",
$|--&&y@ @-@for@o}<>;print@o
Run Code Online (Sandbox Code Playgroud)
A. Rex还提出了一种与perl -ap交换机一起运行的解决方案.此解决方案中包含111个(!)字符加上额外命令行开关的4个笔划,此解决方案的总分为115.
$\="$:
"x5;$p=4+(5-ord)%7,s#..##,$\=~s#(.)\K$#--$p?
$_*!($p&~3)?"$1|".(16<$p*$_?"\\":$1).$1:$1x4:O.$1x3#gemfor@F
Run Code Online (Sandbox Code Playgroud)
此解决方案的第一个换行是重要的.
或者在shebang行中嵌入开关的122个字符:
#!perl -ap
$\="$:
"x5;$p=4+(5-ord)%7,s#..##,$\=~s#(.)\K$#--$p?$_*!($p&~3)?"$1|".(16<$p*$_?
"\\":$1).$1:$1x4:O.$1x3#gemfor@F
Run Code Online (Sandbox Code Playgroud)
(前两个换行很重要).
可以使用额外的12个字符支持半音符:
@o=($/)x10;$/=$";map{m[/];$p=4+(5-ord)%7;
$_.=--$p?!($p&~3)*$'?16<$p*$'?" |\\":" | ":$/x4:$'>2?" @ ":" O ",
$|--&&y@ @-@for@o}<>;print@o
Run Code Online (Sandbox Code Playgroud)
Kir*_*now 15
从技术上讲,这不符合输出规范,因为输出是一个很好的雕刻PDF而不是一个糟糕的ASCII文本替代品,但我认为问题只是迫切需要一个LilyPond解决方案.实际上,您可以删除"\ autoBeamOff\cadenzaOn\stemUp"以使其看起来更加精美.您还可以在"\ layout {}"之后添加"\ midi {}"以获取要收听的MIDI文件.
o=#(open-file"o""w")p=#ly:string-substitute
#(format o"~(~a"(p"2'1""2"(p"4'1""4"(p"6'1""6"(p"8'1""8"(p"/""'"(p"C""c'"(p"D""d'"(p" ""/1"(p"
"" "(ly:gulp-file"M")))))))))))#(close-port o)\score{{\autoBeamOff\cadenzaOn\stemUp\include"o"}\layout{}}
Run Code Online (Sandbox Code Playgroud)
用法: lilypond thisfile.ly
笔记:
我在LilyPond中做的不多,所以我不确定这是最好的方法,因为它必须将输入转换为LilyPond格式,将其写入辅助文件,然后将其读入.我目前无法使内置的LilyPond解析器/评估器工作.:(
现在正在研究ASCII输出解决方案.... :)
str*_*ger 14
#define P,putchar(
N[99];*n=N;y;e=45;main(q){for(;scanf(" %c/%d",n,n+1)>0;n
+=2);for(;y<11;q=y-(75-*n++)%7 P+q-4?e:79)P*n&&q<4&q>0?
124:e)P*n++/4>>q&&q?92:e))*n||(e^=13,n=N,y++P+10))P+e);}
Run Code Online (Sandbox Code Playgroud)
#define P,putchar(
N[99];*n=N;y;e=45;main(q){for(;scanf(" %c/%d",n,n+1)>0;n
+=2);for(;y<11;q=y-(75-*n++)%7 P+q-4?e:v<4?79:64)P*n&&q<4&q>0?
124:e)P*n++/4>>q&&q?92:e))*n||(e^=13,n=N,y++P+10))P+e);}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ooy 14
167是一个误报,我忘了抑制整个笔记上的茎.
R=raw_input().split()
for y in range(10):
r=""
for x in R:o=y-(5-ord(x[0]))%7;b=" -"[y&1]+"O\|";r+=b[0]+b[o==3]+b[-(-1<o<3and''<x[1:])]+b[2*(-1<o<":862".find(x[-1]))]
print r
Run Code Online (Sandbox Code Playgroud)
虽然在那里有2个填充字符,但是在这一个中没有邪恶之眼的空间,所以我添加了一个笑脸.这种技术利用了音符长度的最后一个字符的独特性,幸运的是我没有1/2音符或1/64音符
R=raw_input().split()
for y in range(10):
r=""
for x in R:o=y-(5-ord(x[0]))%7;b=" -"[y&1]+"O\|";r+=b[0]+b[o==3]+b[-(-1<o<3)]+b[2*(-1<o<":862".find(x[-1]))]
print r
Run Code Online (Sandbox Code Playgroud)
<<o>>Python使用<<o>>邪恶的眼睛算子在这里发挥了很大的作用.find()如果找不到该项,则该方法返回-1,这就是D不需要出现在注释中的原因.
R=raw_input().split()
for y in range(10):
r=""
for x in R:o='CBAGFE'.find(x[0])+4;B=" -"[y%2];r+=B+(B,'O')[o==y]+(x[2:]and
y+4>o>y and"|"+(B,'\\')[int(x[2:])<<o>>6+y>0]or B*2)
print r
Run Code Online (Sandbox Code Playgroud)
11个额外字节给出了带有半音符的版本
R=raw_input().split()
for y in range(10):
r=""
for x in R:t='CBAGFE'.find(x[0])+4;l=x[2:];B=" -"[y%2];r+=B+(B,'@O'[l
in'2'])[t==y]+(l and y+4>t>y and"|"+(B,'\\')[int(l)>>(6+y-t)>0]or B*2)
print r
Run Code Online (Sandbox Code Playgroud)
$ echo B B/2 B/4 B/8 B/16 B/32 G/4 D/8 C/16 D B/16| python notes.py
|\
------------------------------|---|\--------
| | |\ |\ |\ | |\ |\
------|---|---|---|\--|\-----@----|--O----|\
| | | | |\ | @ |
-O---O---@---@---@---@----|--------------@--
|
-------------------------@------------------
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)
159个Ruby字符
n=gets.split;9.downto(0){|p|m='- '[p%2,1];n.each{|t|r=(t[0]-62)%7;g=t[2..-1]
print m+(r==p ?'O'+m*2:p>=r&&g&&p<r+4?m+'|'+(g.to_i>1<<-p+r+5?'\\':m):m*3)}
puts}
Run Code Online (Sandbox Code Playgroud)
n=gets;10.times{|y|puts (b=' -'[y&1,1])+n.split.map{|t|r=y-(5-t[0])%7
(r==3?'O':b)+(t[1]&&0<=r&&r<3?'|'<<(r<t[2,2].to_i/8?92:b):b+b)}*b}
Run Code Online (Sandbox Code Playgroud)
n=gets;10.times{|y|puts (b=' -'[y&1,1])+n.split.map{|t|r=y-(5-t[0])%7
(r==3?'O':b)+(t[1]&&0<=r&&r<3?'|'<<(r<141>>(t[-1]&7)&3?92:b):b+b)}*b}
Run Code Online (Sandbox Code Playgroud)
n=gets.split;10.times{|y|puts (b=' -'[y&1,1])+n.map{|t|r=y-(5-t[0])%7;m=t[-1]
(r==3?'O':b)+(m<65&&0<=r&&r<3?'|'<<(r<141>>(m&7)&3?92:b):b+b)}*b}
Run Code Online (Sandbox Code Playgroud)
这是另一种计算标志的方法,
其中m=ord(last character),#flags=1+m&3-(1&m/4)
另一种方法#flags=141>>(m&7)&3是节省一个字节
n=gets.split;10.times{|y|b=' -'[y&1,1];n.each{|t|r=y-(5-t[0])%7;m=t[-1]
print b+(r==3?'O':b)+(m<65&&0<=r&&r<3?'|'<<(r<141>>(m&7)&3?92:b):b+b)}
puts}
Run Code Online (Sandbox Code Playgroud)
第一次尝试是我的Python解决方案的音译
n=gets.split;10.times{|y|r="";n.each{|x|o=y-(5-x[0])%7
r+=(b=" -"[y&1,1]+"O\\|")[0,1]+b[o==3?1:0,1]+b[-1<o&&o<3&&x[-1]<64?3:0,1]+b[-1<o&&o<(":862".index(x[-1]).to_i)?2:0,1]}
puts r}
Run Code Online (Sandbox Code Playgroud)
合理的短,仍然大部分可读:
let s=Array.init 10(fun _->new System.Text.StringBuilder())
System.Console.ReadLine().Split([|' '|])
|>Array.iter(fun n->
for i in 0..9 do s.[i].Append(if i%2=1 then"----"else" ")
let l=s.[0].Length
let i=68-int n.[0]+if n.[0]>'D'then 7 else 0
s.[i+3].[l-3]<-'O'
if n.Length>1 then
for j in i..i+2 do s.[j].[l-2]<-'|'
for j in i..i-1+(match n.[2]with|'4'->0|'8'->1|'1'->2|_->3)do s.[j].[l-1]<-'\\')
for x in s do printfn"%s"(x.ToString())
Run Code Online (Sandbox Code Playgroud)
简短评论:
// create 10 stringbuilders that represent each line of output
let s=Array.init 10(fun _->new System.Text.StringBuilder())
System.Console.ReadLine().Split([|' '|])
// for each note on the input line
|>Array.iter(fun n->
// write the staff
for i in 0..9 do s.[i].Append(if i%2=1 then"----"else" ")
// write note (math so that 'i+3' is which stringbuilder should hold the 'O')
let l=s.[0].Length
let i=68-int n.[0]+if n.[0]>'D'then 7 else 0
s.[i+3].[l-3]<-'O'
// if partial note
if n.Length>1 then
// write the bar
for j in i..i+2 do s.[j].[l-2]<-'|'
// write the tails if necessary
for j in i..i-1+(match n.[2]with|'4'->0|'8'->1|'1'->2|_->3)do s.[j].[l-1]<-'\\')
// print output
for x in s do printfn"%s"(x.ToString())
Run Code Online (Sandbox Code Playgroud)