このチャレンジでは「+」文字を使用します


28

あなたのタスク:番号を指定すると、中心から離れた文字でnある「+」記号が生成さnれます。これがわかりにくい場合は、テストケースを確認してください。

入力の標準方法:出力は文字列または印刷でなければなりません。標準の抜け穴が適用されます。

Input: 1
Output: +           ] 1 away from center `+`.

Input: 2
Output:  +          ] 2 away from center `+`.
        +++         ] 1 away from center `+`.  
         +

Input: 3
Output:   +         ] 3 away from center `+`.
          +
        +++++
          +
          +

これはなので、最短のコードが勝ちます!


結果が正方形になるように、各行の末尾のスペースが許可されると思いますか?
ルイスメンドー

@LuisMendoはい、それで結構です。
同志SparklePony


3
「中心からn文字離れた「+」記号を生成する」-この部分はわかりません。どのセンターに言及していますか?どうして何かがそれ自体に対してエキセントリックなのでしょうか?どうか明らかにしてください。
Wossname

6
センターがそれ自体から0離れていれば、混乱がはるかに少なくなります。
停止ハーミングモニカ

回答:


45

22
この言語は何ですか?
DJMcMayhem

@DJMcMayhem基本的に、タートルグラフィックASCIIアートを使用できます。さまざまな種類のASCII図形(P+=クロスなど)をレンダリングするための便利なビルトインがたくさんあります。
fergusq

それらはマルチバイト文字ではありませんか?
ペタ

3
@Petah Charcoalはカスタムコードページを使用します
ASCIIのみ

@fergusqあなたがして炭を混合される可能性がありますTurtlèd:P、木炭は、タートルグラフィックス言語は、実際にはない
ASCIIのみの


12

JavaScript(ES6)、 67 65 63 60 59バイト

x=>(v=(` `[r=`repeat`](--x)+`+
`)[r](x))+`+`[r](x*2)+`+
`+v;
  • 2つの存在置き換えることによって、保存された2つのバイトx-1を有する第一--x及び第二としますx
  • 2バイトのおかげで保存されたKritixiのLithosを交換する、"\n"`[newline]`
  • user2428118のおかげで3バイトが節約され、最終的repeatにサイズを縮小する方法でエイリアスを作成する方法を見つけるのに役立ちました。(彼女の努力に対してもマリーに敬意を表して)
  • Hermanのおかげで1バイトが間接的に保存されました。

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

f=
x=>(v=(` `[r=`repeat`](--x)+`+
`)[r](x))+`+`[r](x*2)+`+
`+v;
oninput=_=>o.innerText=f(+i.value)
o.innerText=f(i.value=3)
<input id=i min=1 type=number><pre id=o>


1
私は完全にわからないんだけど、私はあなたが交換することができると思う"\n"2つのバッククォートとそれらの間のリテラル改行で
KritixiのLithos

ありがとう、@ KritixiLithos; なぜ自分でそれを考えなかったのか分からない。
シャギー

1
あなたはおそらく、例えば、繰り返しエイリアシングによりバイトを救うことができるように見えますa='repeat',v=.....` `[a]
マリー

ありがとう、マリー。エイリアシングをrepeat()試みましたが、最初の数回の試行が2バイトまたは3バイト大きくなったため、放棄しました!コンピュータの前に戻ったら、もう一度見てみましょう。
シャギー

2
x=>(v=(` `[r='repeat'](--x)+`+<newline>`)[r](x))+`+`[r](x*2+1)+`<newline>`+v
user2428118

9

MATL、11バイト

tZv=&+g43*c

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

例付きの説明

検討してくださいn = 3

t     % Implicitly input n. Duplicate
      % STACK: 3, 3
Zv    % Symmetric range
      % STACK: 3, [1 2 3 2 1]
=     % Equal, element-wise
      % STACK: [0 0 1 0 0]
&+    % All pair-wise additions. Gives a 2D array
      % STACK: [0 0 1 0 0;
                0 0 1 0 0;
                1 1 2 1 1;
                0 0 1 0 0;
                0 0 1 0 0]
g     % Logical: convert non-zero to one
      % STACK: [0 0 1 0 0;
                0 0 1 0 0;
                1 1 1 1 1;
                0 0 1 0 0;
                0 0 1 0 0]
43*   % Multiply by 43 (ASCII for '+'), element-wise
      % STACK: [ 0  0 43  0  0;
                 0  0 43  0  0;
                43 43 43 43 43;
                 0  0 43  0  0;
                 0  0 43  0  0]
c     % Convert to char. Char 0 is displayed as space. Implicitly display.
      % STACK: ['  +  ';
                '  +  ';
                '+++++';
                '  +  ';
                '  +  ']

8

16 13バイト

Nα×+α←↑×+α‖O↘

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

