1

I'm trying to write some code to give me an output that looks like:

              *
             * *
            * * *
           * * * *
          * * * * *
           * * * *
            * * *
             * *
              *

Given my limited r experience the closest I got was:

for(i in 1:5) {
  print(strrep("*", i))
}

for(i in 4:1) {
  print(strrep("*", i))
}

which gives me:

# *
# **
# ***
# ****
# *****

and

# ****
# ***
# **
# *

Any guidance would be really helpful.

Thanks in advance!

etuo
  • 155
  • 1
  • 11

1 Answers1

2

Pad extra space

for(i in c(1:5, 4:1))
  cat( paste0(strrep(" ", 5 - i), strrep("* ", i), "\n") )

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248