# Hackerrank Solution: Triangle Quest in Python [3 Methods]

## Question: Triangle Quest - Hacker Rank (Python)

You are given a positive integer N. Print a numerical triangle of height N – 1 like the one below:

``````1
22
333
4444
55555
......``````

Can you do it using only arithmetic operations, a single for loop, and a print statement?

Use no more than two lines. The first line (the for statement) is already written for you. You have to complete the print statement.

Note: Using anything related to strings will give a score of 0.

Input format:

A single line containing an integer, N.

Constraints:

1 <= N <= 9

Output format:

Print N – 1 line as explained above.

Sample input:

``5``

Output:

``````1
22
333
4444``````

## Possible solutions

As you can see, we need a Python loop to solve the question using a max of two lines. We can use for loop in one line which takes the input from the user and then in the next line we can print out the required triangle:

• Using for loop
• Using for loop-2

Let us use the for loop to find the solution.

### Solution-1: Using For loop

for loops are used when you have a block of code that you want to repeat a fixed number of times. The for-loop is always used in combination with an iterable object, like a list or a range. The Python for statement iterates over the members of a sequence in order, executing the block each time.  Now let us use the for loop to solve the question.

``````# for loop which takes input value from user
for i in range(1,int(input())):

# printing the required output
print(i*((10**i-1)//9))``````

Input:

``5``

Output:

```1
22
333
4444```

As you can see, we get the desired output. But if you just copied the code and past it, with comments, you might not be able to complete the solution on hacker rank. Because as mentioned our code should not be longer than 2 lines. So, it is recommended to run the above code in the editor of Hacker Rank without any comments as shown below: As you can see, we are running the code without any comments.

### Solution-2: Using for loop-2

This time we will again use the for loop but with a different strategy to find the required triangle as shown below:

``````# for loop which takes the input
for i in range(1,int(input())):

# printing the required triangle
print (i * int(bin(2**i - 1)[2:]))``````

Input:

`5`

Output:

```1
22
333
4444```

Again in order to pass the test on Hacker rank, you should write the code without comments.

## Summary

In this short article, we solved the triangle quest from Hacker Rank. There is only one possible solution because we have to pass the test using only two lines. So, we can only use it for loop.

## References

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!