Reference no: EM132354775
Question
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]).