PYTHON Challenge
Problem: Write a python class and a method called getPositions to extract the positions of a given value.
If no argument were provided, the default value is 0.
clue:
data = [1, 0, -1, 0, 0, 1, 1, 0, 0, -1]
item = MyClass(data)0
print(item.getPositions())
print(item.getPositions(1))
print(item.getPositions(-1))
data = [1, 0, -1, 0, 0, 1, 1, 0, 0, -1]
item = MyClass(data)0
print(item.getPositions())
print(item.getPositions(1))
print(item.getPositions(-1))
Expected output:
[1, 3, 4, 7, 8]
[0, 5, 6]
[2, 9]
Solution:
data=[1,0,-1,0,0,1,1,0,0,-1]
class Myclass():
def __init__(self,data):
self.data=data
def getPositions(self,pos=0 ):
return [i for i,e in enumerate(data) if e==pos]
item = Myclass(data)
print(item.getPositions())
print(item.getPositions(1))
print(item.getPositions(-1))
Comments
Post a Comment