ASCIIの宝石をいくつか彫ってください!


25

3月13日は、このチャレンジのテーマであるナショナルジュエルデーとして認められています。だから、整数与えられた0よりも大きい場合、ASCIIの宝石を作成します。例えば:nn

n = 1          n = 2             n = 3
                                       ______
                     ____             /      \
 __                 /    \            \      /
/  \                \    /             \    /
\  /                 \  /               \  /
 \/                   \/                 \/

底は宝石の一番下のものとして定義され\/ます。残りはトップです。上記の例の場合n = 1

Bottom: \  /    Top:   __
         \/           /  \

ご覧のように、底は、宝石の底から最大の線が入った、間にスペースがあるn + 1層で構成され\/てい(1 * lines from the bottom) * 2ますn。2番目の宝石(n = 2)を取得すると、次のことがわかります。

 ____
/    \      
\    /  2 (or n) layers from the bottom with 1*2*2 or 4 spaces in between \/
 \  /   1 layer from the bottom with 1*1*2 or 2 spaces in between \/
  \/    The bottom (0 layers) with 1*0*2 spaces or 0 spaces in between \/

上部は、アンダースコアが上部にあるスペースのペアで構成さ/\n*2ますn*2

ルール

  • ユーザー入力としてゼロ以外の正の整数を取り込むことができる必要があります
  • 上記で定義された仕様で宝石を作成する必要があります(ここで説明します)。
    • 上部は、アンダースコアが上部にあるスペースのペアで構成さ/\n*2ますn*2
    • 一番下は、宝石の一番下から最大の線が入るスペースのあるn + 1層ででき\/てい(1 * lines from the bottom) * 2ますn
  • 宝石の後の改行、または各行の末尾のスペースは許可されます。
  • 標準的な抜け穴は許可されていません

受賞基準

最小バイトが勝ちます!


4
厳密に言えば「非ゼロの正」は冗長です。0を含めたい場合は、「非負」と言う必要があります。
ファンドモニカの訴訟

答えはPETSCIIにありますか?
ショーンビバーズ

3
数字が大きくなるにつれて、「宝石」は宝石のように見えなくなり、ピザのスライスのように見えるようになります。
マリーンスティーブリング

回答:


27

、17バイト

コード:

NβG←β_↙↙¹→↘⁺β¹‖M→

説明:

Nβ                      # Place the input into β
   G←β_                 # Draw a line of length β with _ as the filling character
        ↙                # Move the cursor one down and one left
         ↙¹              # Draw a line from the cursor position to one position ↙
           →             # Move the cursor 1 to the right
             ⁺β¹         # Add one to the input and..
            ↘            # Create a line pointing ↘, with the size calculated above
                ‖M→     # Mirror to the right

非常にきちんとしたコマンドがあり‖M、これも自動的に鏡/の中へ\

チャコールエンコーディングを使用します

オンラインでお試しください!


そのミラーコマンドは本当にクールです!ブラケットや他のキャラクターもミラーリングしていますか?そして、その動作をオーバーライドする方法はありますか?
DJMcMayhem

2
@DJMcMayhem Yes and yes :)
アドナン

27
笑、あなたは炭でダイヤモンドを作りました!
-SteeveDroz

8

05AB1E27 20バイト

ƒN·ð×…\ÿ/}¹·'_×)R.c

オンラインでお試しください!

説明

ƒ                      # for N in range[0 ... n]
 N·ð×                  # push N*2 spaces
     …\ÿ/              # push the string "\ÿ/" with "ÿ" replaced by the spaces 
         }             # end loop
          Â            # push a reversed copy of the top of the stack 
                       # (the largest row of the bottom of the diamond)
           ¹·'_×       # push input*2 underscores
                )      # wrap the stack in a list
                 R     # reverse the list
                  .c   # join the list on newlines, padding each row to equal length

ハハ、いいね!私はあなたが変えることができると考えているD„/\„\/‡Â
アドナン

@アドナン:はい、改善に取り組んでいる間に自分自身に気付いたのです
。P– Emigna

8

Python 2、101 98 95バイト

lambda n:'\n'.join([' '+'__'*n,'/'+'  '*n+'\\']+[' '*i+'\\'+'  '*(n-i)+'/'for i in range(n+1)])

オンラインでお試しください!

正の整数を取り込んで文字列を返す無名関数

