19
\$\begingroup\$

Your task is to generate boxes using any one ASCII character with respect to the inputs given.

Test Cases

1 1   --> =====
          =   =
          =====

1 2   --> =========
          =   =   =
          =========

2 1   --> =====
          =   =
          =====
          =   =
          =====

2 2   --> =========
          =   =   =
          =========
          =   =   =
          =========

2 5   --> =====================
          =   =   =   =   =   =
          =====================
          =   =   =   =   =   =
          =====================

Input

  • Input can be taken from one of the following

    • stdin
    • Command-line arguments
    • Function arguments (2 arguments, one for each number)
  • Input, if taken from stdin or command line arguments, will contain two positive integers, seperated by a space.

  • The two numbers denote the number of boxes in each column and row

Output

  • Boxes must be outputted in stdout (or closest equivalent)
  • Each box should have three horizontal spaces in them

Rules

  • Both the numbers will be greater than 0, but will not go beyond 1000
  • Any visible character can be used for outputting the boxes. (as long as they aren't too harsh on the eye!)
  • You are permitted to write a full program or a function.
  • There should be no unnecessary characters except an optional trailing newline character.

Scoring

This is , so the shortest submission (in bytes) wins.

\$\endgroup\$
0

34 Answers 34

1
2
1
\$\begingroup\$

GolfScript, 48 bytes

2 5:b;:a;0`:z;{4b*1+z*n+}:r~{{z' '3*}b*z+n+r}a*

Takes the size as the first two inputs (here the first two digits).

Demonstration

000000000000000000000
0   0   0   0   0   0
000000000000000000000
0   0   0   0   0   0
000000000000000000000
\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 157 156 152 (123 120 118 117 with unary)

This works with Alex Pankratov's bff (brainfuck interpreter used on SPOJ and ideone) and Thomas Cort's BFI (used on Anarchy Golf).

The input must contain a single pair of integers (in decimal) separated by a space, without a trailing newline.

Demonstration on ideone.

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

Brief explanation:

At the end of first loop we are left with

r 0 c [0]

We then change this to

[r+1] 0 c 0 33 33 11 0

and begin the main loop. In that loop, the tricky part is alternating between 33 (exclamation mark) and 32 (space), and alternating between decrementing and not decrementing the number of rows. This is done with a flag using the cell to the right of c.

Update:

I had not considered unary input until reading some other answers. Here is a unary version:

+
[
  ->++++[<-------->-]
  <[<+<]
  >>,+
]
+++++++++++[>+++>+++>+<<<-]
<<<
[
  >>
  [<+> >>>.<...<<-]
  >+>+>.>-.+
  [<]
  <
  [
    <->
    >>>--<,<
  ]
  <[>+<-]
  <
]
\$\endgroup\$
1
\$\begingroup\$

Haskell, 153

At least it's beating Java!

s r c
 |r==1=a c
 |otherwise=s (r-1)c++(take 2$tail(a c))
 where
 a 1=[f,"=   =",f]
 a n=map(\x->x++((take 4.tail)x))(a(n-1))
 f="====="

p r=unlines.s r

Works in ghci, although ghci doesn't take a new line on \n.

Here is the ungolfed version:

import System.Environment

xsquare :: Int -> [String]
xsquare 1 = ["=====","=   =","====="]
xsquare n = map (\x -> x++((take 4 . tail) x)) (xsquare (n-1))

ysquare :: Int -> [String] -> [String]
ysquare 1 x = x
ysquare n x = ysquare (n-1) (x ++ (take 2 $ tail x))

squares :: Int -> Int -> [String]
squares x y = ysquare y (xsquare x)

main = do
  a <- getArgs
  let [x,y] = (map read . take 2) a
  putStrLn $ unlines $ squares y x

It calculates the horizontal shape first, then finds the vertical shape by adding the last two lines of the horizontal shape the appropriate number of times. The main function is so I could test it properly with command line arguments and proper newline behaviour.

\$\endgroup\$
1
\$\begingroup\$

Lua, 95 93 85 bytes.

Works with command-line arguments.

Gotta love those string methods

a=arg[2]c="="l=c:rep(a*2+1)print((l.."\n"..(("= ")):rep(a)..c.."\n"):rep(arg[1])..l)
\$\endgroup\$
1
  • \$\begingroup\$ You can replace c..(" "):rep(3) by ("= ") \$\endgroup\$
    – ECS
    Commented Jun 17, 2015 at 13:27
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.