他の炭の回答とは異なるアプローチを使用します。

説明

Nα                       # Take input and store it in variable α
×+α                       # α times write a +
←                         # Go left
↑×+α                      # α times write a + upwards

これで、左上隅が完成し、次のようになります。

  +
  +
+++

‖O↘                      # Reflect-overlap it in a SE direction

最後のステップはこのプログラムの鍵です。プラスの左上部分を使用して、南東方向(右および下)に反映することにより、残りのプラスを生成します。


チャコールには独自のコードページがありますか?これらの文字の多くは、UTF-8では複数バイトです。
TRiG

@TRiGはい、そうです!
Kritixiリソス

8

シェークスピアプログラミング言語749 743バイト

N.Puck,.Page,.Ford,.Ajax,.Act I:.Scene I:.[Enter Puck and Ford]Puck:Listen to thy heart!Ford:You is the difference between a cat and I.Scene V:.[Exeunt][Enter Page and Ajax]Ajax:You is the difference between a cat and Ford.Scene X:.Page:You is the product of Puck and I.Is you as big as zero?If so,you is the sum of the sum of the sum of a big big big big big cat and a big big big cat and a big cat and a cat.If not,you big big big big big cat.Speak thy mind!Ajax:You is the sum of you and a cat.Is you as big as Ford?If not,let us return to Scene X.Page:You is the sum of a big big big cat and a big cat.Speak thy mind![Exit Page][Enter Puck]Ajax:You is the sum of you and a cat.Is you as big as Ford?If not,let us return to Scene V.[Exeunt]

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

編集:公式のSPL実装と互換性のある回答を作成しました-以前は機能しませんでした。

シーン番号が連続している必要がないため、6バイトをゴルフしました。

説明

SPLは、シェークスピアの演劇のように見えるように設計されたエソランです。正の名詞の値は1(ここではcatが使用されます)、負の名詞の値は-1(使用されたものはありませんがはその1つです)です。形容詞は、定数に2を掛けて変更します。

N.

最初のドットまでがすべてタイトルであり、問​​題ではありません。

Puck,.                           row counter
Page,.                           column counter
Ford,.                           input
Ajax,.                           temp

文字は整数変数で、それぞれにスタックもありますが、その機能を使用する必要はありませんでした。

Act I:.
Scene I:.

行為とシーンはgotoラベルとして使用されます

[Enter Puck and Ford]

ちょうど2人のキャラクターが同時にステージにいる場合にのみ役立ちます。

Puck:Listen to thy heart!

数値を読み取り、フォードにそれを記憶させます。

Ford:You is the difference between a cat and I.

ご覧のとおり、SPLではEngrishが有効です。これにより、パックの価値は「猫と私とでは異なります」。しかし、それはどういう意味ですか?catは正の名詞なので、Puck = 1 - Fordです。

Scene II:.
[Exeunt]

Exeuntは単なる「exit」の複数形であり、引数がないと、ステージ上の全員が退出することになります。

[Enter Page and Ajax]
Ajax:You is the difference between a cat and Ford.

それはまたPage = 1 - Ford別の俳優によって話されているのでI間違っています。ループなので、の値をコピーすることはできませんPuck

Scene III:.
Page:You is the product of Puck and I.

今ではかなり簡単です。Ajax = Puck * Page

Is you as big as zero?

「as [adj] as」は==演算子です。

If so,you is the sum of the sum of the sum of a big big big big big cat and a big big big cat and a big cat and a cat.

Ajax == 0 ...の場合、「cat」は1、「big cat」は2、「big big cat」は4などです。単純な定数を代入すると、「32と8と2と1の合計の合計」->「40と2と1の合計の合計」->「42と1の合計」が得られます。 -> "43"、これは+のASCIIです。

If not,you fat fat fat fat fat cat.

それ以外の場合は、「ファットファットファットファットファットキャット」であるため、Ajaxは32の値(スペースのASCII)を取得します。

Speak thy mind!

これは、文字を出力するためのコマンドです。

Ajax:
You sum you and cat.Is you as big as Ford?If not,let us return to Scene III.

これはループ構造です。「あなたと猫を合計します」はページをインクリメントし、if(Page != Ford) goto Scene III。プログラムの残りの部分は同じコンポーネントを使用するため、読みやすい擬似コードバージョンを以下に示します。

シーン1:
    input = [入力番号];
    行= 0-入力+ 1;
Scene2:
    col = 0-入力+ 1;
Scene3:
    temp = row * col;
    if(temp == 0){
        temp = '+';
    } else {
        temp = '';
    }

    putchar(temp);
    ページ=ページ+ 1;
    if(Page!= Ford)goto Scene3;
    Ajax = 10;
    putchar(Ajax);
    パック=パック+ 1;
    if(Puck!= Ford)goto Scene2;

" If not,let us return to Scene III." -1; 4番目の壁を破る:P
ヤコブ


6

Mathematica、39バイト

Print@@@(CrossMatrix[#-1]"+"/. 0->" ")&

CrossMatrixは、1sではなく+s、0スペースではなくsを使用して、必要な形状のマトリックスを生成するビルトインです。その行列にを乗算すると"+"1sをsに変更せずに+sに置き換えます0(明らかに... 0*x = 01*x = x、右?)。次に、を使用してゼロを手動でスペースに置き換えます/. 0->" "。最後に、行列の各行をで印刷しPrint@@@(...)ます。


1
Printそのように使用できることを知りませんでした。
-ngenisis

6

C、69バイト

あまり面白くない...正方形をループして、適切なキャラクターを印刷します。

r,c;f(n){for(r=-n;++r<n;puts(""))for(c=-n;++c<n;putchar(r*c?32:43));}


6

GNU sed104 99バイト

-5 seshoumaraに感謝
+1を含む-r

s/1//;h;:;s/(.*)1/ \12/;t;s/( *2)2(2*)/\1\n\1\2/
t;G;s/1+/&&1/;s/(.*)(\n1*)/&\n\1/;/1/!c+
y/12/++/

入力を単項で取ります

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

s/1//                    # Subtract 1 from input
h                        # Hold onto input
:                        # Start loop
s/(.*)1/ \12/            #   Remove a 1, prepend a space, and append a 2
t                        # Loop until all 1s are 2s
                         # Start Loop (uses the previous label)
s/( *2)2(2*)/\1\n\1\2/   #   Shift all but the first 2 from the last line to a new line 
                         #   E.g.  "  2"      "  2"
                         #         "  222" -> "  2"
                         #                    "  22"
t                        # Loop until all 2s are on their own line
G                        # Append a newline and input
s/1+/&&1/                # Double the number of 1s and append an extra
s/(.*)(\n1*)/&\n\1/      # Copy all of the lines with 2s to the end
/1/!c+                   # If there aren't any 1s print a '+'
y/12/++/                 # Convert all 1s and 2s to +s

+1を使用するs/( *2)2(2*)/\1\n\1\2/s/(.*)(\n1*)/&\n\1/、5バイトを節約できます。ここに示すように、合計スコアは99です
。– seshoumara

5

Luaの113、90のバイト

r,w,p=string.rep,io.read(),io.write;s=r(' ',w-1)p(r(s..'+'..'\n',w-1))p(r('+',w*2-1)..'\n')p(r(s..'+'..'\n',w-1))

r,w=string.rep,io.read()d=w*2-1;for a=1,d do print(a~=w and r(' ',w-1)..'+'or r('+',d))end


5

R、54バイト

@Jarko Dubbeldamのおかげで7バイトを削減:

function(n){a=matrix("",y<-n*2-1,y);a[n,]=a[,n]="x";a}

前の答え:

f=function(n){a=matrix("",n*2-1,n*2-1);a[n,]="x";a[,n]="x";a}

1
関数に名前を付ける必要はないので、function(n){a=matrix("",n*2-1,n*2-1);a[n,]="x";a[,n]="x";a}59バイトになります!
JAD

1
また、あなたが使用してバイトを保存することができますmatrix("",y<-n*2-1,y)
JAD

1
a[n,]=a[,n]="x"動作し、さらにバイトを節約します。
-JAD

scan()関数ではなくプログラムとして使用することで、さらに4バイト節約できますn=scan();a=matrix("",y<-n*2-1,y);a[n,]=a[,n]="+";a
。– rturnbull

4

PowerShell、48バイト

param($n)($x=,(" "*--$n+"+")*$n);'+'*(1+2*$n);$x

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

入力を受け取ります$n--$nで連結されたスペースの文字列を作成することから始め+ます。これは、コンマ演算子を使用して(新しくデクリメントされた)$n回配列に変換されます。その配列は$x、パイプラインにコピーを配置するために括弧に格納およびカプセル化されます。

次に、中間セクションを実行します。これは+、適切な回数でストリング乗算されます。それはパイプラインに残っています。最後に、$x再びパイプラインを使用します。

これらはすべてプログラムの完了時にパイプライン上に残され、暗黙的にWrite-Output要素間に改行が挿入されます。


4

Perl 5、45バイト

44バイトのコード+ -pフラグ。

$_=join"+
",@%=($"x--$_)x$_,"+"x($_*2),@%,""

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


いくつかの同様の(しかし、まだ異なる)アプローチ:

48バイト(47+ -p):

$_=join"+"x($_*2-1).$/,(~~($"x--$_."+\n")x$_)x2

50バイト(49+ -n):

$,="+"x($_*2-1).$/;print+(~~($"x--$_."+\n")x$_)x2


3

CJam、23バイト

ri_(S*'++a\2*(*_z..e>N*

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

説明

これは少し最適ではないと感じますが、アイデアは次の2つのグリッドを重ね合わせることです。

  +
  +
  +
  +
  +



+++++

望ましい結果が得られます。

ri    e# Read input and convert to integer N.
_(    e# Duplicate and decrement.
S*    e# Get a string of N-1 spaces (indentation of the vertical bar).
'++   e# Append a + (the vertical bar).
a     e# Wrap the line in an array.
\2*(  e# Swap with the other copy of N and compute 2N-1.
*     e# Repeat the line that many times.
_z    e# Duplicate the grid and transpose it.
..e>  e# Pairwise maximum between the two grids. This superimposes them.
N*    e# Join with linefeeds.

3

CJam、17歳

ri(S*_]'+*_ffe>N*

オンラインで試す

説明:

ri(      read n, convert to int and decrement
S*       make a string of n-1 spaces
_]       duplicate it and put the 2 strings in an array
'+*_     join the strings with a '+' and duplicate the result
ffe>     for each pair of characters from the 2 (identical) strings,
          get the larger character (results in a matrix)
N*       join the rows with newlines


3

05AB1E15 14 12バイト

F'+}¹·<×)û.c

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

-2エミニャに感謝します。


1
F'+}¹·<×)û.c12のことができます
。-エミグナ

オンラインのリンクを試してみてください-投稿の現在のリビジョンにリンクしていないため、上記のテストケースと一致しない「悪い出力」が表示されます。
トーマス区

@ThomasWard:良いキャッチ!リンクを修正しました。
エミグナ


2

JS(ES6)、88 74 73バイト

x=>(x--,y=y=>(" ".repeat(x)+`+
`).repeat(x),y``+"+".repeat(x+x+1)+"\n"+y``)

おそらくもっとゴルフできるでしょう。

Snippetify(x=>(x--,y=y=>(" ".repeat(x)+`+
`).repeat(x),y``+"+".repeat(x+x+1)+"\n"+y``))
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "number">
<pre data-output></pre>


1
I'm not entirely sure, but I think you can replace the "\n" with two backticks and a literal newline between them
Kritixi Lithos

Only seeing this solution now, you beat me by a few minutes. What's the etiquette around here on similar solutions in the same language that are posted within a small window of time?
Shaggy

2

JavaScript (ES6), 60 bytes

f=
n=>(r=([s,t])=>(s=s.repeat(n-1))+t+s+`
`)([r(` +`),r(`++`)])
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>

Outputs two trailing newlines. Alternative formulation, also 60 bytes:

n=>(r=a=>(s=a[0].repeat(n-1))+a[1]+s+`
`)([r(` +`),r(`++`)])

2

PowerShell, 48

Doesn't seem to get shorter than that (and pretty much the same approach as the other solution):

($a=,(' '*($n="$args"-1)+'+')*$n)
'+'+'++'*$n
$a

or

($a=(' '*($n="$args"-1)+'+
')*$n)+'++'*$n+"+
$a"


2

REXX, 81 bytes

arg a
b=a*2-1
do i=1 to b
  if i=a then say copies('+',b)
  else say right('+',a)
  end

2

PHP, 68 Bytes

for(;$i<$c=-1+2*$m=$argn;)echo"\n".str_pad("+",$c," +"[$m==++$i],2);

83 Bytes

for(;$i<($c=($n=$argn)*2-1)**2;)echo$i%$c?"":"\n".!++$k," +"[$k==$n|$i++%$c==$n-1];

1
You can save a few bytes by using $m=$argn and pre-incrementing $i rather than post incrementing it. you can also save a byte by moving the $m assignment ot the end and dropping the brackets.
user59178

@user59178 I could not understand what you exactly mean
Jörg Hülsermann

1
for(;$i<$c=-1+2*$m=$argn;)echo"\n".str_pad("+",$c," +"[$m==++$i],2);
user59178

while(++$y<2*$n=$argn)echo"\n",str_pad("+",$n*2-1," +"[$y==$n],2); 66 bytes (and save one more with a physical linebreak)
Titus


2

Brain-Flak, 216 + 1 = 217 bytes

+1 bytes from the -A flag

([{}]())(()()){<>(((((()()()()()){})){}()){}())<>(({}[()])<({}<(({})){({}()<(({}<<>(({}<(({})<>)>)<>)<>>)<{({}()<(((((()()){}){}){}){})>)}{}>)>)}{}>){(<{}(({}<<>({}<({}<>)>)>)<(({}){}){({}()<(({}))>)}{}>)>)}{}>)}{}{}

Try it online!

Explanation to come

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