Question

In: Computer Science

What I have bolded is what is wrong with this function and cant figure out what...

What I have bolded is what is wrong with this function and cant figure out what needs to bee done to get the second position of this comma?Need help building this function to do what the specifications say to do

import introcs

def second_in_list(s):
"""
Returns: the second item in comma-separated list
  
The final result should not have any whitespace around the edges.
  
Example: second_in_list('apple, banana, orange') returns 'banana'
Example: second_in_list(' do , re , me , fa ') returns 're'
Example: second_in_list('z,y,x,w') returns 'y'
  
Parameter s: The list of items
Precondition: s is a string of at least three items separated by commas.
"""
  
slice = s
first = introcs.find_str(slice,',') #find position of my first comma
print(first)
second = introcs.find_str(slice,',',5,-1) #need to find the position after the first comma
print(second)
#need to useee the positions of my commas to slice the desired output (ie slice[first:seond])
slice2 = slice[first+1:second]
result = introcs.strip(slice2)
return result
return result

Solutions

Expert Solution

If I understand correctly from your code, you were trying to find second element in the list('apple, banana, orange'). The documentation of introcs.find_str says it has the following parameters:

  • text (str) – The string to search

  • sub (str) – The substring to search for

  • start (int) – The start of the search range

  • end (int) – The end of the search range

Now, looking from the function defination, the third parameter is the start. According to your code, the third parameter for the second introcs.find_str function is 5 i.e. the value of first. This is where you are making the mistake. The start value should be first + 1 instead of first because if you initialize the start parameter as first, it will also include the first comma in the resultant substring(the substring that you are searching the second time).

Below I have corrected your code in Python and also attached sample outputs.
Please upvote if you like my work.

Code screenshot:

Code to copy:

import introcs

def second_in_list(s):
    slice = s
    first = introcs.find_str(slice,',') #find position of my first comma
    print(first)
    second = introcs.find_str(slice,',',first+1,-1) #need to find the position after the first comma
    print(second)
    #need to useee the positions of my commas to slice the desired output (ie slice[first:seond])
    slice2 = slice[first+1:second]
    result = introcs.strip(slice2)
    return result

Inputs and outputs:


Related Solutions

The anwsers that was given is not correct. I have tried but cant figure it out>...
The anwsers that was given is not correct. I have tried but cant figure it out> out You are evaluating a project for The Ultimate recreational tennis racket, guaranteed to correct that wimpy backhand. You estimate the sales price of The Ultimate to be $440 per unit and sales volume to be 1,000 units in year 1; 1,250 units in year 2; and 1,325 units in year 3. The project has a 3-year life. Variable costs amount to $245 per...
Cant seem to figure out what is wrong here? On December 31, after adjustments, Gonzalez Company's...
Cant seem to figure out what is wrong here? On December 31, after adjustments, Gonzalez Company's ledger contains the following account balances: 101 Cash $ 94,400 Dr. 111 Accounts Receivable 35,600 Dr. 121 Supplies 8,000 Dr. 131 Prepaid Rent 81,200 Dr. 141 Equipment 128,000 Dr. 142 Accumulated Depreciation—Equip. 4,000 Cr. 202 Accounts Payable 17,000 Cr. 301 Emilio Gonzalez, Capital (12/1/2019) 131,240 Cr. 302 Emilio Gonzalez, Drawing 16,400 Dr. 401 Fees Income 327,200 Cr. 511 Advertising Expense 11,600 Dr. 514 Depreciation...
I have to figure this out in Excel. I have 4 columns with names in a...
I have to figure this out in Excel. I have 4 columns with names in a spreadsheet. I am trying to find the duplicates between columns and count them. Example A B C D Steve John Chris John Sarah Steve John Sarah Carl Chris Paul Paul Jenna Logan Sarah Jenna Moe Norman Joe Logan I am trying to identify the names that show up in multiple columns and count the times they show up, all in Excel. Which excel functions...
Can't figure out what I am doing wrong. Please let me know. Sample output: Please enter...
Can't figure out what I am doing wrong. Please let me know. Sample output: Please enter the name of the weather data file: temperatures.txt Tell me about your crew’s preferred temperature for sailing: What is the coldest temperature they can sail in? 60 What is the hottest temperature they can sail in? 80 Month 1: 93.3 percent of days in range. Month 2: 20.0 percent of days in range. Month 3: 58.1 percent of days in range. Month 4: 100.0...
I have done the first part of this problem, but I cannot figure out the second...
I have done the first part of this problem, but I cannot figure out the second part. I know there are several different answers already posted on Chegg for this particular problem with different acceleration and time, but I need this one specifically, with a step by step solution. Again...just PART B....the answer to Part A is 201.72 A helicopter carrying Dr. Evil takes off with a constant upward acceleration of 4.20 m/s2. Secret agent Austin Powers jumps on just...
How is this done in R-studio? I have been trying to figure it out but I...
How is this done in R-studio? I have been trying to figure it out but I am getting more and more confused. While imprisoned by the Germans during World War II, the English mathematician John Kerrich tossed a coin 10,000 times and obtained 5067 heads. Let p be the probability of a head on a single toss. We wish to check if the data are consistent with the hypothesis that the coin was fair. a) Set up the hypotheses. Why...
How to make an array without setting size?c++ cant figure this out without wanting to make...
How to make an array without setting size?c++ cant figure this out without wanting to make a vector or set a fixed size to the numbers. Prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side.
I have the answers for A through D. I just can't figure out the rest. Hercules...
I have the answers for A through D. I just can't figure out the rest. Hercules Exercise Equipment Co. purchased a computerized measuring device two years ago for $78,000. The equipment falls into the five-year category for MACRS depreciation and can currently be sold for $34,800. A new piece of equipment will cost $230,000. It also falls into the five-year category for MACRS depreciation. Assume the new equipment would provide the following stream of added cost savings for the next...
I have two questions that need to be answered, I can't figure them out: 1) Steve...
I have two questions that need to be answered, I can't figure them out: 1) Steve Coleman has just won the state lottery and has the following three payout options for afterminus?tax prize? money:1. $ 152 comma 000$152,000 per year at the end of each of the next six years2. $ 318 comma 000$318,000 ?(lump sum) now3. $ 500 comma 000$500,000 ?(lump sum) six years from now The annual discount rate is? 9%. Compute the present value of the second...
I have bolded the answers I chose. I just need someone to check my work and...
I have bolded the answers I chose. I just need someone to check my work and if I'm wrong possibly explain why, please? 1. If a stock pays a dividend, the owner of a call option will see the value of their option decrease by the amount of that dividend payment. a. true b. false 2. DEF stock currently trades for $40. Both American calls and puts are available on the stock. All else being constant, which of the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT