Suppose we have a list of numbers called nums, we also have another string op representing operator like "+", "-", "/", or "*", and another value val is also given, we have to perform the operation on every number in nums with val and return the result.
So, if the input is like [5,3,8], then the output will be [15, 9, 24]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums, op, val): res= for i in nums: if op=='+': res.append(i+val) elif op=='-': res.append(i-val) elif op=='*': res.append(i*val) elif val: res.append(i//val) return res ob = Solution() nums = [5,3,8] print(ob.solve(nums, '*', 3))
[15, 9, 24]