Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
def staircase(n):
    for i in range(0,n):
        for j in range(0,n):
            if i + j >= n-1:
                print("#",end='') 
            else:
                print(" ",end='')
        print('\n')


staircase(4)


What I have tried:

mariostairs <- function(n){
      for (i in 0:n){
        for (j in 0:n){
          if (i+j >= n-1){
            print('x')
          }else{
            print('')
          }
        }
      }
    }
Posted
Comments
Dave Kreskowiak 11-Oct-22 17:03pm    
OK, and the question would be .... ?
Kevin Thomas Oct2022 11-Oct-22 17:06pm    
It works for python, how would it be coded in r studio?
Dave Kreskowiak 11-Oct-22 17:26pm    
You're making the assumption that print in R works the exact same way as it does in Python. That is not the case. You might want to look at cat in R.

Keep in mind, that you cannot just convert the code line-by-line and expect it work. R and Python work very differently from each other.
Dave Kreskowiak 11-Oct-22 17:58pm    
I will give you a hint though, you only need one loop to do this.
Kevin Thomas Oct2022 11-Oct-22 21:19pm    
mariostairs <- function(n){
for (i in 1:n) {
cat('x',sep='\n')
}
}

this is the best I can come up with, how can i add an x to each row?

1 solution

mariostairs <- function(n){
      for (i in 1:n) {
          cat(strrep('x',i),sep='\n')
        }
      }
    
     
    
      
      
    
          
     mariostairs(4)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900