Python SolutionsPython Solutions

🔻

Inverse Word Pyramid

Week 41, 2026

All Solutions

Python - head & tail | BMC | Python Solutions

def inversePyramid( sentence :str, initialPadding :int, paddingChange :int, leftWordDec :int, rigthWordDec :int ) -> list: ans = [] s = sentence.split(' ') head = 0 tail = len(s) while len(s) > 0 and head < tail: print(f"{" "*initialPadding}{" ".join(s[head:tail])}") ans.append(f"{" "*initialPadding}{" ".join(s[head:tail])}") initialPadding += paddingChange head += leftWordDec tail -= rigthWordDec return ans def writeFile(fileName:str, data:list[list[str]]) -> None: try: file = open(fileName, 'w') for a in data: for b in a: file.write(b+'\n') file.close() print(f"{len(data)} lists have been written to {fileName}.") except IOError as e: print(e) def main() -> None: # sentence = "Insanity is doing the same thing over and over again and expecting different results" # inversePyramid(sentence, 0, 0, 1, 1) # inversePyramid(sentence, 5, 3, 2, 1) sentence = "The things you do every day matter more than the things you do once in a while" answers = [] answers.append(inversePyramid(sentence, 1, 2, 3, 4)) answers.append(inversePyramid(sentence, 0, 0, 1, 1)) answers.append(inversePyramid(sentence, 40, -5, 2, 0)) answers.append(inversePyramid(sentence, 10, 5, 1, 1)) answers.append(inversePyramid(sentence, 4, 4, 4, 4)) answers.append(inversePyramid(sentence, 0, 0, 0, 1)) writeFile("ans.txt", answers) if __name__ == "__main__": main()