Replacement of zeros X64306


Statement
 

pdf   zip

thehtml

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

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
Language
English
Official solutions
Python
User solutions
Python