35
\$\begingroup\$

An Octothorpe, (also called number sign, hash or hashtag, or pound sign) is the following ASCII character:

#

Isn't that a fun shape? Lets make bigger versions of it! So here is your challenge:

Given a positive integer N, output an ASCII hashtag of size N.

For example, an ASCII hashtag of size 1 looks like this:

 # # 
#####
 # # 
#####
 # # 

Trailing whitespace on each line is allowed, but not required.

The input will always be a valid positive integer, so you don't have to handle non-numbers, negative, or 0. Your output can be in any reasonable format, so outputting to STDOUT, returning a list of strings, or string with newlines, a 2D matrix of characters, writing to a file, etc. are all fine.

Test cases

2:
  ##  ##
  ##  ##
##########
##########
  ##  ##
  ##  ##
##########
##########
  ##  ##
  ##  ##

3:
   ###   ###   
   ###   ###   
   ###   ###   
###############
###############
###############
   ###   ###   
   ###   ###   
   ###   ###   
###############
###############
###############
   ###   ###   
   ###   ###   
   ###   ###   

4:
    ####    ####    
    ####    ####    
    ####    ####    
    ####    ####    
####################
####################
####################
####################
    ####    ####    
    ####    ####    
    ####    ####    
    ####    ####    
####################
####################
####################
####################
    ####    ####    
    ####    ####    
    ####    ####    
    ####    ####    

5:
     #####     #####     
     #####     #####     
     #####     #####     
     #####     #####     
     #####     #####     
#########################
#########################
#########################
#########################
#########################
     #####     #####     
     #####     #####     
     #####     #####     
     #####     #####     
     #####     #####     
#########################
#########################
#########################
#########################
#########################
     #####     #####     
     #####     #####     
     #####     #####     
     #####     #####     
     #####     #####     

Since this is a code-golf, try to write the shortest possible solution you can, and above all else, have fun!

\$\endgroup\$
  • 1
    \$\begingroup\$ Related \$\endgroup\$ – pppery Aug 17 '17 at 21:38

45 Answers 45

21
\$\begingroup\$

MATL, 20 16 12 11 bytes

3 bytes thanks to DJMcMayhem.

1 byte thanks to Luis Mendo.

21BwY"&*~Zc

Try it online!

Explanation

    % stack starts with input e.g. 2
21  % push 21 to stack             2 21
B   % convert to binary            2 [1 0 1 0 1]
w   % swap                         [1 0 1 0 1] 2
Y"  % repeat                       [1 1 0 0 1 1 0 0 1 1]
&*  % one-input multiplication    [[1 1 0 0 1 1 0 0 1 1]
                                   [1 1 0 0 1 1 0 0 1 1]
                                   [0 0 0 0 0 0 0 0 0 0]
                                   [0 0 0 0 0 0 0 0 0 0]
                                   [1 1 0 0 1 1 0 0 1 1]
                                   [1 1 0 0 1 1 0 0 1 1]
                                   [0 0 0 0 0 0 0 0 0 0]
                                   [0 0 0 0 0 0 0 0 0 0]
                                   [1 1 0 0 1 1 0 0 1 1]
                                   [1 1 0 0 1 1 0 0 1 1]]
~   % complement                  [[0 0 1 1 0 0 1 1 0 0]
                                   [0 0 1 1 0 0 1 1 0 0]
                                   [1 1 1 1 1 1 1 1 1 1]
                                   [1 1 1 1 1 1 1 1 1 1]
                                   [0 0 1 1 0 0 1 1 0 0]
                                   [0 0 1 1 0 0 1 1 0 0]
                                   [1 1 1 1 1 1 1 1 1 1]
                                   [1 1 1 1 1 1 1 1 1 1]
                                   [0 0 1 1 0 0 1 1 0 0]
                                   [0 0 1 1 0 0 1 1 0 0]]
Zc  % convert 0 to spaces            ##  ##  
      1 to octothorpes               ##  ##  
      and join by newline          ##########
                                   ##########
                                     ##  ##  
                                     ##  ##  
                                   ##########
                                   ##########
                                     ##  ##  
                                     ##  ##  
