21
\$\begingroup\$

Given an integer n>0, write a program that will output an "isosceles" Triangle out of #'s of height n.

Examples:

n=3

  #  
 ### 
#####

n=1

#

n=5

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

This would be invalid:

#
###
#####
#######
#########
| improve this question | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Are leading and trailing newlines allowed? \$\endgroup\$ – S.S. Anne Feb 6 at 21:45
  • 15
    \$\begingroup\$ How is this not a duplicate? XD \$\endgroup\$ – RGS Feb 6 at 22:06
  • 1
    \$\begingroup\$ May we use another character besides #? \$\endgroup\$ – Shaggy Feb 7 at 0:38
  • 1
    \$\begingroup\$ Which isosceles triangle permitted? For the input 3 is the triangle shown the only allowed triangle? Could you be a little more specific with you challenge? \$\endgroup\$ – Ad Hoc Garf Hunter Feb 7 at 2:47
  • 2
    \$\begingroup\$ Related: Diamond creator + \$\endgroup\$ – Kevin Cruijssen Feb 7 at 9:06

35 Answers 35

1
2
0
\$\begingroup\$

Retina 0.8.2, 21 bytes

.+
$*#
#
¶$.'$* $`#$`

Try it online! Link includes test cases. Output includes both leading and trailing newlines (but that helpfully separates the test cases!) Explanation:

.+
$*#

Convert the input to that many #s.

#
¶$.'$* $`#$`

For each #, output a new line, then as many spaces as there are #s remaining, then the #s so far, mirrored.

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

JavaScript V8, 48 bytes

f=(i,s=`#
`)=>i?(' '.repeat(--i)+s)+f(i,'##'+s):''

Test cases:

f=(i,s=`#
`)=>i?' '.repeat(--i)+s+f(i,'##'+s):''

console.log(f(1))
console.log(f(2))
console.log(f(3))
console.log(f(4))
console.log(f(5))
console.log(f(6))

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

JDK 13, 94 76 bytes

p->{for(int c=1;p>0;c+=2)System.out.println(" ".repeat(p--)+"#".repeat(c));}

TIO

With thanks to Kevin for some suggestions in the comments!

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ Ah, hadn't seen you posted a second answer. This can be 76 bytes as mentioned in my comment on your Java 8 answer. \$\endgroup\$ – Kevin Cruijssen Feb 7 at 10:18
0
\$\begingroup\$

Keg, 22 bytes

:&1ɧ⑷:⑻$-⑬*$⑵;`#`*⑸(
'

Try it online!

A direct rip off of my answer here

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 42 bytes

Copying the Python answer from xnor.

f=->n,i=1{1/n;puts" "*~-n+?#*i;f[n-1,i+2]}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
1
2

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