Python 3.6、92バイト(Ben Frankelに感謝)

lambda n:f' {"__"*n}\n/{"  "*n}\\\n'+'\n'.join(' '*i+'\\'+'  '*(n-i)+'/'for i in range(n+1))

このバージョンのオンラインインタープリターは見つかりませんでしたが、v3.6のf文字列のために少し短くなっています


Python 3.6では3バイトを保存できますlambda n:f' {"__"*n}\n/{" "*n}\\\n'+'\n'.join(' '*i+'\\'+' '*(n-i)+'/'for i in range(n+1))。F文字列を利用します。
ベンフランケル

私はrepl.itは、Python 3のためのテストスイートがあるかなり確信している
アンソニー・ファム

@AnthonyPham repl.itとTryItOnlineは両方ともPython 3.5を使用していますが、チェックしました
数学

おっと、ついに!Pythonがこれほど長くかかったのはなぜでしょうか。すべての言語は、文字列の補間に値する...
フェリックスDombek

7

PHP、123バイト

echo($s=str_pad)(" ",$z=1+2*$a=$argv[1],_).$s("\n/",$z+1," ")."\\\n";for($i=0;$i<=$a;)echo$s($s("",$i)."\\",$z-$i++)."/\n";

143バイトの最初のバージョン

for(;$i<3+$a=$argv[1];$i++)echo 1-$i?str_pad("",$i?$i-2:1):"/",str_pad($i>1?"\\":"",$i<2?2*$a:2*($a-$i+2)+1,$i?" ":_),$i<2?$i?"\\":"":"/","\n";

ここで試してみてください!


これはどこで試すことができますか?
アンソニーファム

@AnthonyPham こちら
アドナン

119バイトにすることができます:ideone.com/RPCVZe
Tschallacka

@Tschallacka私は唯一のLinux SYTEMを使用することを前提とした場合
イェルクHülsermann

まあ、notepad.exeで編集しない限り、ほとんどのエディターにはLinuxの行末記号があります... i.imgur.com/QZsmf4r.png Windowsコンソールは、\ nを実際の改行として表示します。そう、あなたはあなたの答えの数バイトを剃ることができます。
-Tschallacka

6

V28 27 26バイト

@DJMcMayhemのおかげで、>代わりにÉ

Ài__<esc>É ÙÒ r/Á\Ùr\$r/òÙlxx>

<esc>0x1b

オンラインでお試しください!

Hexdump:

00000000: c069 5f5f 1bc9 20d9 d220 722f c15c d972  .i__.. .. r/.\.r
00000010: 5c24 722f f2d9 6c78 783e                 \$r/..lxx>

説明

上:

Ài__<esc>              " Write argument times __
É<space>               " Prepend a space to the line
Ù                      " Duplicate line below cursor, cursor also moves down
Ò<space>               " Replace every character with a space
r/                     " Change the first character in the line to a /
Á\                     " Append a \ to the end of the line

底:

Ù                      " Duplicate
r\                     " Change the first character in the line to a \
$r/                    " Replace the last character with a /
ò                      " Until a breaking error occurs do:
  Ù                    "  Duplicate
  lxx                  "  Remove 2 middle characters (spaces)
  >                    "  Indent by one space (implicit ending >)
                       " Implicit ending ò

いい答えだ!あなたは変更することができますÉ<space>>、マクロの最後に、暗黙的に記入されている>>
DJMcMayhem

@DJMcMayhem素敵な提案!では、>1つのタブではなく1つのスペースをインデントしますか?
クリチキシリトス

うん!のは、私が持っている原因となるset expandtabset shiftwidth=1
DJMcMayhem


5

JavaScript(ES6)、80バイト

f=
n=>` ${"_".repeat(n*2)}
/${s=" ".repeat(n)}${s}\\`+s.replace(/|/g,"\n$`\\$'$'/")
<input type=number oninput=o.textContent=f(this.value)><pre id=o>


3

Pythonの3、107の 105バイト

n,s=int(input())," "
print(s+n*"__","/"+n*2*s+"\\",*[i*s+"\\"+2*(n-i)*s+"/"for i in range(n+1)],sep="\n")

Stdinからintを取得します


3

MATL、34バイト

QE:qgOO(t~E3O(GQXy3*tPgEhv'_/\ 'w)

MATL Online試しください

説明

QE:qg   % Create array [0 1 1 ... 1 1] of size2*(n+1)
OO(     % Turns last 1 into a 0: [0 1 1 ... 1 0]
t~      % Duplicate and negate: [1 0 0 ... 0 1]
E3O(    % Multiply by 2, turn last 2 into 3: [2 0 0 ... 0 3]
GQXy    % Push identity matrix of size n+1
3*      % Multiply by 3
tPgE    % Duplicate, flip, turn 3 into 2
h       % Concatenate the two matrices horizontally
v       % Concatenate all arrays vertically into a matrix
'_/\ '  % Push this string
w)      % Index (modular, 1-based) with the matrix into the string. Implicitly display

3

PowerShellの76、74のバイト

param($n)" "+'_'*2*$n;"/$(' '*$n*2)\";$n..0|%{' '*($n-$_)+"\$(' '*$_*2)/"}

注:オンラインの例には、デモとして少しラッピングが含まれています。実行するPoSH関数またはスクリプトに配置します。

オンラインでお試しください!


PPCGへようこそ!最初の答えは素晴らしく、他のPowerShellerを見るのもいいですね -あなたはカップルがループ内でインクリメント変数を使用してバイトを保存することができます' '*$i++代わりに' '*($n-$_)
AdmBorkBork

3

C、131バイト

i;f(n){for(printf(" ",i=0);i++<n*2;)printf("_");for(printf("\n/%*c\n",n*2+1,92,i=0);i++<n+1;)printf("%*c%*c\n",i,92,(n-i)*2+3,47);}

オンラインでお試しください!


これはどこでテストできますか?
アンソニーファム

@AnthonyPham Tioリンクが追加されました。
-Steadybox

printfの幅を使用してスペースを埋めるのに最適な方法です。あなたはprintfのためのマクロを作成した場合、9つの以上のバイトを保存する最初のi = 0を削除し、のではなく、新しい変数jを追加することができセカンドランで0 iに再初期化:i,j;f(n){for(p(" ");i++<n*2;p("_"));for(p("\n/%*c\n",n*2+1,92);j++<n+1;p("%*c%*c\n",j,92,(n-j)*2+3,47));}
クラウディウ

@Claudiuありがとう。ただし、関数は初めて呼び出されたときにのみ正しい出力を生成し、IIRCはここでのルールに反します。この関数は、何回呼び出されても機能するはずです。
-Steadybox

@Steadyboxああ、それはごめんなさい。これはすべてのcodegolfの質問に適用されますか?この特定の質問だけを見ると、複数の入力が必要なようには見えません。
クラウディウ

2

Pyth、44バイト

+" "*Q"__"++\/**2Qd\\jm+++*d\ \\**2-Qd\ \/hQ

それを試してみてください!

説明

コードは3つの部分で構成されています。

+" "*Q"__"               # pretty straightforward " "+input()*"__"
++\/**2Qd\\              # d is defined as " ":  "/"+2*input()*d+"\"
jm+++*d\ \\**2-Qd\ \/hQ  # The third part is a bit more complex so I'll explain it further:

jm                   hQ  # Map some lambda function onto range(input()+1) and join the result on newlines
  +++*d\ \\**2-Qd\ \/    # Here d is the lambda argument (so I can't use it for spaces -.-) 
  +++*d\ \\**2-Qd\ \/    # In Python: d*" "+"\\"+2*(Q-d)*" "+"/"

2

Python3、104バイト

n=int(input());print(" "+"__"*n+"\n/"+"  "*n+"\\")
for i in range(n+1):print(" "*i+"\\"+"  "*(n-i)+"/")

プログラムは、STDINから整数を受け取り、宝石をSTDOUTに返します。


2

ピップ、43バイト

42バイトのコード、-nフラグの場合は+1 。

Ps.'_Xa*2P"/\"JsXa*2sX_.'\.sXa-_X2.'/M,a+1

入力をコマンドライン引数として受け取ります。オンラインでお試しください!

説明

最初の2行を個別に作成し、次にマップ操作で残りの宝石を作成します。

Ps.'_Xa*2
      a*2  Cmdline arg, times 2
   '_X     That many underscore characters
 s.        Concatenated to a space character
P          Print (with newline)

P"/\"JsXa*2
        a*2  Cmdline arg, times 2
      sX     That many space characters
 "/\"J       Join the string "/\" with the above as the separator
P            Print (with newline)

sX_.'\.sXa-_X2.'/M,a+1
                  ,a+1  Numbers from 0 up to and including a
                 M      Map the following lambda function:
sX_                      Space, repeated (fn arg) times
   .'\                   Concatenate \
      .                  Concatenate:
       sXa-_              Space, repeated (a - (fn arg)) times
            X2            repeated twice
              .'/        Concatenate /
                         Print result list, newline separated (implicit, -n flag)

別の解決策

また、42 + 1バイト、今度は-lフラグ付き:

Ys.tAL'_.sX2+,a.0(yALRVyRXD1-_)R0'\R1'/ZDs

TIO



2

C、115バイト

#define p printf(
i;j;f(n){for(p" ");i++<n;p"__"));for(p"\n/%*c",2*n+1,92);j<=n;p"\n%*c%*c",++j,92,n*2-j*2+3,47));}

オンラインでお試しください!

C、123バイト

チャレンジには必要ありませんが、8バイトを犠牲にして関数を再利用可能にすることができます(最初のソリューションでは、グローバル変数の暗黙的な初期化に依存して8バイトを節約します)。

#define p printf(
i;f(n){for(i=0,p" ");i++<n;p"__"));for(i=0,p"\n/%*c\n",2*n+1,92);i<=n;p"%*c%*c\n",++i,92,n*2-i*2+3,47));}

オンラインでお試しください!


2

バッチ、152バイト

@set s=
@for /l %%i in (1,1,%1)do @call set s=  %%s%%
@echo  %s: =_%
@echo /%s%\
@set s=\%s%/
:l
@echo %s%
@if %s:~-2%==/ set s=%s:\  = \%&goto l

テスト:

n = 1
 __
/  \
\  /
 \/

n = 2
 ____
/    \
\    /
 \  /
  \/

n = 3
 ______
/      \
\      /
 \    /
  \  /
   \/

これをテストするにはテストスイートが必要です。
アンソニーファム

2

C#、187バイト

もっとコンパクトなソリューションがあると確信していますが、これが私の最初の試みです。

var a=" "+new string('_',2*n)+"\n/"+new string(' ',2*n)+"\\\n";for(int i=n;i>0;i--){a+=new string(' ',n-i)+"\\"+new string(' ',2*i)+"/\n";}a+=new string(' ',n)+"\\/";Console.Write(a);

オンラインでお試しください。


これをテストするにはテストスイートが必要です。
アンソニーファム

1

JavaScript(ES6)、93バイト

n=>(` 0
/2\\`+`
1\\4/`.repeat(k=++n)).replace(/\d/g,c=>' _'[+!+c].repeat(c&1?k-n-2:+c+--n*2))

デモ



1

Perl 5 109 94 + 1(フラグ-pの場合)= 95バイト

オンラインでお試しください!

$l=$_*2;$s=" "."_"x$l."\n/"." "x$l."\\\n";$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;print$s

次のように実行できます:

perl -p <name of file> <<< n

非ゴルフ

$l=$_*2;
$s=" "."_"x$l."\n/"." "x$l."\\\n";
$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;
print$s

説明

#Sets $l to twice the value of the input 'n'
$l=$_*2;  

#Top 2 rows of jewel adding $l underscores then newline  
#followed by '/' and $l spaces.  The '\\\n' is an escaped '\' and a newline
$s=" "."_"x$l."\n/"." "x$l."\\\n";

#The meat of the jewel generation.  It contains a for-loop
#that iterates from 0 to $_ (the input value 'n')
#The loop uses its iterator value ($_ (which overrides the outer $_))
#to determine how many leading spaces it needs to apply.
#Then it adds a '\' with '\\' followed by $l-$_*2 number of spaces
#(the inside of the jewel).  Again, while under the umbrella of the for-loop,
#the $_ refers to the iterator value of the for-loop.
#After the inner spaces, it goes on to add in the '/' and a new line
$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;

#Lastly, it prints the compiled Scalar value $s.  (In Perl, Strings are Scalar values or references
print$s

誰もが非常に理解したり、あなたが述べたこの方法を実行する能力があるように私はこのアウトをテストするためのテストスイートが必要になります
アンソニー・ファム

@AnthonyPham「オンラインで試す」リンク
-CraigR8806
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.