\$\endgroup\$
  • 1
    \$\begingroup\$ You can use Zc instead of 35*c and ~ (logical NOT) instead of 0= \$\endgroup\$ – DJMcMayhem Aug 17 '17 at 20:12
  • 1
    \$\begingroup\$ @DJMcMayhem @_@ why is that a built-in \$\endgroup\$ – Leaky Nun Aug 17 '17 at 20:13
  • 1
    \$\begingroup\$ Actually, the reason that's a builtin is really interesting. I could be wrong, but I think conor suggested it, and Suever wrote a script that looks at all the MATL answers to see what functions are more common for future improvements. Zc was just added \$\endgroup\$ – DJMcMayhem Aug 17 '17 at 20:15
  • \$\begingroup\$ Also, since each cell just has to be non-zero, you could do Q instead of 2< \$\endgroup\$ – DJMcMayhem Aug 17 '17 at 20:18
  • 1
    \$\begingroup\$ @LeakyNun You can change !t* to &*. The latter means "one-input multiplication", which multiplies (element-wise) the input by its transpose \$\endgroup\$ – Luis Mendo Aug 18 '17 at 1:50
14
\$\begingroup\$

Brain-Flak, 420 bytes

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

Try it online!

No, the score of 420 was not intentional. I promise. Readable version:

# 3 Times...
(()()())
{
({}<

    #Duplicate the input
    (({}))

    #Input times...
    {
        ({}<

        #Switch to the main stack
        <>

        #Grab the duplicate of the input
        (<>({})<>)

        #That many times...
        {({}<

            # Push a hash
            ((((()()()()){}){}()){}())

        >[()])}{}

        #Grab the duplicate of the input
        (<>({})<>)

        #That many times...
        {({}<

            #Push a space
            ((((()()()()){}){}){})

        >[()])}{}

        #Grab the duplicate of the input
        (<>({})<>)

        #That many times...
        {({}<

            # Push a hash
            ((((()()()()){}){}()){}())

        >[()])}{}

        #Grab the duplicate of the input
        (<>({})<>)

        #That many times...
        {({}<

            #Push a space
            ((((()()()()){}){}){})

        >[()])}{}

        #Push a newline
        ((()()()()()){})

        #Toggle back to the alternate stack
        <>

        #Decrement the (second) loop counter
        >[()])

    #Endwhile
    }

    #Pop the now zeroed loop counter
    {}

    #Turn [a] into [a, a*5, a]
    ((({}))<(({})(({}){}){})>)

    #A times....
    {
        ({}<

        #Toggle back over
        <>

        #Grab a*5
        (<>({})<>)

        #That many times...
        {({}<

            #Push a space
            ((((()()()()){}){}()){}())

        >[()])}{}

        #Push a newline
        ((()()()()()){})

        #Toggle back
        <>

        #Decrement the (second) loop counter
        >[()])

    }

    #Pop the loop counter and the a*5
    {}{}

#Decrement the outer loop counter
>[()])
}

#Pop the zeroed loop counter
{}

#Pop a over
({}<>)

#Pushes (a**2) * 5 + a
(({})((({({})({}[()])}{})){}){}{})

#That many times...
{({}<

    #Pop a character off the output stack
    {}

>[()])}
\$\endgroup\$
13
\$\begingroup\$

6502 machine code (C64), 59 56 bytes

00 C0 20 9B B7 A9 06 85 FC 86 FE A6 FE 86 FD A9 03 4D 1F C0 8D 1F C0 C6 FC D0
01 60 A9 23 A0 05 49 00 20 D2 FF CA D0 FA A6 FE 88 D0 F3 A9 0D 20 D2 FF C6 FD
D0 E6 F0 D3

Online demo

Usage: SYS49152,N where N is a number between 1 and 255.

(values greater than 4 will already be too large for the C64 screen, starting from 8, the output is even too wide)

Explanation:

         00 C0       .WORD $C000    ; load address

.C:c000  20 9B B7    JSR $B79B      ; read N into X
.C:c003  A9 06       LDA #$06       ; number of "logical" lines plus 1 for hash
.C:c005  85 FC       STA $FC        ; store in counter variable for lines
.C:c007  86 FE       STX $FE        ; store N in counter variable for char repetitions
.C:c009  A6 FE       LDX $FE        ; load repetition counter
.C:c00b  86 FD       STX $FD        ; store in counter variable for line repetitions
.C:c00d  A9 03       LDA #$03       ; value to toggle the character toggle
.C:c00f  4D 1F C0    EOR $C01F      ; xor character bit toggle
.C:c012  8D 1F C0    STA $C01F      ; store character bit toggle
.C:c015  C6 FC       DEC $FC        ; decrement "logical" lines
.C:c017  D0 01       BNE $C01A      ; not 0 -> continue
.C:c019  60          RTS            ; program done
.C:c01a  A9 23       LDA #$23       ; load hash character
.C:c01c  A0 05       LDY #$05       ; load "logical" columns for hash
.C:c01e  49 00       EOR #$00       ; in each odd "logical" line, toggle character
.C:c020  20 D2 FF    JSR $FFD2      ; output one character
.C:c023  CA          DEX            ; decrement character repetition
.C:c024  D0 FA       BNE $C020      ; not 0 -> back to output
.C:c026  A6 FE       LDX $FE        ; reload character repetition
.C:c028  88          DEY            ; decrement "logical" columns
.C:c029  D0 F3       BNE $C01E      ; not 0 -> back to character toggle
.C:c02b  A9 0D       LDA #$0D       ; line done, load newline character
.C:c02d  20 D2 FF    JSR $FFD2      ; and output
.C:c030  C6 FD       DEC $FD        ; decrement line repetitions
.C:c032  D0 E6       BNE $C01A      ; not 0 -> back to character init
.C:c034  F0 D3       BEQ $C009      ; else back to main loop (toggle char toggling)

