Jun-04-2020, 10:57 PM
(This post was last modified: Jun-04-2020, 10:57 PM by knackwurstbagel.)
I see that you have extracted the last word, what happens if you last_word.strip("!") on this. The other thing is I happen to be a member of PyBites and solved that one long ago, I did it a completely different way but I noticed in the problem description
I would like to note that python documentation on str.strip methodsays
Quote:- strip the trailing dot (.) and exclamation mark (!) from this last word,that and should be or and have notified the PyBites team.
I would like to note that python documentation on str.strip methodsays
Quote:Return a copy of the string the leading and trailing characters removed.Let us think about a situation where the line contains both:
results_as_string = 'Sed a tincidunt nisl Mauris!.' results_as_string.strip('!').strip('.')What does this really mean. Well if we are to chain the methods, it first try to take '!' off the end of
results_as_string
but the last character is a '.' so what's does it return? It returns the same string back, unmodified. Now the second call to strip takes the result of the last strip and then looks for a '.', It finds one and removes it. You would get back 'Sed a tincidunt nisl Mauris!'
So the dot was removed but ! was ignored. But looking at the example in the python documentation, it implies that you could provide a string with the characters to be removed like so:results_as_string = 'Sed a tincidunt nisl Mauris!.' results_as_string.strip('!.')So now we understand, for every character you want to strip, put them all in a string. Ah ha! Ok but now why are we adding the last_word to results before striping '!' or '.' from the ending. Lets try something like this:
results = [] stripped = text.strip() splitted = stripped.split("\n") # naive debug: # print(f"First debug:{splitted}") for line in splitted: # strip off any leading spaces: line.lstrip(' ') line.rstrip(' ') # naive debug: # print(f"Second debug: {line}") # check if the first character is lowercase: if line[0].islower(): # split the line into words and get the last word: new_line_split = line.split() # naive debug: # print(f"Third debug: {new_line_split}") last_word = new_line_split[-1] last_word_striped = last_word.strip('!.') # naive debug: # print(f"Fourth debug: {last_word} {last_word_striped}") results.extend(last_word_striped) results_as_string = ''.join(results) print(results_as_string)Striping it before adding it to the results. Now I have not run this code so I do not know if it all works but it should or be pretty close to working. Let me know.