279. Python Program to Print Floyd's Triangle
EasyPhase 1 · Warm-up
You know nested for loops and print() with end. That is all you need here.
Overview
Floyd's triangle is named after Robert Floyd, a computer scientist. It is a common pattern question, because you keep one counter going across all the rows.
Problem Statement
Fatima numbers the seats in a small hall shaped like a triangle. Row 1 has 1 seat, row 2 has 2 seats, and so on. The seat numbers keep counting up from one row to the next.
Read a whole number n and print n rows of seat numbers. Row 1 is 1, row 2 is 2 3, row 3 is 4 5 6, and so on. Put one space between the numbers in a row.
Steps
- Read
nand turn it into a whole number. - Make a counter that starts at 1.
- Loop over the rows. In row number
i, printinumbers on one line: print the counter, then add 1 to it. - End the line after each row.
Examples
Example 1. Input 4 gives:
1
2 3
4 5 6
7 8 9 10
Why: the numbers never start again from 1. Row 3 ends at 6, so row 4 starts at 7.
Example 2. Input 2 gives:
1
2 3
Why: row 1 has 1 number and row 2 has 2 numbers, so the numbers go up to 3.
Input
One whole number n (1 or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
n lines. Line i holds i numbers, with one space between them. Every line starts at the left edge, with no space in front. Numbers with two digits make a row a little wider, and that is fine. A space at the end of a line is fine: the tests ignore it. The tests try 5 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.