Screenshot

\$\endgroup\$
  • 5
    \$\begingroup\$ +1 for nostalgia (6502 assembly on a c64 was my first programming experience...) \$\endgroup\$ – Olivier Dulac Aug 18 '17 at 14:01
10
\$\begingroup\$

Haskell, 43 bytes

l%n=l!!1:l++l<*[1..n]
f n=["##"%n,"# "%n]%n

Try it online!

Outputs a list of strings.

\$\endgroup\$
8
\$\begingroup\$

Python 2, 55 bytes

def f(n):p=[(" "*n+"#"*n)*2]*n;print(p+["#"*n*5]*n)*2+p

Try it online!

This returns a 2D list of characters.

Python 2, 65 bytes

def f(n):p=((" "*n+"#"*n)*2+"\n")*n;print(p+("#"*n*5+"\n")*n)*2+p

Try it online!

Python 2, 66 bytes

def f(n):p=[(" "*n+"#"*n)*2]*n;print'\n'.join((p+["#"*n*5]*n)*2+p)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Wat witchkraft is yer footer \$\endgroup\$ – Leaky Nun Aug 17 '17 at 20:25
  • \$\begingroup\$ @LeakyNun A for loop :) \$\endgroup\$ – Mr. Xcoder Aug 17 '17 at 20:25
  • \$\begingroup\$ No, I'm talking about the f(i); storing the result in a temp and print accessing it. \$\endgroup\$ – Leaky Nun Aug 17 '17 at 20:26
  • 1
    \$\begingroup\$ @LeakyNun Ya misunderstood: f(i) prints and print in Python 2 adds a newline :P \$\endgroup\$ – Mr. Xcoder Aug 17 '17 at 20:27
  • \$\begingroup\$ Oh, how stupid of me. \$\endgroup\$ – Leaky Nun Aug 17 '17 at 20:30
6
\$\begingroup\$

Charcoal, 21 bytes

NθUOײθ#UOθ F²⟲OO²⁴⁶θ

Try it online! Link is to verbose version of code. I'd originally tried a cute bitmap approach:

F⁵F⁵F&|ικ¹«J×ιIθ×κIθUOθ#

Try it online! Link is to verbose version of code. Explanation: Works by considering the # as an array of 5×5 squares. The squares that are in odd rows or columns need to be filled in.

\$\endgroup\$
  • \$\begingroup\$ does charcoal really not have a hashtag shape built-in? \$\endgroup\$ – dzaima Aug 17 '17 at 20:58
  • \$\begingroup\$ Did I tie charcoal O_O? \$\endgroup\$ – Magic Octopus Urn Aug 17 '17 at 21:06
  • \$\begingroup\$ yay (hmm looks like I need to fix that a bit) \$\endgroup\$ – ASCII-only Aug 17 '17 at 21:23
  • \$\begingroup\$ @ASCII-only What needs fixing? \$\endgroup\$ – Neil Aug 17 '17 at 21:32
  • \$\begingroup\$ Oblong shouldn't be printing the steps for the polygon it uses internally lol \$\endgroup\$ – ASCII-only Aug 17 '17 at 21:33
6
\$\begingroup\$

J, 22 bytes

#('# '{~#:5$21,0)#~"1]

Try it online!

A lot of similarity to the other J answer, though I don't understand trains with lots of nouns well, so my answer has three potential bytes to cut off (two parens and a reflexive-~).

Explanation

Generating the octothorpe

The octothorpe is made by everything in the parenthetical, reproduced below for convenience.

