Create a function which given a number of lines n, makes a bigA.
- The horizontal bar of
bigAmust be at the middle row, or the lower of the two ifnis 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
-
\$\begingroup\$ May I add whitespace to the right side? Also, is trailing newline allowed? \$\endgroup\$Bubbler– Bubbler2018年03月23日 01:12:54 +00:00Commented Mar 23, 2018 at 1:12
-
\$\begingroup\$ @Bubbler, Any whitespace on the right side is fine, no trailing newline though \$\endgroup\$Budd– Budd2018年03月23日 01:20:38 +00:00Commented 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\$Olivier Grégoire– Olivier Grégoire2018年03月23日 08:26:50 +00:00Commented 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\$Budd– Budd2018年03月23日 09:00:24 +00:00Commented Mar 23, 2018 at 9:00
-
1\$\begingroup\$ @TonHospel, No, that really defeats the purporse of this \$\endgroup\$Budd– Budd2018年03月23日 18:18:16 +00:00Commented Mar 23, 2018 at 18:18
20 Answers 20
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.
-
1\$\begingroup\$ Thank you very much for the canvas explanation, that's a brilliant feature :-) \$\endgroup\$user2956892– user29568922018年03月23日 09:33:50 +00:00Commented Mar 23, 2018 at 9:33
-
\$\begingroup\$ TIL triplicate is a word \$\endgroup\$Quintec– Quintec2018年03月23日 19:38:09 +00:00Commented Mar 23, 2018 at 19:38
-
\$\begingroup\$ @thecoder16 quadruplicate, quintuplicate, ..., decuplicate \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年03月26日 13:56:47 +00:00Commented 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\$Quintec– Quintec2018年03月26日 15:37:45 +00:00Commented 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\$Adnan– Adnan2018年11月08日 01:25:41 +00:00Commented Nov 8, 2018 at 1:25
Charcoal, (削除) 17 (削除ここまで) 15 bytes
×ばつθA
Try it online! Link is to verbose version of code. Explanation:
Nθ
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.
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))
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.
Stax, 15 bytes
┴3╬*ôP^x'┌_╓J2♫
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)
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
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))
J, 65 bytes
f=:3 :''' A''{~1(([:(<@;]+i.@+:)<.@-:)y)}([:(}:@|."1,.])=/~@i.)y'
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
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;}
Credits
- -3 bytes thanks to ceilingcat
SOGL V0.12, 12 bytes
A*:╚╥≤.»I:ž
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
Japt -R, (削除) 20 (削除ここまで) 19 bytes
Çç" A"gZ¶Uz1i'A êÃû
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êÃû
-
1\$\begingroup\$ Another alternate that's a byte longer:
ç h'AUz)¬íp ®i'A êÃû\$\endgroup\$ETHproductions– ETHproductions2018年03月23日 15:19:59 +00:00Commented Mar 23, 2018 at 15:19 -
\$\begingroup\$ @ETHproductions Replace
pwith²and it's also 19 bytes. \$\endgroup\$Shaggy– Shaggy2018年03月23日 17:51:04 +00:00Commented Mar 23, 2018 at 17:51 -
\$\begingroup\$ +1 way better than my monstrosity. \$\endgroup\$Oliver– Oliver2018年03月25日 05:55:02 +00:00Commented Mar 25, 2018 at 5:55
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)){}
Ruby, 73 bytes
->n{(0...n).map{|i|(?A.ljust(i*2,i==n/2??A:' ')+(i>0??A:'')).center n*2}}
Jelly, (削除) 23 (削除ここまで) (削除) 20 (削除ここまで) (削除) 19 (削除ここまで) 18 bytes
=þ`o\L‘HĊƲ¦UŒBị)A
=þ` 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.
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
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! (削除ここまで)
-
\$\begingroup\$ Welcome to PPCG! You can save a byte on your first approach by defining
las infix operator. \$\endgroup\$Laikoni– Laikoni2018年03月25日 01:44:03 +00:00Commented Mar 25, 2018 at 1:44 -
\$\begingroup\$
m True='A'an be shortened tom b|b='A'. \$\endgroup\$Laikoni– Laikoni2018年03月25日 01:47:31 +00:00Commented Mar 25, 2018 at 1:47 -
\$\begingroup\$ It turned out that even two bytes could be saved. Thanks! :) \$\endgroup\$Radek– Radek2018年03月25日 13:16:10 +00:00Commented Mar 25, 2018 at 13:16
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)]
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)]
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
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))
-3 by @ovs using f-string