Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function named indices with two parameters: an object, needle, and a list of objects, haystack. Your function should return the list of positions

Write a function named indices with two parameters: an object, needle, and a list of objects, haystack. Your function should return the list of positions of all occurrences of needle in haystack.

For example:

>>> indices []

>>> indices []

>>> indices [0]

>>> indices

[1, 2]

>>> indices [1, 3, 5]

(7 , []) (7 , [6]) (7 , [7,8,9]) (7 , [1,7,7,6]) (7 , [0,7,1,7,2,7,3])

Notice that the output is a list of positions. That is, in the last example, the result is [1, 3, 5] because the needle 7 appears in the haystack at positions 1, 3, and 5.

Hint: youll need to iterate through the haystack, and, if a given item matches the needle, append it to an accumulator. You are prohibited from using Pythons built-in searching methods, such as index. You are, however, permitted to use the indexing operator.

Your function should not use print or input at all. Test your code thoroughly before submitting it.

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_2

Step: 3

blur-text-image_3

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions