Hackerrank Solution: Triangle Quest in Python [3 Methods]


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

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:

triangle

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

Python for loop
Question on Hacker Rank: Triangle Quest in Python

 

Related Keywords: triangle quest hackerrank solution, triangle quest 1 hackerrank solution in python, triangle quest 1 hackerrank solution in python, triangle quest 1 hackerrank solution in pythobn3

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment