1. Write a function called censor. The function is passed a string as a paramete
ID: 3564592 • Letter: 1
Question
1. Write a function called censor. The function is passed a string as a parameter, which consists of words separated by blanks and/or punctuation marks, newlines, etc. censor returns a string in which any 4-letter words are replaced with 'XXXX'. Also, all punctuation marks, etc. are removed, and each word in the return string is separated from the next by a single blank.
Here is an example of how your function should work.
>>> censor('Here. This is a test of your function. Here is some indented text.') 'XXXX XXXX is a XXXX of XXXX function XXXX is XXXX indented XXXX.'
2.Write a function called url. This function is passed a string that represents an HTML hyperlink. The function returns the hyperlink's URL. For our purposes, a hyperlink takes this format:
For example:
If included in a .html file, the above hyperlink is displayed in a browser window as:
Google homepage
If the user clicks on the hyperlink, the page at http://www.google.com is displayed.
Here is an example of how your function should behave:
Explanation / Answer
1.
import re
def censor(inp):
mystr = re.sub('[^a-zA-Z0-9]',' ',inp).split()
newstr = ''
for item in mystr:
if(len(item)== 4):
item = 'XXXX'
newstr = newstr + item + ' '
return newstr
2.
import re
def url(inp):
obj = inp.split('"')
if( obj is not None):
if 'http://' in obj[1]:
print obj[1][7:]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.