Operator list X54712


Statement
 

pdf   zip   main.py

html

Write a function operate(data, ops) that applies in turn the operators listed in ops to the data. The data is a list of n>0 integers. Then ops is a string of length n−1 made up of the characters ’+’ and ’*’ standing respectively for sum and product. The result of the function is to be the outcome of the operations in left-to-right order both of ops and of data: the first operator is applied to the first two integers, the second operator is applied to the outcome and the third integer, and so on.

Sample session
>>> operate([25, 3], '*')
75
>>> operate([1, 1], '+')
2
>>> operate([2, 3, 4], '*+')
10
>>> operate([2, 3, 4], '+*')
20
>>> operate([333], '')
333
>>> operate([5, 5, 5, 5], '+++')
20
>>> operate([5, 5, 5, 5], '+*+')
55
Information
Author
José Luis Balcázar
Language
English
Official solutions
Python
User solutions