Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 6 . Create the Slice We called the variable slice because that is what we need to do now. Ultimately, the value you will

16. Create the Slice
We called the variable slice because that is what we need to do now. Ultimately, the value you will be returning will be cut from the original string s. Once you slice properly, the function should not work just on the string "apple, banana, orange", but also on the string "Billy, Andrew, Taylor". You should replace the right hand side of the assignment stub for slice with an expression slicing s.
This step is a little more complicated. Think about how we are slicing. We are slicing from one comma to another (not including the commas). As a result, this expression is going to require two new variables.
Examine the Expression
Why is that?
One for the first comma position, one for the second comma position.
One for the comma position, one for the string to search.
One for the comma position, one for suffix of the string.
One for the comma position, one for the space after the comma.
Answer: One for the first comma position, one for the second comma postion.
Once you have written this expression, you will need assignment stubs to keep the function from crashing. Again, we are going to use our test cases to guide us. Notice the positions of the commas in "apple, banana, orange". The first one is at position 5 and the second one is at position 13. In fact, the commas in Billy, Andrew, Taylor are at the exact same positions.
Create two variables for the two comma positions, and assign the first one the value 5, and the second one the value 13. Then use these variables to slice the input string and assign the result to our slice variable.
HINT: you may need to adjust the slice so it doesnt include either comma.
Run the test script again. If you did everything correctly, you should pass the first three tests! However, the fourth test will fail.Create the Slice
to guide us. Notice the positions of the commas in
"apple, banana, orange". The first one is at position
5 and the second one is at position 13. In fact, the
commas in "Billy, Andrew, Taylor" are at the exact
same positions.
Create two variables for the two comma positions,
and assign the first one the value 5, and the
second one the value 13. Then use these variables
to slice the input string and assign the result to
our variable.
HINT: you may need to adjust the slice so it
doesn't include either comma.
Run the test script again. If you did everything
correctly, you should pass the first three tests!
However, the fourth test will fail.
Check the Function
You may run this test multiple times.
LAST RUN on 1/15/2024,1:28:10 PM
The function crashed on input 'apple, banana,
orange' .
import introcs
def second_in_list(s):
"""
Returns: the second item in comma-separat
The final result should not have any whit
Example: second_in_list ('apple, banana, o
Example: second_in_list(' do, re ,
Example: second_in_list('z,y,x, w') return
Parameter s: The list of items
Precondition: s is a string of at least t
"""
#find position of first comma
first =s[6]
#find position of last comma
last =s[13]
#slice location of first and last comma
slice =s[first: last]
#strip the spaces from the result
result = introcs.strip(slice)
#return the result
return result
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

Whats involved in listening?

Answered: 1 week ago