Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how to solve: Construct a Python function named finder that takes two string parameters, needle and haystack. Your function should find the first (i.e. left-most)

how to solve:

Construct a Python function named finder that takes two string parameters, needle and haystack. Your function should find the first (i.e. left-most) occurrence of needle in haystack and return its position as an int, where 0 means the first character in the string, and so on. If needle does not occur in haystack, your program should return 1.

Examples:

finder ('a' , 'bananas ') # 1

finder ('na' , 'bananas ') # 2

finder ('nas' , 'bananas ') # 4

finder ('banana ' , 'bananas ') # 0

finder ('nanner ' , 'bananas ') # -1

For the purposes of this problem, you must implement your solution as a loop (either while or for, your choice). Specifically, the index, find, and other functions on string objects are prohibited. You are, however, permitted to use the indexing and slicing operators (that is, s[n] and s[n:m]).

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

Students also viewed these Programming questions

Question

What are four recommendations for designing a successful MSS?

Answered: 1 week ago