Question

In: Computer Science

My output is incorrect when i put an empty string first_inside_quotes(' "" '). Not sure where...

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

Solutions

Expert Solution

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.


Related Solutions

The Table in my homework question below is completely wrong. I am not sure where I...
The Table in my homework question below is completely wrong. I am not sure where I went wrong in my calculations but coud you rework this question and answer the parts below?? Here are earnings per share for two companies by quarter from the first quarter of 2009 through the second quarter of 2012. Forecast earnings per share for the rest of 2012 and 2013. Use exponential smoothing to forecast the third period of 2012, and the time series decomposition...
I was not sure what "subject" to put it under, so I'll pick it under Psychology...my...
I was not sure what "subject" to put it under, so I'll pick it under Psychology...my question is What is the personal ethics, social ethics, and conservation ethics when it comes to Vape Pens or any sort of Vaping instrument. Can you go into detail about how it fits under the personal, social, and conservation ethics. Also explain why Vaping is considered bad to our health. Thanks!
Firstly, I wasn't sure exactly where to put this. It's a typesetting query but the scope...
Firstly, I wasn't sure exactly where to put this. It's a typesetting query but the scope is greater than TEX; however it's specific also to physics and even more specific to this site. I've recently been reading a style guide for scientific publications (based on ISO 31-11), however there was no mention of quantum mechanical operators. I've seen them written a few ways and was wondering if there was a decision handed down from "up above" that any particular way...
C++ i want .00 at the output cout << fixed << setprecision (2);where should i put...
C++ i want .00 at the output cout << fixed << setprecision (2);where should i put this line? quesstion 13. Array of Payroll Objects Design a PayRoll class that has data members for an employee’s hourly pay rate and number of hours worked. Write a program with an array of seven PayRoll objects. The program should read the number of hours each employee worked and their hourly pay rate from a file and call class functions to store this information...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
I worked on this my self but I am not sure about it and I feel...
I worked on this my self but I am not sure about it and I feel like I get confuse in explaining some of them, I want to capare and contrast the different types of membrane transport processes.( including differences and similarities) simple diffusion facilitated difusion osmosis primaryactive transport secendary active transport vesicular transport The comparison and similarities should be about each of these topics. 1.direction of transport 2.energy requirement 3.protein requirement 4.types of protein if applicable 5. example of...
Why does my code print nothing in cout? I think my class functions are incorrect but...
Why does my code print nothing in cout? I think my class functions are incorrect but I don't see why. bigint.h: #include <string> #include <vector> class BigInt { private:    int m_Input, m_Temp;    std::string m_String = "";    std::vector<int> m_BigInt; public:    BigInt(std::string s)   // convert string to BigInt    {        m_Input = std::stoi(s);        while (m_Input != 0)        {            m_Temp = m_Input % 10;            m_BigInt.push_back(m_Temp);       ...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
True or False) The following code will output "I was true". bool isGreater(string s1, string s2)...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT