In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 23rd
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #06 exercise sheet]
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
Apu Nahasapeemapetilon, the hardworking owner of the Kwik-E-Mart, is facing an organizational challenge. With Springfield's residents shopping (and sometimes shoplifting) at his store day and night, keeping track of his inventory is becoming more chaotic by the day. His shelves are full of various items - Buzz Cola cans, Squishee cups, Krusty-O's cereal boxes, donuts, and random convenience store essentials.
After a particularly busy shift, Apu notices that some items are scattered around the store in multiple places. He realizes he needs to track the exact positions of each item in his inventory list, so he can find out where and how often each item appears. For example, if Buzz Cola shows up in several spots in his inventory list, he wants to know the positions of each occurrence to better organize his shelves (and to know which items are running low).
Write a function occurrences(lst, x) that given a list lst and a value x returns a new list with the positions where the value x occurs. Note that, as obvious for Apu, the positions start in zero.
The following limits are guaranteed in all the test cases that will be given to your program:
0 ≤ |lst| ≤ 100 | Number of elements in list |
Example Function Calls | Example Output |
lst = ["cola","cereal","donut","cola","cola","donut","cola"] print(occurrences(lst, "cola")) print(occurrences(lst, "cereal")) print(occurrences(lst, "donut")) print(occurrences(lst, "water")) |
[0, 3, 4, 6] [1] [2, 5] [] |