I'm attempting to solve a homework problem:
The first explorers of the Napieuler Mountains, in their leisure time, entertained themselves by shouting different phrases, to listen how the echo sounded. In their games, they noticed that the first echo was always a fraction of the original phrase, and the second was the same fraction of the first echo, and so on until everything was silent. For example, in the Napieuler area, the fraction was approximately 0.368.
When shouting a phrase of 100 seconds long, the first echo was 100 * 0.368 seconds long. The second was 100 * 0.368 * 0.368, and so on until it was imperceptible. Write a program that approximates, in text, the echo of the Napieuler Mountains. The program must receive by console the characteristic fraction of the echo that it wants to approximate (as a single decimal number). Then program must need to call a recursive function that you should receive a phrase. Finally, you should print out all the echoes of the phrase, including the original phrase shouted by a human. You must also show the total of repetitions, including the original phrase. As you can not easily calculate the time duration of a written phrase, you can assume that each letter takes a constant time, including spaces and punctuation. Round down multiplications with non-integer result. If you do the program correctly without calling a recursive function you will have a 0 in the problem.
Your function needs to be
Examples:
Enter the fraction: 0.369
Enter the sentence: I love mac and cheese
I love mac and cheese
cheese
se
Total of echoes: 3
Enter the fraction: 0.369
Enter the sentence: Today it is not a beautiful spring day. I wish ot was.
Today it is not a beautiful spring day. I wish ot was.
day. I wish it was.
it was.
s.
Total of echoes: 4
I've started writing the code, but I keep getting a stack-overflow error. Any help would be appreciated.
The code that keeps producing the stack-overflow error:
def echo(a,b):
if len(b)>=2:
return [b]+echo(a,b[(-(int(len(b)*a))):])
else:
return []
print(echo(0.369,"I love mac and cheese."))