In: Computer Science
My output is incorrect when i put an empty string first_inside_quotes(' "" '). Not sure where the issue lies
import introcs
def first_inside_quotes(s):
'''
Returns the first substring of s between two (double) quote
characters
Note that the double quotes must be part of the string. So "Hello
World" is a
precondition violation, since there are no double quotes inside the
string.
Example: first_inside_quotes('A "B C" D') returns 'B C'
Example: first_inside_quotes('A "B C" D "E F" G') returns 'B C',
because it only
picks the first such substring.
Parameter s: a string to search
Precondition: s is a string with at least two (double) quote
characters inside
'''
#finds the first quote in the string
openedquotes = introcs.find_str(s, '"')
print('position of first quotes' ,openedquotes)
print(type(openedquotes))
#finds the second quote in the string asfter the first one
closequotes = introcs.find_str(s, '"', openedquotes + 1, -1)
print('position of second quotes', closequotes)
print(type(closequotes))
#slices my original string so I get what is in between the
quotes
slicing = s[openedquotes + 1 : closequotes]
return slicing def first_inside_quotes(s):
'''
Returns the first substring of s between two (double) quote
characters
Note that the double quotes must be part of the string. So "Hello
World" is a
precondition violation, since there are no double quotes inside the
string.
Example: first_inside_quotes('A "B C" D') returns 'B C'
Example: first_inside_quotes('A "B C" D "E F" G') returns 'B C',
because it only
picks the first such substring.
Parameter s: a string to search
Precondition: s is a string with at least two (double) quote
characters inside
'''
#finds the first quote in the string
openedquotes = introcs.find_str(s, '"')
print('position of first quotes' ,openedquotes)
print(type(openedquotes))
#finds the second quote in the string asfter the first one
closequotes = introcs.find_str(s, '"', openedquotes + 1, -1)
print('position of second quotes', closequotes)
print(type(closequotes))
#slices my original string so I get what is in between the
quotes
slicing = s[openedquotes + 1:closequotes]
return slicing
I have modified your code in the line where the position of the second quote is to be found. Now it works fine.
Python Code:
import introcs
def first_inside_quotes(s):
'''
Returns the first substring of s between two (double) quote characters
Note that the double quotes must be part of the string. So "Hello World" is a
precondition violation, since there are no double quotes inside the string.
Example: first_inside_quotes('A "B C" D') returns 'B C'
Example: first_inside_quotes('A "B C" D "E F" G') returns 'B C', because it only
picks the first such substring.
Parameter s: a string to search
Precondition: s is a string with at least two (double) quote characters inside
'''
#finds the first quote in the string
openedquotes = introcs.find_str(s, '"')
print('position of first quotes' ,openedquotes)
print(type(openedquotes))
#finds the second quote in the string asfter the first one
closequotes = introcs.find_str(s[openedquotes + 1:], '"') + openedquotes + 1
print('position of second quotes', closequotes)
print(type(closequotes))
#slices my original string so I get what is in between the quotes
slicing = s[openedquotes + 1 : closequotes]
return slicing
print(first_inside_quotes('""'))
Output of the above code:
As you can see from the output, empty string is returned which is what we wanted.