Secara dinamis membuat kotak


22

Tantangan:

Gambar kotak ASCII persegi panjang: []

Aturan:

Mengambil input lebar dan tinggi

Anda dapat menganggap keduanya adalah angka

Harus menghasilkan string dengan karakter baris baru, \ n

Contoh:

2, 2:

[][]
[][]

2, 3:

[][]
[][]
[][]

Bytes Paling Sedikit menang.


2
Posting pertama yang bagus! Selamat datang di PPCG!
MD XF

1
Dapatkah saya berasumsi bahwa angkanya positif? Mungkinkah ada trailing baris baru?
dzaima

@dzaima Bilangan bulat positif, tidak ada barang tertinggal atau terkemuka
Robinlemon

dapatkah kita mencetak ke konsol atau apakah kita perlu mengembalikan string?
Giuseppe

5
bagaimana jika kita benar-benar tidak dapat mencetak jejak baru? itu cenderung menjadi praktik yang baik untuk memungkinkan satu baris tambahan
Destructible Lemon

Jawaban:


6

SOGL , 5 byte

Ƨ[]*∙

Sederhana:

Ƨ[]    push "[]"
   *   multiply horizontally (repeating width times)
    ∙  get an array with input (height) items of that
       implicitly output the array joined with newlines

4

Mathematica, 26 byte

Grid@Table["[]",{#2},{#}]&

Apakah Gridobjek Mathematica dihitung sebagai "string dengan karakter baris baru"?
David Zhang

4

MATL , 7 byte

v&DiiX"

Cobalah online!

Penjelasan

v    % Concatenate the (non-existing) stack contents: gives []
&D   % String representation: gives '[]'
ii   % Take two inputs
X"   % Repeat those numbers of times vertically and horizontally. Implicit display

4

Pyth - 7 5 byte

-2 byte oleh trik pintar berkat insert_name_here

VE*`Y

Coba di sini

Penjelasan:

VE*`Y
V      # Loop
 E     # <input> number of times
   `Y  # String representation of empty list (used to be "[]", but insert_name_here pointed out this shorter alternative)
  *    # repeat string implicit input number of times
       # implicit print

3
Anda dapat menyimpan 2 byte dengan menggunakan `Y(representasi string dari daftar kosong), bukan "[]".
insert_name_here

@insert_name_here Ingenious !! Saya memperbarui jawabannya. Terima kasih telah menunjukkan itu!
Maria

1
Datang dengan kode persis ini secara independen. Bagus sekali.
isaacg

4

C, 47 46 byte

f(w,h){for(h*=w;h--;)printf(h%w?"[]":"[]\n");}

atau

f(w,h){for(h*=w;h--;)printf("[]%c",h%w?0:10);}

Upaya golf kode pertama saya, apakah saya melewatkan sesuatu yang jelas?


Ada ini untuk 45, tetapi memiliki baris baru di awal:f(w,h){h*=w;while(h--)printf("\n[]"+!(h%w));}
Conor O'Brien

Itu hanya berfungsi ketika lebar adalah 2.
dbandstra

Begitulah, kesalahanku
Conor O'Brien

Golf pertama yang hebat! Selamat datang di situs ini!
MD XF

1
Tidakkah menggunakan forloop akan mempersingkat kodenya lagi?
Spikatrix

3

05AB1E , 6 byte

F„[]×,

Cobalah online!

Penjelasan

Input diambil sebagai height, width

F         # height times do
 „[]      # push "[]"
    ×     # repeat width times
     ,    # print with newline

3

; # + , 197 byte

>;;;;;;~++++++++:>~;;;;:>~*(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)-::<-::::>-::(;)::>-::*(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)-:<~<;;;;;-+>-:<-:-(-:::~<-:::(~<#<-;;-#~;)-:<#-::<;>-:-)

Cobalah online! Membutuhkan nol byte setelah setiap nomor input.

Saya agak tidak tahu cara kerjanya. Apa yang dapat saya katakan adalah bagian kode ini:

 *(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)-::<-::::>-::(;)::>-::*(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)

mengurai nomor input.


3

brainfuck, 145 byte

+++++++++[>++++++++++<-]>+[>+>+<<-]>>++>,>+++++++++[<----->-]<--->>>,>+++++++++[<----->-]<--->++++++++++<[<<<[>+>+<<-]>[<<<.>.>>-]>[<<+>>-]>>.<-]

Cobalah online!

Golf kode pertama saya! Yay!

Input dalam ascii + 48, jadi untuk melakukan 50, 50 Anda harus memasukkan b, b (huruf ascii untuk 98)

Penjelasan

+++++++++[>++++++++++<-]>+ Get the opening square bracket into first position
[>+>+<<-] Get it into the second and third position
>>++ Get the third position to be the closing bracket
>
,>+++++++++[<----->-]<--- Get first number into fourth cell
>>>
,>+++++++++[<----->-]<--- Get second number into seventh cell
>++++++++++ get newline into 8th position
<

[ Start our height loop
<<<[>+>+<<-] Get the width into the fifth and sixth positions
>[ Start our width loop at the fifth position
<<<.>. Print the second and third positions
>>-] Decrement the fifth position
>
[<<+>>-] copy the sixth position into the fourth position
>>. print newline
<-]

Impresif. Selamat datang di situs ini! :)
DJMcMayhem

Mengapa input ASCII + 48? Anda dapat menyimpan banyak byte hanya dengan menggunakan input ASCII + 0 (mungkin menghubungkan ke versi ASCII + 48 untuk kegunaan)
CalculatorFeline

Saya hanya ingin memenuhi kriteria untuk input, @calculatorFeline
vityavv

... Oh benar. : |
CalculatorFeline

2

J , 12 byte

'[]'$~],+:@[

Cobalah online!

Penjelasan

'[]'$~],+:@[   input: y, x
        +:@[   double y
      ],       pair with x
               this gives (x, 2y)
    $~         shape the left argument into the right argument's shape
'[]'           2-length character string

Ini memberi kita xdengan 2ystring mengulangi []karakter.




2

Jelly , 7 byte

ẋ⁾[]ẋ$Y

Tautan diad mengembalikan daftar karakter (atau program lengkap mencetak hasilnya).

Cobalah online!

Bagaimana?

ẋ⁾[]ẋ$Y - Main link: number w, number h          e.g. 2, 3
ẋ       - repeat w h times                            [2,2,2]
     $  - last two links as a monad:
 ⁾[]    -   literal ['[',']'],                        "[]"
    ẋ   -   repeat list (vectorises)                  ["[][]","[][]","[][]"]
      Y - join with newlines                          "[][]\n[][]\n[][]"
        - if a full program, implicit print




2

Ohm , 9 byte

M"[]"َJ,    

Cobalah online!

Penjelasan

M"[]"َJ,
M         //Executes code input1 times
 "[]"     //Pushes []
     َ   //Duplicates [] input2 times
       J  //Joins the stack
        , //Prints with a trailing newline

2

PowerShell, 25 Bytes

param($w,$h),("[]"*$w)*$h

-3 Terima kasih untuk Mathias!


You can shorten it to 25 like so: param($w,$h),("[]"*$w)*$h
Mathias R. Jessen

2

Japt, 13 12+1= 14 13 bytes

+1 for the -R flag.

"[]"pN× òU*2

Try it online

  • 1 byte saved thanks to obarakon.

Being a little drunk can sometimes help your programming skills though :P
ETHproductions

@ETHproductions: the very cartoon I was looking for but was too drunk to find!
Shaggy

Haha, hope you guys are having a fun night. fyi, U*V can be shortened to
Oliver

1
@obarakon: That's 2 opportunities to work with N last night. Never drink & golf, kids!
Shaggy

2

APL (Dyalog), 11 bytes

'[]'⍴⍨⊢,2×⊣

Try it online!

'[]' the string

⍴⍨ cyclically repeated to fill the shape

 right argument (rows)

, and

 twice

the left argument (columns)


2

Charcoal, 8 7 bytes

EN×[]Iη

Try it online! Link is to verbose version of code. Takes input in the order height, width. Charcoal's drawing primitives aren't suited to this, so this just takes the easy way out and repeats the [] string appropriately. Explanation:

 N      First input as a number
E       Map over implcit range
      η Second input
     I  Cast to number
   []   Literal string
  ×     Repeat
        Implicitly print on separate lines

Well it has drawing primitives for this but still 8 bytes :P
ASCII-only

@ASCII-only Sorry, I didn't realise that Oblong worked on arbitrary strings. Neat!
Neil

@ASCII-only Oh, and what's the verbose name of the predefined empty string variable?
Neil


@ASCII-only Then what am I doing wrong here: Try it online!
Neil

1

R, 70 bytes

p=paste
function(w,h)p(rep(p(rep('[]',w),collapse=''),h),collapse='
')

Try it online!

Returns an anonymous function that constructs and returns the string.

45 bytes, non-conforming

function(w,h)write(matrix('[]',w,h),'',w,,'')

An anonymous function that prints out the string in the desired format.

Try this online


1

Japt, 7 bytes

6 bytes of code, +1 for the -R flag.

VÆç"[]

Doesn't work in the latest version due to a bug with ç, but it does work in commit f619c52. Test it online!

Explanation

VÆ   ç"[]
VoX{Uç"[]"}  // Ungolfed
             // Implicit: U, V = input integers
VoX{      }  // Create the range [0...V) and replace each item X with
    Uç"[]"   //   U copies of the string "[]".
-R           // Join the result with newlines.
             // Implicit: output result of last expression


1

QBIC, 14 bytes

[:|?[:|?@[]`';

Explanation:

[:|     FOR a = 1 to (read input from cmd line)
?       PRINT a newlne
[:|     FOR c = 1 to (read input from cmd line)
?@[]`   PRINT A$ (containing the box)
';         and inject a semicolon in the compiled QBasic code to suppress newlines

This takes its arguments in the order of #rows, #cols. Output starts with a newline.




1

C#, 78 bytes

(w,h)=>"".PadLeft(h).Replace(" ","".PadLeft(w).Replace(" ","[]")+'\n').Trim();

Run in C# Pad

This is shorter than with for-loops and I'm not aware of any function in C# which can repeat with less code.



1

JavaScript (ES6), 43 36 bytes

From the comments, a trailing newline is now permitted.

w=>h=>("[]".repeat(w)+`
`).repeat(h)

Try it

f=
w=>h=>("[]".repeat(w)+`
`).repeat(h)
oninput=_=>o.innerText=f(+i.value)(+j.value);o.innerText=f(i.value=2)(j.value=2)
*{font-family:sans-serif;}
input{margin:0 5px 0 0;width:50px;}
<label for=i>w: </label><input id=i type=number><label for=j>h: </label><input id=j type=number><pre id=o>



Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.