21
\$\begingroup\$

Create a function which given a number of lines n, makes a bigA.

  • The horizontal bar of bigA must be at the middle row, or the lower of the two if n is even
  • Assume a monospace font for output

Output should be a string (or similar, eg character array) with clear linebreaks to break up the lines, and with correct whitespace for left-padding (you can assume \t to be 4 spaces). There can be any whitespace on the right.

Examples

n = 1

A

n = 2

 A
AAA

n = 3

 A
 AAA
A A

n = 4

 A
 A A
 AAAAA
A A

n = 5

 A
 A A
 AAAAA
 A A
A A

This is inspired by Create an "H" from smaller "H"s

asked Mar 23, 2018 at 1:00
\$\endgroup\$
9
  • \$\begingroup\$ May I add whitespace to the right side? Also, is trailing newline allowed? \$\endgroup\$ Commented Mar 23, 2018 at 1:12
  • \$\begingroup\$ @Bubbler, Any whitespace on the right side is fine, no trailing newline though \$\endgroup\$ Commented Mar 23, 2018 at 1:20
  • \$\begingroup\$ Are we allowed to return 2D character arrays instead of strings? (hint: it's usually recommended to allow any kind of output) \$\endgroup\$ Commented Mar 23, 2018 at 8:26
  • 1
    \$\begingroup\$ @OlivierGrégoire Sure, as long as there is a clear break for the lines (eg an "\n" element, nested arrays) \$\endgroup\$ Commented Mar 23, 2018 at 9:00
  • 1
    \$\begingroup\$ @TonHospel, No, that really defeats the purporse of this \$\endgroup\$ Commented Mar 23, 2018 at 18:18

20 Answers 20

12
\$\begingroup\$

05AB1E, 13 bytes

Code:

Ð;î1)'A1376SΛ

Uses the 05AB1E encoding. Try it online!

Explanation:

Ð # Triplicate the input.
 ;î # Compute ceiling(n / 2).
 1 # Push the first input again.
 ) # Wrap into an array. For input 7, this would result in:
 [7, 7, 4, 7].
 'A # Push the character 'A'
 1376S # Push the array [1, 3, 7, 6]. These are the directions of the canvas.
 This essentially translates to [↗, ↘, ↖, ←].
 Λ # Write to canvas using the previous three parameters.

Canvas

I should probably document the canvas a little bit more (and a lot of other functions), but this basically sums it up. The canvas has different 'modes' based on the parameter types given. The canvas command has three parameters: <length> <string> <direction>.

Since the length and direction parameters are lists, it 'zips' these lists to create a set of instructions to be executed. The string parameter is just the letter A, so this is the fill character used by all instructions. The canvas interprets this as the following set of instructions (for input 7):

  • Draw a line of length 7 with the string A in direction
  • Draw a line of length 7 with the string A in direction
  • Draw a line of length 4 with the string A in direction
  • Draw a line of length 7 with the string A in direction

The directions are translated in the following manner:

7 0 1
 ↖ ↑ ↗
6 ← X → 2
 ↙ ↓ ↘
5 4 3

If nothing has been outputted, 05AB1E automatically outputs the canvas result.

answered Mar 23, 2018 at 7:30
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Thank you very much for the canvas explanation, that's a brilliant feature :-) \$\endgroup\$ Commented Mar 23, 2018 at 9:33
  • \$\begingroup\$ TIL triplicate is a word \$\endgroup\$ Commented Mar 23, 2018 at 19:38
  • \$\begingroup\$ @thecoder16 quadruplicate, quintuplicate, ..., decuplicate \$\endgroup\$ Commented Mar 26, 2018 at 13:56
  • \$\begingroup\$ Wow. I was doubtful of nonuplicate, but it exists as all the others do. Of course we have such useless words in English xD \$\endgroup\$ Commented Mar 26, 2018 at 15:37
  • 1
    \$\begingroup\$ @KevinCruijssen Hey, apologies for all the late replies, it's been incredibly busy for me the last couple of weeks (I only managed to get 8 hours of sleep the last 72 hours haha) so I don't think I'm able to do anything right now, but feel free to add it to the tip page if you'd like. \$\endgroup\$ Commented Nov 8, 2018 at 1:25
