Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!