ASCII钢琴键盘


24

钢琴键的宽度为3个字符,高度为7个字符。但是,如果每个键都为3个字符宽,则黑色键的空间将不足。这就是为什么某些白色键的某些部分被切掉的原因。白键有3种类型。

缺少右半(R)的键:

____
|  |
|  |
|  |
|  |
|   |
|   |
|___|

左半边缺少的键(L):

 ____
 |  |
 |  |
 |  |
 |  |
|   |
|   |
|___|

以及缺少左右两半的键(M):

 ___
 | | 
 | | 
 | | 
 | | 
|   |
|   |
|___|

在真正的键盘上,这些模式如下所示:

RMLRMML, RMLRMML, RMLRMML...

并重复总共88个按键。现在,当各个按键分别显示时,您将看不到它,但是将它们推在一起时,您可以看到黑色按键。

_________________________________________________________
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |
|___|___|___|___|___|___|___|___|___|___|___|___|___|___|

你的任务

给定正整数N,用N个白键打印钢琴的ASCII码。您应该能够处理从1到52(含)之间的任何N(因为实际的88键钢琴具有52个白键)。这是从1到8的测试输出,然后模式以类似的方式增加。

1
____
|  |
|  |
|  |
|  |
|   |
|   |
|___|

2
________
|  | | |
|  | | |
|  | | |
|  | | |
|   |   |
|   |   |
|___|___|

3
_____________
|  | | | |  |
|  | | | |  |
|  | | | |  |
|  | | | |  |
|   |   |   |
|   |   |   |
|___|___|___|

4
________________
|  | | | |  |  | 
|  | | | |  |  | 
|  | | | |  |  | 
|  | | | |  |  | 
|   |   |   |   |
|   |   |   |   |
|___|___|___|___|

5
____________________
|  | | | |  |  | | | 
|  | | | |  |  | | | 
|  | | | |  |  | | | 
|  | | | |  |  | | | 
|   |   |   |   |   |
|   |   |   |   |   |
|___|___|___|___|___|

6
________________________
|  | | | |  |  | | | | | 
|  | | | |  |  | | | | | 
|  | | | |  |  | | | | | 
|  | | | |  |  | | | | | 
|   |   |   |   |   |   |
|   |   |   |   |   |   |
|___|___|___|___|___|___|

7
_____________________________
|  | | | |  |  | | | | | |  |
|  | | | |  |  | | | | | |  |
|  | | | |  |  | | | | | |  |
|  | | | |  |  | | | | | |  |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|___|___|___|___|___|___|___|

8
________________________________ 
|  | | | |  |  | | | | | |  |  | 
|  | | | |  |  | | | | | |  |  | 
|  | | | |  |  | | | | | |  |  | 
|  | | | |  |  | | | | | |  |  | 
|   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |
|___|___|___|___|___|___|___|___|

最后但并非最不重要的是,这是完整的52键输出:

_________________________________________________________________________________________________________________________________________________________________________________________________________________
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |
|  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |  | | | | | |  |  | | | |  |
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|

禁止使用标准漏洞,以字节为单位的最短答案将获胜!


什么是“标准漏洞”?
通配符


是否允许在任何/所有行上使用尾随空格?尾随换行符如何?
2016年

1
@AlexL。由于真正的88键钢琴具有55个白键
DJMcMayhem

1
@BMac> _>我不会... <_ <我不知道你在说什么。我 52,瞧!您可以在R̶e̶v̶i̶s̶i̶o̶n̶̶H̶i̶s̶t̶o̶r̶y̶中清楚地看到它,呃,我是说现在的职位状态!无论如何,值得庆幸的是,这可能不会破坏任何现有的答案,因为大多数答案可能无论如何都能达到55+。
DJMcMayhem

Answers:


4

Pyth,68 65 63字节

*lJ:+\|s@Lj;*L" |"_j4536 7*4Q" $"k\_jb+*4]J*2]K+\|*Q"   |":Kd\_

在线尝试!

测试套件。

