Word Replacement X47082


Statement
 

pdf   zip   main.py

thehtml

Write a function replace(ws, x, y) that given a list of words ws, and two words x and y, replaces all occurrences of x in ws with y. The function must return two lists: The original list ws and the modified list with the replaced elements (if any).

Sample session
>>> replace(['the', 'cat', 'sat', 'on', 'the', 'mat'], 'cat', 'dog')
(['the', 'cat', 'sat', 'on', 'the', 'mat'], ['the', 'dog', 'sat', 'on', 'the', 'mat'])
>>> replace(['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'], 'wood', 'gum')
(['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'], ['how', 'much', 'gum', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'gum'])
>>> replace(['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'], 'woodchuck', 'beaver')
(['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'], ['how', 'much', 'wood', 'would', 'a', 'beaver', 'chuck', 'if', 'a', 'beaver', 'could', 'chuck', 'wood'])
>>> replace(['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'], 'groundhog', 'beaver')
(['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'], ['how', 'much', 'wood', 'would', 'a', 'woodchuck', 'chuck', 'if', 'a', 'woodchuck', 'could', 'chuck', 'wood'])
Information
Author
Language
English
Official solutions
Python
User solutions
Python