288. Python Program to Print a Palindrome Number Pyramid
MediumPhase 2 · Practice
This needs a few steps, and you know every tool in it: nested for loops, a range() that counts down, and print() with end.
Overview
A palindrome reads the same from left to right and from right to left, like 121. In this pyramid every row is a palindrome, so each row counts up and then back down.
Problem Statement
Hana makes a number pyramid for a classroom maths poster. Read a whole number n and print n rows:
- Row
istarts withn - ispaces. - Then it counts up from 1 to
i. - Then it counts back down from
i - 1to 1.
There are no spaces between the digits. Row 3, for example, is 12321. Each row has 2 * i - 1 digits, like the stars in a star pyramid, so the pyramid is centred.
Examples
Example 1. Input 4 gives:
1
121
12321
1234321
Why: row 4 has no spaces. It counts up from 1 to 4 and then back down to 1, and 4 is the middle digit, written once.
Example 2. Input 2 gives:
1
121
Why: row 1 has 1 space and the digit 1. Row 2 counts up to 2 and back down to 1.
Input
One whole number n, from 1 to 9 (so every number in the pyramid has one digit). 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. The spaces at the start of a line matter: the tests check them. The tests try 6 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.