6
\$\begingroup\$

Charcoal, (削除) 17 (削除ここまで) 15 bytes

×ばつθA

Try it online! Link is to verbose version of code. Explanation:

Input n.

×ばつθA

Print the horizontal bar of the big A. (For even numbers, the n+1th overlaps the right side anyway.)

M⊘θ↗

Move to the top of the big A.

×ばつθA

Print both sides of the big A.

answered Mar 23, 2018 at 1:15
\$\endgroup\$
4
\$\begingroup\$

Python 2, 80 bytes

lambda n:'\n'.join(' '*(n+~i)+('A'+' A'[i==n/2]*n*2)[:i*2]+'A'for i in range(n))

Try it online!

Divide the desired output into the left whitespace, left A plus middle whitespace or As, and the right A. Compute the middle part using slicing on a fixed string. This allows to use the same way to generate the first line.

answered Mar 23, 2018 at 1:58
\$\endgroup\$
4
\$\begingroup\$

Stax, 15 bytes

┴3╬*ôP^x'┌_╓J2♫

Run and debug it

Unpacked, ungolfed, and commented, the program looks like this.

m map over [1 .. input] using rest of the program, output each result
'A "A" literal
xhi= is the iteration index equal to (integer) half the input?
65* multiply by 65 (character code of "A")
]i* repeat that character (" " or "A") i times
+ concat to initial "A"
x) left pad to the original input
|p palindromize (concatenate the reverse minus the last character)

Run this one

answered Mar 23, 2018 at 3:29
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 77 bytes

This source code has a rectangle shape! Oh wait ... wrong challenge :-/

f=(n,k=n>>1,p='A')=>--n?f(n,k,' '+p)+`
${p}${(k-n?' ':'A').repeat(n*2-1)}A`:p

Try it online!

answered Mar 23, 2018 at 14:41
\$\endgroup\$
4
\$\begingroup\$

Python 3.6, 79 bytes or 73 bytes

Using f-strings to align horizontal parts of the letter:

lambda n:'\n'.join(f"{'A'+' A'[i==n//2]*2*i:>{n+i}}"[:-1]+'A'for i in range(n))

With \b used to delete one A (possibly cheating):

lambda n:'\n'.join(f"{'A'+' A'[i==n//2]*2*i:>{n+i}}\bA"for i in range(n))
answered Mar 23, 2018 at 22:10
\$\endgroup\$
3
\$\begingroup\$

J, 65 bytes

f=:3 :''' A''{~1(([:(<@;]+i.@+:)<.@-:)y)}([:(}:@|."1,.])=/~@i.)y'

Try it online!

It can be reduced by approx. 12 bytes by simply making the verb tacit, but I have problems doing it.

Explanation:

3 : '...' denotes an explicit one-liner verb

y is the argument

=/~@i. creates an identity matrix with size the argument

 =/~@i. 4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

([:(}:@|."1,.]) prepends the identity matrix with its mirror copy with last elements of each row dropped.

 ]a =. ([:(}:@|."1,.])=/~@i.) 4
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
1 0 0 0 0 0 1

1(...)}(...) changes to 1 the positions in its right argument, selected by the left one

([:(<@;]+i.@+:)<.@-:) - prepares the selection by doing the following:

 <.@-: - halves the argument and finds the floor (finds the row number)
 <@; - box the row, followed by a list of columns: 
 ]+i.@+: - a list form the argumnt to the doubled row number
 ([:(<@;]+i.@+:)<.@-:) 4
┌───────────┐
│┌─┬───────┐│
││2│2 3 4 5││
│└─┴───────┘│
└───────────┘
 1(([:(<@;]+i.@+:)<.@-:) 4)}a
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 1 1 1 1 0
1 0 0 0 0 0 1

' A'{~ renders a space in the places of 0 and 'A' where there is 1

 ' A'{~1(([:(<@;]+i.@+:)<.@-:) 4)}a
 A 
 A A 
 AAAAA 
A A
answered Mar 23, 2018 at 10:31
\$\endgroup\$
3
\$\begingroup\$

Canvas, (削除) 17 (削除ここまで) 13 bytes

×ばつ╶»╵:╋│

Try it here!

-4 bytes thanks to dzaima!

answered Mar 23, 2018 at 16:42
\$\endgroup\$
0
3
\$\begingroup\$

Java (JDK), 121 bytes

n->{var a=new char[n][n+n-1];for(int c=n*n,r,q;c-->0;a[r=c%n][q=c/n]=a[r][n+n-q-2]+=r==n/2&q>=r|r==n+~q?65:32);return a;}

Try it online!

Credits

answered Mar 23, 2018 at 8:51
\$\endgroup\$
0
2
\$\begingroup\$

Ruby, 66 bytes

->n{(0...n).map{|i|(s=(i==n/2??A:?\s)*2*i+?A)[0]=?A;?\s*(n+~i)+s}}

Try it online!

Returns as array of lines

answered Mar 23, 2018 at 12:38
\$\endgroup\$
2
\$\begingroup\$

SOGL V0.12, 12 bytes

 A*:╚╥≤.»I:ž

Try it Here!

Explanation:

 A* repeat "A" input times
 : duplicate it
 ╚ create a "/" diagonal of one of the copies of As
 ╥ palindromize it horizontally
 ≤ get the other copy of the "A"s on top
 .»I: push floor(input/2)+1 twice
 ž and at those coordinates in the palindromized diagonals place in the row of As
answered Mar 23, 2018 at 7:24
\$\endgroup\$
2
\$\begingroup\$

Japt -R, (削除) 20 (削除ここまで) 19 bytes

Çç" A"gZ¶Uz1i'A êÃû

Try it


Explanation

 :Implicit input of integer U
Ç :Create the range [0,U) and pass each Z through a function
 Uz : Floor divide U by 2
 Z¶ : Test for equality with Z (true=1, false=0)
 " A"g : Get the character in the string " A" at that index
 ç : Repeat Z times
 1 : (Closes a few nested methods)
 i'A : Prepend an "A"
 ê : Palindromise
 Ã :End function
 û :Centre pad each element to the length of the longest element
 :Implicitly join with newlines and output

Alternative

(In the hope that it might help me spot some savings!)

Æ'AúXÄ" A"gX¶Uz1êÃû
answered Mar 23, 2018 at 7:17
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Another alternate that's a byte longer: ç h'AUz)¬íp ®i'A êÃû \$\endgroup\$ Commented Mar 23, 2018 at 15:19
  • \$\begingroup\$ @ETHproductions Replace p with ² and it's also 19 bytes. \$\endgroup\$ Commented Mar 23, 2018 at 17:51
  • \$\begingroup\$ +1 way better than my monstrosity. \$\endgroup\$ Commented Mar 25, 2018 at 5:55
1
\$\begingroup\$

Javascript, 124 bytes

A fairly naive solution, gave it a shot to practice js skills.

for(i=-1,p=" ".repeat(n-1)+"A ";++i<n;console.log(i-~~(n/2)?p:p.slice(0,i)+"A".repeat(n)),p=p.slice(1,n)+" "+p.slice(n-1)){}

Unpacked

for(
 //create the first line
 i=-1, p=" ".repeat(n-1)+"A "; 
 ++i<n;
 console.log( 
 //if we are not at the bar
 i-~~(n/2)?
 //otherwise, use the modified previous line
 p
 //slice the start of the previous line and add As
 :p.slice(0,i)+"A".repeat(n)), 
 //add a space in between the previous line and remove padding on each side
 p=p.slice(1,n)+" "+p.slice(n-1)){}
answered Mar 23, 2018 at 7:09
\$\endgroup\$
1
\$\begingroup\$

Ruby, 73 bytes

->n{(0...n).map{|i|(?A.ljust(i*2,i==n/2??A:' ')+(i>0??A:'')).center n*2}}

Try it online!

answered Mar 23, 2018 at 12:20
\$\endgroup\$
1
\$\begingroup\$

Jelly, (削除) 23 (削除ここまで) (削除) 20 (削除ここまで) (削除) 19 (削除ここまで) 18 bytes

=þ`o\L‘HĊƲ¦UŒBị)A 

Try it online!

=þ` creates an identity matrix of size n.

L‘HĊƲ finds the row index of the horizontal bar with ¦ picking that row out and applying o\ to it which creates the bar.

U reverses each row so we don't have an upside down "A" and ŒB (palindromize; vectorizes) makes the second half of the "A".

ị)A (with a space that is getting trimmed in the formatting) replaces 0s with spaces and 1s with As.

answered Mar 23, 2018 at 1:48
\$\endgroup\$
1
\$\begingroup\$

T-SQL, (削除) 182 (削除ここまで) 177 bytes

DECLARE @n INT=5DECLARE @ INT=0a:DECLARE @s VARCHAR(9)=STR(POWER(10,@),@n)PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@=@n/2,'A',' '))SET @+=1IF @<@n GOTO a

First version (with 182 bytes):

DECLARE @n INT=5DECLARE @ INT=0WHILE @<@n BEGIN DECLARE @s VARCHAR(9)=STR(POWER(10,@),@n)PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@=@n/2,'A',' '))SET @+=1 END

The version above works up to @n=9.

Here is another version, which works up to @n=23, but has 2 extra bytes:

DECLARE @n INT=5DECLARE @ INT=0WHILE @<@n BEGIN DECLARE @s VARCHAR(23)=STR(POWER(10.,@),@n)PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@=@n/2,'A',' '))SET @+=1 END

Ungolfed:

DECLARE @n INT=5
DECLARE @i INT=0
WHILE @i<@n BEGIN
 DECLARE @s VARCHAR(9)=STR(POWER(10,@i),@n)
 PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@i=@n/2,'A',' '))
 SET @i+=1
END
answered Mar 23, 2018 at 17:08
\$\endgroup\$
1
\$\begingroup\$

Haskell, (削除) 98 (削除ここまで) (削除) 97 (削除ここまで) 95 bytes and 109 bytes

Two very different approaches. First (95 bytes):

c!n=([1..n]>>c)++"A"
f n=unlines[" "!(n-x)++drop 3([" "!(abs$n`div`2-x+1)!!0]!(2*x))|x<-[1..n]]

and second (109 bytes):

m True='A'
m _=' '
g n=unlines[[m(abs(n-j)==l||l==q&&elem j[q+1..q+n])|j<-[1..2*n]]|l<-[0..n-1],q<-[n`div`2]]

(削除) Try them here! (削除ここまで); (削除) Try modified version here! (削除ここまで)

Try third version here!

answered Mar 24, 2018 at 13:28
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to PPCG! You can save a byte on your first approach by defining l as infix operator. \$\endgroup\$ Commented Mar 25, 2018 at 1:44
  • \$\begingroup\$ m True='A' an be shortened to m b|b='A'. \$\endgroup\$ Commented Mar 25, 2018 at 1:47
  • \$\begingroup\$ It turned out that even two bytes could be saved. Thanks! :) \$\endgroup\$ Commented Mar 25, 2018 at 13:16
1
\$\begingroup\$

Python 2, 70 bytes or 65 bytes

List of strings is acceptable result, as @Budd stated in comments.

lambda n:['%*sA\n'%(n+i,('A'+i*2*' A'[i==n/2])[:-1])for i in range(n)]

Try it online!


Seemingly cheaty solution, using \b. It looks funky in TIO, in console it does the job.

lambda n:['%*s\bA\n'%(n+i,'A'+i*2*' A'[i==n/2])for i in range(n)]

Try it online!

answered Mar 26, 2018 at 11:24
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -n, 57 bytes

#!/usr/bin/perl -n
say$"x-$_.uc(A x($n+$_&-2?1:$n*2)|$"x2x$n++.A)for 1-$_..0

Try it online!

answered Mar 24, 2018 at 0:00
\$\endgroup\$
0
\$\begingroup\$

Python 3, (削除) 93 (削除ここまで), 88 bytes

lambda n:'\n'.join(f"{'A'+(x>0)*('A '[x!=n//2]*(x*2-1)+'A'):^{n*2-1}}"for x in range(n))

Try it online!

-3 by @ovs using f-string

answered Mar 23, 2018 at 10:21
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.