您的任务是将一系列字符(音乐)作为输入(在函数或程序中),并打印(或返回)音乐,就像在音乐框中那样。
您将只接收作为输入的字符ABCDEFG.(),并且输入永远不会是空的。如果你愿意的话,你也可以收到小写字母。
这是一个空的音乐盒,长度为3:
.......
.......
.......正如你所看到的,这些线有7个字符长,而且由于音乐盒的长度是3,我们有3行。这里只有.s,因为音乐盒是空的。让我们放点音乐进去!
首先,我们创建音乐盒。在本例中,输入将是CDAG.DAG。
CDAG.DAG的长度是8,所以我们需要一个长度为8的音乐盒:
.......
.......
.......
.......
.......
.......
.......
.......然后,我们读取输入,每次一个字符,并将一个O放在它各自的位置。
第一个字符是C,每个注释的位置与此相等(我添加了空格以表示清晰度):
A B C D E F G
. . . . . . .
. . . . . . .
(and so on)如果输入字符是.,则只需打印空行.......。
因此,C将是第三个字符。让我们把它放在音乐盒的顶部:
..O....
.......
.......
.......
.......
.......
.......
.......对于所有其他字符,我们将重复这个过程(括号中的文本只是为了向您展示说明,您不应该输出):
..O.... (C)
...O... (D)
O...... (A)
......O (G)
....... (.)
...O... (D)
O...... (A)
......O (G)由于音乐盒是如何工作的,如果我们在输出中使用O、.和<insert newline here>以外的字符(例如空格),那么它就不会播放正确的音乐!
这是一个和弦:
(ACE)这个和弦指示我们同时演奏A、C和E的音符。和弦中永远不会有停顿( .)。
这就是它的写法:
O.O.O...这就是它在音乐中的表现方式:B(ACE)D
你永远不会收到和弦中的和弦(这是无效的:(AB(CD)EF)或this:A(B()) ),和弦不会是空的(这将无效:A()B )
您将永远不会收到无效的输入。
B(ACE)D
.O.....
O.O.O..
...O...
B
.O.....
GGABC
......O
......O
O......
.O.....
..O....
...
.......
.......
.......
A..F.C(DA).
O......
.......
.......
.....O.
.......
..O....
O..O...
.......
.(ABCDEF)
.......
OOOOOO.允许输出上的尾随/前导空格。
因为这是密码-高尔夫,最短的代码就赢了!
发布于 2017-03-11 01:13:57
28字节代码,+1表示-l标志。
'.X7RA_'OMz@?a@`\(\w+.|.`@XL将小写输入作为命令行参数。在网上试试!
a is 1st cmdline arg; XL is `[a-z]`; z is lowercase alphabet
a@`\(\w+.|.` List of all matches in a of this regex:
Either a ( followed by letters followed by another
character (i.e. the closing paren), or any one character
@XL For each of those matches, a list of all matches of this
regex (effectively, split the match into a list of
characters and keep only the lowercase letters)
z@? Find index of each letter in the lowercase alphabet
M To that list of lists of indices, map this function:
'.X7 Take a string of 7 periods
RA_ and replace the characters at all indices in the argument
'O with O
Finally, autoprint the resulting list, with each item on
its own line (-l flag)下面是如何转换输入的示例:
"b.(ceg)"
["b" "." "(ceg)"]
[["b"] [] ["c" "e" "g"]]
[[1] [] [2 4 6]]
[".O....." "......." "..O.O.O"]发布于 2017-03-10 09:41:59
@set s=%1
@set p=)
@for %%n in (a b c d e f g)do @set %%n=.
:g
@if %s:~,1% lss @ (set "p=%s:~,1%")else set %s:~,1%=O
@set s=%s:~1%
@if %p%==( goto g
@echo %a%%b%%c%%d%%e%%f%%g%
@if not "%s%"=="" %0 %s%如果最后看到的符号不是(,则通过积累字母和输出行来工作。
发布于 2020-06-05 00:52:00
'.O'[⍉(⍳7)∘.∊⍎⍕⎕A⍳@(~∊∘'()')⎕]只有1b的Pip解决方案,真可惜!
'.O'[⍉(⍳7)∘.∊⍎⍕⎕A⍳@(~∊∘'()')⎕] ⍝
⎕ ⍝ Read input. For below examples, this starts at '(AC).D'
⎕A⍳ ⍝ Converts to the index (⍳) of the alphabet (⎕A)...
@ ⍝ ... For characters which are...
(~∊∘'()') ⍝ ... Not (~) an element (∊) of '()'. Characters outside the alphabet (e.g. dots) will get index 26
⍝ e.g. '(AC).D' -> ( 0 2 ) 26 3
⍕ ⍝ Convert the mixed array (numerics and characters) to characters (⍕)
⍎ ⍝ Evaluate this string. This abuses the remaining brackets to work as groups, e.g. ( 0 2 ) 26 3 -> three array elements (0 2) 26 3
∘. ⍝ For each element, compute the outer product of...
(⍳7) ⍝ Each number 0-6...
∊ ⍝ ... vs a boolean of whether this integer is contained in the corresponding vector. Result is a boolean matrix
⍉ ⍝ Transpose this matrix. The result is now in the correct shape with 1 where 'O' needs to be and 0 where a '.' needs to be
'.O'[ ] ⍝ For each element of the boolean matrix, treat it as an index of the string '.O'. This will turn 1 into 'O' and 0 into '.'https://codegolf.stackexchange.com/questions/112520
复制相似问题