Word Replacement X47082


Statement
 

pdf   zip   main.py

Write a function replace(ws,x,y)replace(ws, x, y) that given a list of words wsws, and two words xx and yy, replaces all occurrences of xx in wsws with yy. 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