Member-only story

Pyramid of Letters (Difficult Python Practice Question 3)

Liu Zuo Lin
2 min readJan 29, 2024

--

Day 74 of experimenting with video content

Write a function pyramid(string) that takes in a string, and prints the following pyramid-like pattern.

  • assume string has no spaces
  • the 1st line has 1 character, the 2nd line has 2, the 3rd line has 3 etc
  • if there are insufficient characters, use * to pad your row.
pyramid('abcdef')

a
bc
def
pyramid('abcde')

a
bc
de*
pyramid('abcdefg')

a
bc
def
g***

Do take some time to give this a go before taking a look at the answer.

Getting to the answer

Let’s use the string abcdefg as an example.

a
bc
def
g***

We need to keep track of 1) the row length 2) index of starting character

      row_length   start_character  start_index
a 1 a 0
bc 2 b 1
def 3 d…

--

--

Liu Zuo Lin
Liu Zuo Lin

Written by Liu Zuo Lin

SWE @ Meta | [Ebook] 101 Things I Never Knew About Python: https://payhip.com/b/vywcf

Responses (1)