Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated for Understanding "Find Quotes" solution in Clear category for Find Quotes by BootzenKatzen
def find(str, ch): # Our two arguments for this function are a string and a character
for i, ltr in enumerate(str): # enumerate takes the items from our iteration (in this case
# each letter in the string) and numbers them, so you end up with
# pairs like (0, a) (1, b) etc. So for each letter
if ltr == ch: # if that letter is the character we're searching for
yield i # it gives us the first part of the pair (the count)
# Which is also the index for that character
def find_quotes(a):
quotes = [] # establishing an empty list
if '"' in a: # if there is a quote present in the test
marks = list(find(a, '"')) # we create a list called marks with all the indexes of " used
# We use list() because find() yeilds a "generator" not a list
while len(marks) > 0: # While we have some values in our marks list
text = a[marks.pop(0) + 1 :marks.pop(0)] # text = a[first index + 1 : second index]
# pop removes the value from the list which is why we use pop(0)
# twice to get the pair of quotation marks
# we add 1 to the first index to exclude the quotation mark
# we don't need to -1 from the other end, because the index
# only goes up to and not including the last character
quotes.append(text) # add the quote we found to our quotes list
# if we didn't have any " it would just jump down to here
return quotes # and we return our list of quotes
# if there were no quotes it would return the empty list
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!")
May 16, 2023
Comments: