首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >制作音乐盒

制作音乐盒
EN

Code Golf用户
提问于 2017-03-10 08:35:51
回答 7查看 1.6K关注 0票数 28

您的任务是将一系列字符(音乐)作为输入(在函数或程序中),并打印(或返回)音乐,就像在音乐框中那样。

您将只接收作为输入的字符ABCDEFG.(),并且输入永远不会是空的。如果你愿意的话,你也可以收到小写字母。

这是一个空的音乐盒,长度为3:

代码语言:javascript
复制
.......
.......
.......

正如你所看到的,这些线有7个字符长,而且由于音乐盒的长度是3,我们有3行。这里只有.s,因为音乐盒是空的。让我们放点音乐进去!

首先,我们创建音乐盒。在本例中,输入将是CDAG.DAG

CDAG.DAG的长度是8,所以我们需要一个长度为8的音乐盒:

代码语言:javascript
复制
.......
.......
.......
.......
.......
.......
.......
.......

然后,我们读取输入,每次一个字符,并将一个O放在它各自的位置。

第一个字符是C,每个注释的位置与此相等(我添加了空格以表示清晰度):

代码语言:javascript
复制
 A B C D E F G
 . . . . . . .
 . . . . . . .
 (and so on)

如果输入字符是.,则只需打印空行.......

因此,C将是第三个字符。让我们把它放在音乐盒的顶部:

代码语言:javascript
复制
..O....
.......
.......
.......
.......
.......
.......
.......

对于所有其他字符,我们将重复这个过程(括号中的文本只是为了向您展示说明,您不应该输出):

代码语言:javascript
复制
..O.... (C)
...O... (D)
O...... (A)
......O (G)
....... (.)
...O... (D)
O...... (A)
......O (G)

由于音乐盒是如何工作的,如果我们在输出中使用O.<insert newline here>以外的字符(例如空格),那么它就不会播放正确的音乐!

这是一个和弦:

代码语言:javascript
复制
(ACE)

这个和弦指示我们同时演奏ACE的音符。和弦中永远不会有停顿( .)。

这就是它的写法:

代码语言:javascript
复制
O.O.O...

这就是它在音乐中的表现方式:B(ACE)D

你永远不会收到和弦中的和弦(这是无效的:(AB(CD)EF)或this:A(B()) ),和弦不会是空的(这将无效:A()B )

您将永远不会收到无效的输入。

示例:

代码语言:javascript
复制
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.

允许输出上的尾随/前导空格。

因为这是密码-高尔夫,最短的代码就赢了!

EN

回答 7

Code Golf用户

回答已采纳

发布于 2017-03-11 01:13:57

Pip,29字节

28字节代码,+1表示-l标志。

代码语言:javascript
复制
'.X7RA_'OMz@?a@`\(\w+.|.`@XL

将小写输入作为命令行参数。在网上试试!

解释

代码语言:javascript
复制
                              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)

下面是如何转换输入的示例:

代码语言:javascript
复制
"b.(ceg)"
["b" "." "(ceg)"]
[["b"] [] ["c" "e" "g"]]
[[1] [] [2 4 6]]
[".O....." "......." "..O.O.O"]
票数 2
EN

Code Golf用户

发布于 2017-03-10 09:41:59

批处理,209个字节

代码语言:javascript
复制
@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%

如果最后看到的符号不是(,则通过积累字母和输出行来工作。

票数 4
EN

Code Golf用户

发布于 2020-06-05 00:52:00

Dyalog Unicode,30 字节数

代码语言:javascript
复制
'.O'[⍉(⍳7)∘.∊⍎⍕⎕A⍳@(~∊∘'()')⎕]

在网上试试!

只有1b的Pip解决方案,真可惜!

解释

代码语言:javascript
复制
'.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 '.'
票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/112520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档