'# '{~#:5$21,0

A lot of the way I make the octothorpe is abuse of the way that J pads its arrays when they aren't long enough.

21,0 simply creates the array 21 0.

5$ reshapes that array into a 5-atom array: 21 0 21 0 21.

#: converts each atom into a binary number. Since #: operates on each atom, the output is a matrix. Each 21 is replaced by 1 0 1 0 1 as expected, but each 0 is replaced by 0 0 0 0 0! This is because J pads arrays not long enough to match the shape of the resulting 2D array which is forced to be 5 5 because of the 1 0 1 0 1 rows. Fortunately, for numbers it pads with 0, so we get the resulting matrix

1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1

'# '{~ converts each 1 to a space and 0 to #. { means "take" and ~ means "switch the dyadic arguments, so J looks to each element in the matrix as indices for the string '# ' meaning each 0 becomes the zeroth element, # and each 1 becomes the first element, a space. This yields the size one octothorpe.

Resizing the octothorpe

This is simply a matter of copying n times along each axis, done using

the first # (which is part of a hook) and #~"1]. # copies along the horizontal axis and #"1 copies along the vertical axis.

\$\endgroup\$
  • 1
    \$\begingroup\$ ##"1&('# '{~#:5$21,0) saves a byte. \$\endgroup\$ – Zgarb Aug 18 '17 at 19:01
6
\$\begingroup\$

CJam, 27 26 25 bytes

{_[{S3*'#*'#5*}3*;]fe*e*}

Try it online!

Fun fact: This originally started at 29 bytes, and bytes have been removed one-by-one ever since, alternating between block and full-program mode.

Explanation:

{                          e# Stack:               | 2
 _                         e# Duplicate:           | 2 2
  [                        e# Begin array:         | 2 2 [
   {                       e# Do the following 3 times:
    S                      e#   Push a space       | 2 2 [" "
     3*                    e#   Repeat it 3 times: | 2 2 ["   "
       '#*                 e#   Join with '#':     | 2 2 [" # # "
          '#               e#   Push '#':          | 2 2 [" # # " '#
            5*             e#   Repeat it 5 times: | 2 2 [" # # " "#####"
              }3*          e# End:                 | 2 2 [" # # " "#####" " # # " "#####" " # # " "#####"
                 ;         e# Delete top of stack: | 2 2 [" # # " "#####" " # # " "#####" " # # "
                  ]        e# End array:           | 2 2 [" # # " "#####" " # # " "#####" " # # "]
                   fe*     e# Repeat characters:   | 2 ["  ##  ##  " "##########" "  ##  ##  " "##########" "  ##  ##  "]
                      e*   e# Repeat strings:      | ["  ##  ##  " "  ##  ##  " "##########" "##########" "  ##  ##  " "  ##  ##  " "##########" "##########" "  ##  ##  " "  ##  ##  "]
                        }  e# End
e# Result:
e# ["  ##  ##  "
e#  "  ##  ##  "
e#  "##########"
e#  "##########"
e#  "  ##  ##  "
e#  "  ##  ##  "
e#  "##########"
e#  "##########"
e#  "  ##  ##  "
e#  "  ##  ##  "]
\$\endgroup\$
  • \$\begingroup\$ Someone was prepared for this challenge :P \$\endgroup\$ – ETHproductions Aug 17 '17 at 20:10
  • \$\begingroup\$ @ETHproductions It was a CMC and moved to main... \$\endgroup\$ – Esolanging Fruit Aug 17 '17 at 20:10
  • \$\begingroup\$ @ETHproductions Can't really blame him for that... \$\endgroup\$ – Leaky Nun Aug 17 '17 at 20:12
6
\$\begingroup\$

Husk, 12 10 bytes

´Ṫ▲Ṙ" # # 

Try it online! Note the trailing space.

Explanation

´Ṫ▲Ṙ" # #   Implicit input, e.g. n=2.
   Ṙ" # #   Repeat each character of the string n times: "  ##  ##  "
´Ṫ          Outer product with itself by
  ▲         maximum: ["  ##  ##  ","  ##  ##  ","##########","##########","  ##  ##  ","  ##  ##  ","##########","##########","  ##  ##  ","  ##  ##  "]
            Print implicitly, separated by newlines.
\$\endgroup\$
6
\$\begingroup\$

J, 23 19 bytes

' #'{~1=]+./~@#i:@2

Saved 4 bytes thanks to @LeakyNun.

Try it online!

Explanation

' #'{~1=]+./~@#i:@2  Input: integer n
                  2  The constant 2
               i:@   Range [-2, -1, 0, 1, 2]
        ]            Get n
              #      Copy each n times
         +./~@       GCD table
      1=             Equals 1, forms the hashtag for input 1
' #'{~               Index and select the char
\$\endgroup\$
  • \$\begingroup\$ Rats! Was just about to post a (4 byte longer) solution of my own. I'm really impressed by how you're able to compose these functions without caps and with few conjunctions. \$\endgroup\$ – cole Aug 17 '17 at 20:57
  • \$\begingroup\$ @cole Thanks. Sometimes caps can be avoided by using a noun and dyad. For example, [:|:f could be 0|:f \$\endgroup\$ – miles Aug 17 '17 at 21:14
  • \$\begingroup\$ ' # '{~]#"1]#+./~@i:@2 saves a byte \$\endgroup\$ – Conor O'Brien Aug 18 '17 at 0:45
  • \$\begingroup\$ repeat before multiplication gives you 19 bytes: f=:' #'{~1=]+./~@#i:@2 \$\endgroup\$ – Leaky Nun Aug 18 '17 at 3:57
  • 1
    \$\begingroup\$ @hoosierEE It's a new feature coming in J 8.06. You can try the beta jsoftware.com/download/j806/install \$\endgroup\$ – miles Aug 18 '17 at 22:55
5
\$\begingroup\$

Jelly, 14 13 11 bytes

Saved 2 bytes thanks to @JonathanAllen

5ẋ€Ẏ&þ`ị⁾ #

A monadic link returning a list of lines. Note the trailing space.

Try it online!

How it works

5ẋ€Ẏ&þ`ị⁾ #    Main link. Arguments: n (integer)            1
5              Yield 5.
 ẋ€            Create a range and repeat each item n times. [[1], [2], [3], [4], [5]]
   Ẏ           Tighten; dump all sublists into the main list.
                                                            [1, 2, 3, 4, 5]
     þ         Create a table of                            [[1, 0, 1, 0, 1],
    &          bitwise ANDs,                                 [0, 2, 2, 0, 0],
      `        reusing this list.                            [1, 2, 3, 0, 1],
                                                             [0, 0, 0, 4, 4],
                                                             [1, 0, 1, 4, 5]]
       ị⁾ #    Index into the string " #".                   [" # # ",
               0 -> "#", 1 -> " ", 2 -> "#", etc.             "#####",
                                                              " # # ",
                                                              "#####",
                                                              " # # "]
\$\endgroup\$
  • \$\begingroup\$ Nice observation regarding bitwise or - save two bytes by switching from or to and - removing the need to lower, allowing an implicit range and removing the need for µ (or the you could have had there instead)... 5ẋ€Ẏ&þ`ị⁾ # \$\endgroup\$ – Jonathan Allan Aug 17 '17 at 23:04
  • \$\begingroup\$ @JonathanAllan Interesting--why does 5Ḷẋ€ require the µ, but not 5ẋ€? \$\endgroup\$ – ETHproductions Aug 17 '17 at 23:26
  • \$\begingroup\$ I thought the need was just to stop acting on n and then passing it to the right of ẋ€, since with a nilad-dyad leading chain being called monadically it's not necessary. I'm not quite sure, however, how ` seems to then place 5 (or maybe the list of that length) on the right of the tabled & though. \$\endgroup\$ – Jonathan Allan Aug 18 '17 at 1:04
4
\$\begingroup\$

Game Maker Language, 138 108 bytes

n=argument0 s=''for(j=0;j<5*n;j+=1){for(l=0;l<5*n;l+=1)if(j div n|l div n)&1s+='#'else s+=' 's+='
'}return s

Intended as a script (Game Maker's name for user-defined functions), thus the n=argument0 and return s. 20 bytes could be shaved by taking n directly from the current instance and using s as the result. (The instance gets these variables anyway because they weren't declared with var).

Beware of course that # is used by Game Maker's graphics stuff as an alternative newline character, so you might want to prefix it with \ if you want to output to the screen ;)

Also note that I'm using Game Maker 8.0's version of GML here; modern GML versions might have features that could save additional bytes.

Some ideas courtesy of friends wareya and chordbug.

\$\endgroup\$
  • \$\begingroup\$ I think this is the first GML answer i've ever seen \$\endgroup\$ – Timothy Groote Aug 18 '17 at 14:32
  • \$\begingroup\$ @TimothyGroote It's a shame it isn't used more, its optional brackets and semicolons are great for golfing :) \$\endgroup\$ – Andrea Aug 18 '17 at 14:33
4
\$\begingroup\$

Perl 5, 49 + 1 (-p) = 50 bytes

$_=' # # 
'=~s/./$&x$_/gre x$_;$_.=(y/ /#/r.$_)x2

Try it online!

How?

Implicitly store the input in $_ via the -p flag. Start with the most basic possible top line " # # " with its trailing newline. Replicate each of those characters by the input number. Then replicate that by the input number to form the top part of the octothorpe, storing all of that back in $. Then append the line with all characters replaced by '#' times the input number. Then append the top section. Do those last two sentences a total of two times. Output of the $ is implicit in the -p flag.

\$\endgroup\$
  • \$\begingroup\$ I like how your answer is just as readable as mine. \$\endgroup\$ – AdmBorkBork Aug 17 '17 at 20:31
  • \$\begingroup\$ They've always said that Perl is a write-only language. \$\endgroup\$ – Xcali Aug 17 '17 at 20:41
3
\$\begingroup\$

05AB1E, 25 22 21 bytes

•LQ•bûε×}5ôεS„# èJ¹F=

Try it online!


-1 because Emigna hates transliterate and, thankfully, reminds me I should too :P.


Gotta be a better way than bitmapping it... Still working.

\$\endgroup\$
  • \$\begingroup\$ Reflection... is not the answer in 05AB1E, though it seems like it could be... \$\endgroup\$ – Magic Octopus Urn Aug 17 '17 at 20:53
  • \$\begingroup\$ 5ôεS„# èJ¹F= saves a byte. \$\endgroup\$ – Emigna Aug 17 '17 at 20:56
  • \$\begingroup\$ @Emigna would canvas be good for this? \$\endgroup\$ – Magic Octopus Urn Aug 17 '17 at 20:59
  • \$\begingroup\$ Possibly. I haven't tried the canvas yet so I'm not really sure of its capabilities. Seems like something it's made for. \$\endgroup\$ – Emigna Aug 17 '17 at 21:00
3
\$\begingroup\$

JavaScript (ES6), 79 bytes

f=
n=>[...Array(n*5)].map((_,i,a)=>a.map((_,j)=>` #`[(i/n|j/n)&1]).join``).join`
`
<input type=number oninput=o.textContent=f(this.value)><pre id=o>

Port of the bitmap approach that I'd used for my original Charcoal attempt.

\$\endgroup\$
3
\$\begingroup\$

Python 2, 124, 116, 113, 112, 98, 96 66 bytes

New (Credit: HyperNeutrino):

def f(a):i='print(" "*a+"#"*a)*2;'*a;exec(i+'print"#"*a*5;'*a)*2+i

Old:

a=input();b,c="# "
for i in"012":
	exec'print c*a+b*a+c*a+b*a;'*a
	if i<"2":exec'print b*a*5;'*a

Try it online!

Obviously not the shortest solution, but I think it's decent. Any feedback would be appreciated!

\$\endgroup\$
  • 1
    \$\begingroup\$ a,b,c=input()," #" should save some bytes. \$\endgroup\$ – DJMcMayhem Aug 17 '17 at 23:33
  • \$\begingroup\$ @DJMcMayhem That gave me an error. Did you mean a,b,c=input(),"#"," "? Which isn't any shorter...I appreciate the help! \$\endgroup\$ – Braeden Smith Aug 17 '17 at 23:41
  • \$\begingroup\$ Oh, sorry. I assumed that worked because a,b="# " works. \$\endgroup\$ – DJMcMayhem Aug 17 '17 at 23:44
  • \$\begingroup\$ a=input();b,c="# " will work and save bytes \$\endgroup\$ – Wheat Wizard Aug 17 '17 at 23:44
  • \$\begingroup\$ You can also get get rid of the parens in (i==2) and add a space to the beginning. \$\endgroup\$ – Wheat Wizard Aug 17 '17 at 23:47
3
\$\begingroup\$

Brain-Flak, 338 332 bytes

6 bytes thanks to Riley.

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

Try it online!

More "readable" version

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ (({})<>)(())<>({}<>) at the beginning can be replaced with (({}<>)<(())>) \$\endgroup\$ – Riley Aug 18 '17 at 13:46
2
\$\begingroup\$

SOGL (SOGLOnline commit 2940dbe), 15 bytes

ø─Ζ┘Χ⁴‘5n{.∙.*T

To run this, download this and run the code in the index.html file.

Uses that at that commit (and before it) * repeated each character, not the whole string.

Explanation:

ø─Ζ┘Χ⁴‘          push " # # ##### # # ##### # # "
       5n        split into lines of length 5
         {       for each line do
          .∙       multiply vertically input times
            .*     multiply horizontally input times
              T    output in a new line

Bonus: add 2 inputs for separate X and Y length!

\$\endgroup\$
  • \$\begingroup\$ "commit 2940dbe" - I like that idea. Can you explain why ø─Ζ┘Χ⁴‘ pushes that though? \$\endgroup\$ – Magic Octopus Urn Aug 17 '17 at 20:54
  • 1
    \$\begingroup\$ @MagicOctopusUrn That's SOGLs compression, which here stores a dictionary of " " and # and the base-2 data required for that string. \$\endgroup\$ – dzaima Aug 17 '17 at 21:12
  • \$\begingroup\$ Neat, is it stable enough for me to start using :)? \$\endgroup\$ – Magic Octopus Urn Aug 17 '17 at 21:40
  • 1
    \$\begingroup\$ @MagicOctopusUrn Well it's pretty stable as there have been no answer-breaking changes since SOGLOnline, but whether you can use it (as in understand it) is another question. You can try though and ask question in TNB \$\endgroup\$ – dzaima Aug 17 '17 at 21:44
  • \$\begingroup\$ Haha... Ill wait for documentation then. I do need coddled a little. \$\endgroup\$ – Magic Octopus Urn Aug 17 '17 at 21:45
2
\$\begingroup\$

brainfuck, 224 bytes

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

Try it online!

Making-of

I tried to build this code by hand and spent quite a few hours, so I decided to make a transpiler in Python.

Here is the code I entered to make this code:

read(0)
copy(0,(1,1),(5,1))
add(3,1)
add(4,5)
loop(4)
loop(1)
add(2,1)

add(7,1)
add(8,5)
loop(8)
loop(5)
add(6,1)

loop(3)
add(9,1)
loop(7)
add(10,1)
add(11,-3)
end(7)
end(3)
copy(9,(3,1))
copy(10,(7,1))
add(10,5)
copy(10,(11,7))
write(11)
clear(11)

end(5)
copy(6,(5,1))
copy(7,(6,-1))
add(6,1)
copy(6,(7,1))
end(8)
add(6,10)
write(6)
clear(6)

end(1)
copy(2,(1,1))
copy(3,(2,-1))
add(2,1)
copy(2,(3,1))
end(4)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 98 93 bytes

5 bytes thanks to Felix Palmen.

i,j;main(a){for(scanf("%d",&a);j<a*5||(j=!puts(""),++i<a*5);)putchar(i/a+1&j++/a+1&1?32:35);}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Gaia, 9 bytes

 # ”ṫ&:Ṁ‡

Pretty much a port of Zgarb's great answer

Try it online! (the footer is just to pretty print, the program itself returns a 2D list of characters)

Explanation

 # ”       Push the string " # "
    ṫ      Bounce, giving " # # "
     &     Repeat each character by input
      :    Copy
       Ṁ‡  Tabled maximum with itself
\$\endgroup\$
2
\$\begingroup\$

Befunge, 105 103 bytes

p9p&5*08p>08g-   v
>,g1+:00p^v0:/5g8_p9g1+:09p08g-#v_@
gvg90\/\g0< # # ##### 0:/5g8,+55<
^>\/2%5*+92++2

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Could you add a link to an online interpreter? tio.run/#befunge is a good one AFAIK. \$\endgroup\$ – DJMcMayhem Aug 18 '17 at 4:54
1
\$\begingroup\$

Python, 88 84 77 bytes

lambda x:[[(' #'[i//x%2]+'#')[j//x%2]for j in range(5*x)]for i in range(5*x)]

Try it online!

Returns 2D list of characters.

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

PowerShell, 72 68 63 60 bytes

param($a)(,($x=,((' '*$a+"#"*$a)*2)*$a)+,("#"*5*$a)*$a)*2;$x

Try it online!

Takes input $a. Then, we do a bunch of magic string and array manipulation.

(,($x=,((' '*$a+"#"*$a)*2)*$a)+,("#"*5*$a)*$a)*2;$x
         ' '*$a+"#"*$a                              # Construct a string of spaces and #
        (             )*2                           # Repeat it twice
      ,(                 )*$a                       # Repeat that $a times to get the top as an array
  ($x=                       )                      # Store that into $x and immediately output it
 ,                            +                     # Array concatenate that with ...
                               ,("#"*5*$a)          # another string, the middle bar ...
                                          *$a       # repeated $a times.
(                                            )*2;   # Do that twice
                                                 $x # Output $x again

You can peel off the parts of the explanation starting from the bottom to see how the output is constructed, so hopefully my explanation makes sense.

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

Haskell, 72 bytes

a#b=a++b++a++b++a
c%l=((c<$l)#('#'<$l))<$l
f n=(' '%[1..n])#('#'%[1..n])

Returns a list of strings. Try it online!

How it works:

a#b=a++b++a++b++a          -- concatenate the strings a and b in the given pattern
c%l=                       -- take a char c and a list l (we only use the length
                           -- of l, the actual content doesn't matter)
    c<$l                   -- make length l copies of c
         '#'<$l            -- make length l copies of '#'
        #                  -- combine them via function #
               <$l         -- and make length l copies of that string
f n=                       -- main function
              #            -- make the "a b a b a" pattern with the strings
                           -- returned by the calls to function %                                
    ' '%[1..n]             --   one time with a space 
               '#'%[1..n]  --   one time with a '#'
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 63 bytes

ArrayFlatten@Array[x=#;Table[If[OddQ@-##," ","#"],x,x]&,{5,5}]&

Explanation

ArrayFlatten@Array[x=#;Table[If[OddQ@-##," ","#"],x,x]&,{5,5}]&  (* input N *)

                   x=#                                           (* Set x to N *)
                                                      &          (* A function that takes two inputs: *)
                             If[OddQ@-##," ","#"]                (* if both inputs are odd (1), " ". "#" otherwise *)
                       Table[                    ,x,x]           (* Make N x N array of that string *)
             Array[                                     ,{5,5}]  (* Make a 5 x 5 array, applying that function to each index *)
ArrayFlatten@                                                    (* Flatten into 2D array *)

(1)-## parses into Times[-1, ##]

\$\endgroup\$
  • \$\begingroup\$ ArrayFlatten is very nice. \$\endgroup\$ – Mark S. Aug 20 '17 at 2:48
1
\$\begingroup\$

Python 2, 113 bytes

As an array of strings:

r=[1-1*(i%(2*n)<n)for i in range(5*n)]
print[''.join(' #'[r[k]+r[j]>0]for k in range(len(r)))for j in range(n*5)]

As ASCII art:

Python 3, 115 bytes

r=[1-1*(i%(2*n)<n)for i in range(5*n)]
for j in range(n*5):print(*(' #'[r[k]+r[j]>0]for k in range(len(r))),sep='')

Python 3, 117 bytes

p=range(5*n)
for i,e in enumerate([j%(2*n)>=n for j in p]for k in p):print(*[' #'[i%(2*n)>=n or k]for k in e],sep='')

As an array of booleans

Python 2, 75 bytes

p=range(5*n)
f=lambda o:o%(2*n)>=n
print[[f(j)or f(i)for j in p]for i in p]

\$\endgroup\$
  • 1
    \$\begingroup\$ Long time, no see :-) \$\endgroup\$ – ETHproductions Aug 17 '17 at 22:55
  • \$\begingroup\$ Yes, it has! @ETHproductions \$\endgroup\$ – Zach Gates Aug 17 '17 at 23:01
1
\$\begingroup\$

Java 8, 103 bytes

Lambda accepts Integer and prints the octothorpe to standard out. Cast to Consumer<Integer>.

n->{for(int s=5*n,x=0,y;x<s;x++)for(y=0;y<s;)System.out.print((x/n%2+y++/n%2>0?'#':32)+(y<s?"":"\n"));}

Try It Online

Ungolfed lambda

n -> {
    for (
        int
            s = 5 * n,
            x = 0,
            y
        ;
        x < s;
        x++
    )
        for (y = 0; y < s; )
            System.out.print(
                (x / n % 2 + y++ / n % 2 > 0 ? '#' : 32)
                + (y < s ? "" : "\n")
            );
}

The key observation here is that, on a 5 by 5 grid of n by n cells, octothorpes appear wherever the row or column number (0-based) is odd. I'm pretty sure this is the cheapest general approach, but it seems further golfable.

Acknowledgments

  • -1 byte thanks to Kevin Cruijssen
\$\endgroup\$
  • 1
    \$\begingroup\$ You can place the int s=5*n,x=0,y instead the for-loop to save a byte on the semicolon. \$\endgroup\$ – Kevin Cruijssen Aug 18 '17 at 6:40
1
\$\begingroup\$

Pyth, 28 22 bytes

-6 bytes thanks to @LeakyNun

JsC*]S5Qjmsm@"# "*kdJJ

Test Suite.

\$\endgroup\$
  • \$\begingroup\$ 22 bytes \$\endgroup\$ – Leaky Nun Aug 18 '17 at 13:59
  • \$\begingroup\$ @LeakyNun Thanks, I didn't really golf this answer so far. \$\endgroup\$ – Mr. Xcoder Aug 18 '17 at 14:00
1
\$\begingroup\$

R, 87 85 62 bytes

m=matrix(" ",x<-scan()*5,x);m[s,]=m[,s<-rep(!1:0,e=x/5)]="#";m

2 bytes saved by representing c(F,T) as !1:0, thanks to LeakyNun

23 bytes saved thanks to Giuseppe

Try it online!

Explanation (ungolfed):

x=scan()*5              # Multiply input by 5 to get the required width/height of the matrix
m=matrix(" ",x,x)       # Create a matrix of the required dimensions
s=rep(!1:0,each=x/5)    # The sequence s consists of F repeated n times, followed by T repeated n times
m[s,]="#"               # Use s as logical indices to set those rows as "#" characters.
                        # R recycles the sequence to the height of the matrix.
m[,s]="#"               # Same, with columns
write(m,"",x,,"")       # Print out across the required number of columns             
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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