57
\$\begingroup\$

Inspired by a task for programming 101 here's a task that hopefully isn't too easy or is a duplicate (kinda hard to search for things like this).

Input:

  • A positive integer n >= 1.

Output:

  • n lines of asterisks, where every new line has one asterisk more than the line before, and starting with one asterisk in the first line.

General rules:

  • This is code-golf, so shortest answer in bytes wins.
  • Since the course is taught in C++, I'm eager to see solutions in C++.

Test case (n=5):

*
**
***
****
*****
\$\endgroup\$
  • 6
    \$\begingroup\$ Not duplicate, just subset of Generate a right triangle. \$\endgroup\$ – manatwork Oct 10 '16 at 12:20
  • 2
    \$\begingroup\$ Training spaces allowed on each line? \$\endgroup\$ – Luis Mendo Oct 10 '16 at 12:33
  • 2
    \$\begingroup\$ Is a trailing new line acceptable? \$\endgroup\$ – Fatalize Oct 10 '16 at 12:34
  • 1
    \$\begingroup\$ Is a leading newline allowed? \$\endgroup\$ – Riley Oct 10 '16 at 14:38
  • \$\begingroup\$ I don't see a reason why not. \$\endgroup\$ – Sickboy Oct 11 '16 at 11:35

134 Answers 134

1
\$\begingroup\$

JavaScript (ES6), 44 bytes

n=>[...Array(n)].map(_=>s+="*",s="").join`
`

If outputting as an array is permitted then subtract 8 bytes.

n=>[...Array(n)].map(_=>s+="*",s="")

Try it

f=
n=>[...Array(n)].map(_=>s+="*",s="").join`
`
oninput=_=>o.innerText=f(+i.value)
o.innerText=f(i.value=5)
<input id=i min=1 type=number><pre id=o>

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

tcl, 39

time {puts [string repe * [incr i]]} $n

demo

tcl, 46

while {[incr i]<=$n} {puts [string repe * $i]}

demo

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

RProgN 2, 5 bytes

²`**S

Outputs a stack of strings.

Explained

²`**S
²       # Define the function with the next two concepts, `* and * in this case.
    S   # Create a stack from 1 to the input, and execute the previous function on each element.
   *    # Multiply the element by
 `*     # The string "*", which repeats it. Output is implicit.

Try it online!

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

8th, 38 bytes

Code

( ( "*" . ) swap times cr ) 1 rot loop

SED (Stack Effect Diagram) is: n --

Example

ok> 5 ( ( "*" . ) swap times cr ) 1 rot loop
*
**
***
****
*****
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 37 Bytes

Anonymous VBE immediate window function that takes input from cell [A1] and outputs to range [B:B]

[B1].Resize([A1])="=Rept(""*"",Row())
\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 59 bytes

	n =input
o	output =dupl('*',x)
	x =lt(x,n) x + 1 :s(o)
end

Try it online!

SNOBOL4 (CSNOBOL4), 59 bytes

	n =input
o	x =x + 1
	output =le(x,n) dupl('*',x) :s(o)
end

Try it online!

The first prints with a leading newline, the latter without.

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

J, 11 Bytes

$&'*'@>:@i.

Includes trialing spaces on every line.

Explanation:

$&'*'@>:@i.    | Full program
     @  @      | Verb conjunction characters, make sure it isn't executed as a hook
         i.    | Integers 0 to n-1
      >:       | Increment (Integers 1 to n)
$&'*'          | Reshape the array '*' to the size of each item by repeating it cyclically

Note that normally the dyadic ranks of $ are 1 _, so that $&'*' 1 2 3 would create a 1 by 2 by 3 array of '*'s. However, the @ cunjunction ensures that $&'*' is applied to each cell of it's argument.

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

Julia 0.6, 13 bytes

n->"*".^(1:n)

^ is used for exponentiation and string repeating in Julia. This follows from the choice of '*' for string concatenation, which was chosen over '+' because addition is supposed to be commutative, and string concatenation is not. A function or operator preceded by . is "broadcasted", which in this case mean it is applied elementwise.

Outputs an array of strings. Depending on how the rules are interpreted it may need println.("*".^(1:n)) (23 bytes, meets any interpretation) or display("*".^(1:n)) (22 bytes, prints exact desired output plus an additional line about the array type) or "*".^(1:x).*"\n" (19 bytes, array of strings with newlines). Example of each in TIO.

Try it online!

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

Windows batch, 69 bytes

@set v=
@for /l %%G in (1,1,%1)do @call set v=*%%v%%&call echo %%v%%

Just putting an extra asterisk after each line.

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

Pyt, 17 bytes

←ř↔0⇹Á`⑴67**Ƈǰƥłŕ

Explanation:

←                             Get input
 ř↔                           Push [input,input-1,...,1] onto stack
   0⇹                         Push 0, and flip the top two items on the stack
     Á                        Push contents of array onto stack
      `         ł             While top of stack is not 0, loop:
       ⑴                     Create an array of 1s with length equal to the top of the stack
         67**                 Multiply each element in the array by 42
             Ƈǰƥ              Convert to ASCII, join, and print; if top of stack is 0, exit loop
                 ŕ            Pop the 0

Try it online!

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

brainfuck, 42 bytes

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

Try it online!

Takes the number as the char code of the input. Add two to avoid using negative cells, four to avoid wrapping.

How It Works:

Tape Format:  Input 0 10 * * * *...
++++++++++ Creates the newline cell
<<, Gets input
[ While input
 >>[>] Go to the end of the line of asterisks
 >--[<+>++++++]<- Add an asterisk to the end of the line
 [.<] Print the line including the newline
 <- Subtract one from the input
]
\$\endgroup\$
1
\$\begingroup\$

Stax, 4 bytes

m'**

Run and debug it

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

Japt -R, 4 bytes

õç'*

Try it here

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

Noether, 14 bytes

I("*"i1+*P?!i)

Try it online

\$\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.