Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear Solution solution in Clear category for Fibonacci Poem by mAzrunnr
def fibo_poem(text: str) -> str:
fib_prev, fib_current = 0, 1 # Fibonacci sequence variables
poem = "" # Resulting Fibonacci poem
words = text.split() # Split input text into words
while words:
if len(words) < fib_current:
# If there are fewer words than the current Fibonacci number,
# extend the word list with underscores
words += ['_'] * (fib_current - len(words))
# Append the current group of words to the poem
poem += ' '.join(words[:fib_current]) + '\n'
# Remove the processed words from the word list
words = words[fib_current:]
# Move to the next Fibonacci numbers
fib_prev, fib_current = fib_current, fib_prev + fib_current
return poem.rstrip('\n')
print("Example:")
print(fibo_poem("Zen of Python"))
# These "asserts" are used for self-checking
assert fibo_poem("") == ""
assert fibo_poem("Django framework") == "Django\nframework"
assert fibo_poem("Zen of Python") == "Zen\nof\nPython _"
assert (
fibo_poem("There are three kinds of lies: Lies, damned lies, and the benchmarks.")
== "There\nare\nthree kinds\nof lies: Lies,\ndamned lies, and the benchmarks."
)
print("The mission is done! Click 'Check Solution' to earn rewards!")
June 23, 2023