Mutable Functions: Implement a function sentence_buffer which returns another one-argument function. This function will take in one word at a time, and it saves all the words that it has seen so far. If it takes in a word that ends in a period (""), that denotes the end of a sentence, and the function returns all the words in the sentence. It will also clear its memory, so that it no longer remembers any words. Your solution should contain nonlocal variable. Do not use a list variable to hold the words. def sentence_buffer(): "Returns a function that will return entire sentences when it receives a string that ends in a period. >>> buffer= sentence_buffer() >>> buffer ("This") >>> buffer("is") >>> buffer("Spot.") This is Spot." >>> buffer("See") >>> buffer("Spot") >>> buffer("run.") "See Spot run."
Step-1: Start
Step-2: Define function sentence_buffer()
Step-2.1: Declare variable buffer and assign ""
Step-2.2: Define function inner(word)
Step-2.2.1: Declare nonlocal buffer
Step-2.2.2: If buffer equal to "" then In buffer assign word
Step-2.2.3: else
Step-2.2.3.1: In buffer assign buffer + " " + word
Step-2.2.4: If word.endswith(".")
Step-2.2.4.1: Declare variable result and assign buffer
Step-2.2.4.2: In buffer assign ""
Step-2.2.4.3: Return result
Step-2.2.4: Return None
Step-2.3: Return inner
Step-3: In main
Step-3.1: Declare variable buffer and call function sentence_buffer()
Step-3.2: Print buffer("This")
Step-3.3: Print buffer("is")
Step-3.4: Print buffer("Spot.")
Step-3.5: Print buffer("See")
Step-3.6: Print buffer("Spot")
Step-3.7: Print buffer("run.")
Step-4: Stop
Step by step
Solved in 4 steps with 2 images