Recently, somewhere, I came across a text saying that in Yandex (and not only for them) they like to ask a task about building a pyramid using symbolic graphs. I don’t know how true this is, but it became interesting – how can this be realized?
In fact, in addition to building a pyramid in the form of … a pyramid 🙂 this task can have options:
– a banal one-sided pyramid with an expansion right-down (it is solved by a banal cycle, an example of which is below):
– A more interesting task is with a pyramid with an extension left-down, and a pyramid with a center and an extension down-to-side is absolutely interesting. The main difficulty in this task, as it seems to me, is the construction of a cycle in a cycle. The very logic of this can be represented as follows: we have a variable that denotes the total maximum width of a tier, a variable responsible for the number of spaces in a tier, and a variable responsible for the number of # characters in a line. Those. the first line we have is composed of:
First tier: the total number of characters in the tier, of which one character at the end of the line is the # character, and all previous ones are spaces
Second tier: the total number of characters in the tier minus, of which two characters at the end of the line are the # character, and all the previous ones are spaces
and more … and of course, you can create a loop in a loop, however – why not go the other way?
Turn on lazy mode, and open one of my previous posts , dedicated to working with strings in Python. In one of the paragraphs, I talk about aligning text in lines: in the center, on the left side or on the right. Well – now everything is definitely very, very simple;):
As seen from the code:
piramide =  – create a list
text = str (‘#’) – create a variable text (the one that will fill the string)
quantity = int (15) – set the number of tiers
stage = int (0) – initial value of the number of tiers
print (text.rjust (quantity)) – print the list with alignment to the right (rjust is responsible for this (if you need to align the pyramid to the left – use ljust, if centered – center). At the same time, you need emphasize that if the code works in the center, the pyramid turns out to be somewhat … crooked, because we add one character to each subsequent line. To fix this, we need to add two characters to the last line;)
stage = stage + 1 – add one to the variable i.e. increase the number and gradually reach the specified maximum
text = text + ‘#’ – we also add a character to the variable – so that in the next iteration we print more per character.