Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Not the shortest but explained solution in Clear category for Find Quotes by Selindian
def find_quotes(a):
list_of_idx = [i for i, ltr in enumerate(a) if ltr == '"'] # Create a list of all indexes of occurances of '"'
results = list()
while True: # Start a Loop
try: # ... try to ...
start = list_of_idx.pop(0) # ... ... get a staring index
end = list_of_idx.pop(0) # ... ... and a ending index
results.append(a[start+1:end]) # ... ... then add all between start and end to the list of results
except IndexError: # ... if it was not possible to get start and/or end index
break # ... ... break the loop
return results # return the list of results
if __name__ == '__main__':
print("Example:")
print(find_quotes('"Greetings"'))
# These "asserts" are used for self-checking and not for an auto-testing
assert find_quotes('"Greetings"') == ['Greetings']
assert find_quotes('Hi') == []
assert find_quotes('good morning mister "superman"') == ['superman']
assert find_quotes('"this" doesn\'t make any "sense"') == ['this', 'sense']
assert find_quotes('"Lorem Ipsum" is simply dummy text '
'of the printing and typesetting '
'industry. Lorem Ipsum has been the '
'"industry\'s standard dummy text '
'ever since the 1500s", when an '
'unknown printer took a galley of '
'type and scrambled it to make a type '
'specimen book. It has survived not '
'only five centuries, but also the '
'leap into electronic typesetting, '
'remaining essentially unchanged. "It '
'was popularised in the 1960s" with '
'the release of Letraset sheets '
'containing Lorem Ipsum passages, and '
'more recently with desktop '
'publishing software like Aldus '
'PageMaker including versions of '
'Lorem Ipsum.') == ['Lorem Ipsum',
"industry's standard dummy text ever "
'since the 1500s',
'It was popularised in the 1960s']
assert find_quotes('count empty quotes ""') == ['']
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 7, 2022
Comments: