Replacement of zeros X57494


Statement
 

pdf   zip

Write a function @replacement(f, g)@ that provided two integer lists ff and gg returns the list obtained after replacing the zeros in list ff with values in gg. First zero has to be replaced with the first number in gg, the second zero with the second number in gg and so on, until there are no more zeros in ff or there are no more available numbers in gg. When values in gg have been exhausted no more replacements are done. Lists ff and gg do not have to change.

Sample session

Sample session
>>> replacement([1, -1, 0, 2, 0], [3, -2, 1])
[1, -1, 3, 2, -2]
>>> replacement([1, -1, 0, 2, 0], [5])
[1, -1, 5, 2, 0]
>>> replacement([1, -1, 0, 2, 0], [0])
[1, -1, 0, 2, 0]
>>> replacement([1, -1, 0, 2, 0], [])
[1, -1, 0, 2, 0]
>>> f, g = [0, 0, -1, 0, 0], [2, 5, 3]
>>> replacement(f, g)
[2, 5, -1, 3, 0]
>>> f == [0, 0, -1, 0, 0] and  g == [2, 5, 3]
True
Information
Author
ProAl
Language
English
Official solutions
Python
User solutions
Python