Challenge #5: Monkey Business
This exercise depends on a simple but unusual technique.
The 'string' library in Python has a function 'capwords': http://python.org/doc/lib/node110.html
You already have a program that uses this function to display headlines, but your editorial guidelines have changed. Now, a word should not be capitalized if it is three letters or less. Write a new function that does this. But the problem is that you call 'capwords' all over the place already. Can you make a change in one place so that you don't have to change all these occurrences? Demonstrate how.
For a hint, use a decoder from challenge 3 to read this string:
wklv whfkqltxh lv xvxdoob fdoohg prqnhb sdwfklqj
You already have a program that uses this function to display headlines, but your editorial guidelines have changed. Now, a word should not be capitalized if it is three letters or less. Write a new function that does this. But the problem is that you call 'capwords' all over the place already. Can you make a change in one place so that you don't have to change all these occurrences? Demonstrate how.
For a hint, use a decoder from challenge 3 to read this string:
wklv whfkqltxh lv xvxdoob fdoohg prqnhb sdwfklqj

>>> from string import capwords
>>> capwords
<function capwords at 0x402377d4>
>>> ts = 'blah blah bla bla bl bl a a blahh blahh'
>>> capwords(ts)
'Blah Blah Bla Bla Bl Bl A A Blahh Blahh'
>>> def capbigwords(st):
......words = st.split(' ')
......c_words = []
......for word in words:
........if len(word) > 3:
..........cword = word.capitalize()
..........c_words.append(cword)
........else:
..........c_words.append(word)
......return ' '.join(c_words)
...
>>> capbigwords(ts)
'Blah Blah bla bla bl bl a a Blahh Blahh'
>>> capwords = capbigwords
>>> capwords(ts)
'Blah Blah bla bla bl bl a a Blahh Blahh'
>>> def capbigwords(st):
......return ' '.join([t[len(t[1])>3] for t in [(word, word.capitalize()) for word in ts.split(' ')]])
http://mountainbunker.org/~jenn/python-challenges/python-challenge-5.txt
Not that these plain text files are much better, really....
http://mountainbunker.org/~cbearden/PythonChallenge/challenge05.py.txt