在此版本中,我只替换了内部的分配(J和K)以节省2个字节。因此,请阅读以下版本。

先前的65字节版本及其说明

J:+\|s@Lj;*L" |"_j4536 7*4Q" $"kK+\|*Q"   |"*lJ\_jb+*4]J*2]K:Kd\_

在线尝试!

J:+\|s@Lj;*L" |"_j4536 7*4Q" $"k    This part generates the most irregular line.

        j;*L" |"_j4536 7            Generate the whole line by black magic
      @L                *4Q         Get the first (4*input) characters of it, with wrapping.
  +\|                               Add "|" in front of it (we took away the first "|")
 :                         " $"k    Replace the ending space by nothing
J                                   Store the line to J.


K+\|*Q"   |"      This part generates the line just below the irregular line.
    *Q"   |"      Repeat "   |" input times
 +\|              Prepend "|"
K                 Store to K


*lJ\_     Now we can actually start printing

*  \_     Repeat "_" ...
 lJ                      [the length of J] times
          (and implicitly print it out)


jb+*4]J*2]K

   *4]J             Repeat J 4 times
       *2]K         Repeat K 2 times
  +                 Concatenate them together
jb                  Join with newlines
                    (and implicitly print it out)

:Kd\_

:K      Replace in K
  d                  " "
   \_                    by "_"
        (and implicitly print it out)

黑魔法

We find the irregular line from input=7, and cut out the first "|":
"  | | | |  |  | | | | | |  |"
 2  1 1 1 2  2  1 1 1 1 1 2

j;*L" |"_j4536 7    Black magic.

         j4536 7    4536 converted to base 7: [1,6,1,4,0]
        _           Reverse: [0,4,1,6,1]
  *L" |"            Repeat " |" <each element> times:
                    [""," | | | |"," |"," | | | | | |"," |"]
j;                  Join by whitespace:
                    "  | | | |  |  | | | | | |  |"

我只是开你的玩笑而已。an吟...
尼尔

11

的JavaScript(ES6),155个 149 147字节

n=>[`_`[r=`repeat`](n*4+(9>>n%7&1)),s=[...Array(n*12/7|0)].map((_,i)=>1998>>i%12&1?` |`:`  |`).join``,s,s,s,s=`   |`[r](n),s,`___|`[r](n)].join`\n|`

其中\n代表文字换行符。利用第一行之后的所有行都以|字符开头的事实。说明:

f=
n=>[                        // Start by building up a list of rows
 `_`[r=`repeat`](n*4+       // 4 underscores per key
  (9>>n%7&1)),              // Third and seventh keys have one extra
 s=[...Array(n*12/7|0)]     // Calculate total of white and black keys
  .map((_,i)=>              // Process each key in turn
   1998>>i%12&1?` |`:`  |`  // Bitmap of narrow and wide keys
  ).join``,                 // Join the keys together
 s,s,s,                     // Repeated 4 times in total
 s=`   |`[r](n),            // Full width part of the white keys
 s,                         // Repeated twice in total
 `___|`[r](n)               // Base of the white keys
].join`\n|`                 // Join the rows together
<input type="number" oninput="o.textContent=f(this.value);"><pre id=o>

编辑:通过修复我对键的高度规范的误读,节省了2个字节。


2
先生,你让我震惊。
罗比·科恩

哦,这很好,您可以添加某种演练吗?
nobe4

如果可以的话,您应该添加一个可运行的代码段。
巴林特

1

Ruby,119个字节

->n{puts ?_*(1+n*4+(0<=>-n%7%4)),(0..6).map{|i|("01"+("%b"%[0xAADAAAD,13][i/4]*99)).tr('10',' |_|'[i/6*2,2])[0,1+n*4]}}

取消测试程序

f=->n{
  puts ?_*(1+n*4+(0<=>-n%7%4)),           #Draw 1+n*4 _'s for top row (one less if -n%7%4>0, black note), then...
  (0..6).map{|i|                          #Cycle through remaining 7 rows
    ("01"+("%b"%[0xAADAAAD,13][i/4]*99)).   #Generate string version of binary number corresponding to pattern, repeat 99 times.
    tr('10',' |_|'[i/6*2,2]                 #Binary 1-> space or underscore. Binary 0 -> | (These choices ensured no leading 0)
    )[0,1+n*4]                              #truncate to the right amount of keys.
  }
}


10.times{|j|f[j]}

1

> <>,188182字节

&0"_"o011.
.\:7%8g95*-"_"o1-:a1@@?.~~~1+:&:&=1+1$
.v~:7=?;ao"|"o1+:0$0$
.>:7%8g"0"-4*:9go1+:9go1+:9go1+9go1+:&:&=3$-1$
 ^
.>"| _"{:}7=?$~::oooo1+:&:&(3*2+1$
>^
^
0120112
  | | | |  |

程序启动时,堆栈上应显示要显示的白键数量。

编辑:我设法通过组合5/6和7行的输出来减少一些字节。先前版本:

&0"_"o011.
.\:7%8g95*-"_"o1-:a1@@?.~~~1+:&:&=1+1$
.v~ao"|"o1+:0$0$.
.>:7%8g"0"-4*:9go1+:9go1+:9go1+9go1+:&:&=3$-1$
 ^
.>"|   "oooo1+:&:&(3*2+1$
 ^
 "|___"oooo1+:&:&=?;
0120112
  | | | |  |

1

PHP,238字节

$n=$argv[1];$s=str_repeat;echo'_'.$s($a=$s('_',28),$m=($n-$r=$n%7)/7).substr($a,0,$k=4*$r-($r&&$r!=3))."\n",$g='|'.$s($b='  | | | |  |  | | | | | |  |',$m).substr($b,0,$k)."\n",$g,$g,$g,$h='|'.$s('   |',$n)."\n",$h,'|'.$s('___|',$n)."\n";

像往常一样,在代码之前加上<?php,将其放入一个PHP文件(将其命名为keyboard.php),然后使用以下命令运行它:

$ php -d error_reporting=0 keyboard.php 55

另外两个字节可以通过挤压的初始化被保存在PHP7 $n$s到他们的第一次使用:

echo'_'.($s=str_repeat)($a=$s('_',28),$m=($n-$r=($n=$argv[1])%7)/7).substr($a,0,$k=4*$r-($r&&$r!=3))."\n",$g='|'.$s($b='  | | | |  |  | | | | | |  |',$m).substr($b,0,$k)."\n",$g,$g,$g,$h='|'.$s('   |',$n)."\n",$h,'|'.$s('___|',$n)."\n";

可以在github上找到未发布的代码,测试套件和其他东西。


1

3 2,191 185 180 182 171 145个 144 133 132字节

def k(n):print"\n|".join(["_"*(4*n+(9>>n%7&1))]+[''.join("   ||"[1998>>j%12&1::2]for j in range(n*12/7))]*4+["   |"*n]*2+["___|"*n])

这可能需要打高尔夫球,但是我已经在代码上摆弄太多了,以至于我可能看不到高尔夫球场在哪里。欢迎任何打高尔夫球的建议。

编辑:误读了按键高度的规格。该错误已修复。

编辑:Neil的 Javascript答案中借用了Neil的12个关键思想,删除了一些括号,并切换到Python 2以节省11个字节。

编辑:进行了大量更改,以使功能下降到一个for循环。

编辑:现在是程序而不是函数。

编辑:现在print"\n|".join()按照尼尔建议使用以保存11个字节。将程序转回一个保存字节的函数。


我认为您可以使用我的"\n|".join把戏再节省10个字节。
尼尔

啊,我忘了删除之间的空间print"\n|"
尼尔

0

C#1683字节

所以....在看到尼尔在上面的回答后,这真令人尴尬,但是我还是把它贴出来,因为这花了我一段时间(祝福)。我用C#创建了我的。在“ Fncs”类中,我创建了典型键顺序的数组。然后,我创建了一个函数,该函数可以允许用户基于给定的整数为该数组获取适当的索引。为了编辑单独的行,我创建了一个“ PianoKeyboard”类,其中包含一个字典,该字典存储了代表各个行的多个字符串。最后,我创建了“ DrawKey”函数,该函数将适当的文本追加到各行,而“ GetKeys”函数则返回整个字符串值。

namespace ASCIIPiano{public enum WhiteKeyType{Left,Middle,Right}public static class Fncs{public static WhiteKeyType[] Order{get{return new WhiteKeyType[]{WhiteKeyType.Left,WhiteKeyType.Middle,WhiteKeyType.Right,WhiteKeyType.Left,WhiteKeyType.Middle,WhiteKeyType.Middle,WhiteKeyType.Right};}}public static PianoKeyboard DrawKey(this PianoKeyboard keyboard, WhiteKeyType type){keyboard.Append(1,"_____");if (type == WhiteKeyType.Left){keyboard.Append( 2,"|  | ");keyboard.Append( 3,"|  | ");keyboard.Append( 4,"|  | ");keyboard.Append( 5, "|  | ");}else if (type == WhiteKeyType.Middle){keyboard.Append(2, " | | ");keyboard.Append(3," | | ");keyboard.Append( 4," | | ");keyboard.Append(5," | | ");}else{keyboard.Append( 2," |  |");keyboard.Append(3, " |  |");keyboard.Append(4," |  |");keyboard.Append(5, " |  |");}keyboard.Append(6,"|   |");keyboard.Append(7,"|   |");keyboard.Append(8,"|___|");return keyboard;}public static int GetWhiteKeyIndex(this int number){return number % 7;}public static string GetKeys(this int quantityofwhitekeys){PianoKeyboard keyboard = new PianoKeyboard();for (int i = 0; i < quantityofwhitekeys; i++){WhiteKeyType key=Fncs.Order[i.GetWhiteKeyIndex()];keyboard.DrawKey(key);}return keyboard.TOTALSTRING;}}public class PianoKeyboard{public PianoKeyboard(){}private Dictionary<int, string> lines = new Dictionary<int, string>();public void Append(int index,string value){if (index > 8 || index < 1){throw new Exception("URGH!");}else{if (lines.Keys.Contains(index)){lines[index] += value;}else{lines.Add(index, value);}}}public string TOTALSTRING{get{string returner = "";foreach (int key in lines.Keys){returner += lines[key] + "\n";}return returner;}}}}

1
嗨,欢迎来到PPCG!您可能已经注意到,代码高尔夫球挑战在于编写尽可能短的代码。因此,诸如C#,Java之类的语言以及基本上所有的OO语言通常都是错误的选择。但是,编写短代码可能是一个很好的做法,因此,我不会阻止您这样做:)至于您的回答,如果您不保留代码长度,这是解决此问题的好方法心神!玩得开心,祝你好运,尝试编写小的C#代码:)
Bassdrop Cumberwubwubwub

@BassdropCumberwubwubwub谢谢,我实际上是这个网站的新手(当然)。Kinda还是编程新手,但我会尽力的^ _ ^
Robbie Coyne,2016年

很高兴看到所有<200字节答案之间有4KB的答案。我个人也喜欢用尽可能短的时间来编写Java代码来应对这些Codegolf挑战,因为我从未使用过任何Codegolf语言。当然,Java和C#永远无法与其他答案竞争,但是尝试创建尽可能短的代码很有趣。这是您可能会发现有趣的帖子:C#中的代码高尔夫技巧。PS:我已将您的代码复制到文件中以查看确切的字节数,即:4,052。;)无论如何,欢迎来到PPCG!
凯文·克鲁伊森

2
欢迎使用PPCG,但此答案必须充分考虑。我看到空白和可以删除的注释。
Rɪᴋᴇʀ

1
1 .:重命名变量,因此它们的长度为1个字符。2 .:始终添加适当的字节数,它必须是字节精确的
巴林特
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.