Skip to content

Rhaptos Software Development

Personal tools
You are here: Home » Development » Tuesday Python Challenge » Challenge #5: Monkey Business

Challenge #5: Monkey Business

Document Actions
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

Created by jccooper
Last modified 2006-08-15 19:32

Solutions here

Posted by jccooper at 2006-08-15 19:34
Post answers here to avoid spoilers!

Re: Solutions here

Posted by kclarks at 2006-08-16 15:34
My answer isn't very concise. I tried to think of a fancier, shorter way to do it. But, I ended up deciding just to use the basic "for loop/if test" structure. Maybe I'll come up with something fancier later.

>>> 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'

Re: Re: Solutions here

Posted by kclarks at 2006-08-16 17:24
I've got a one line version of capbigwords() now. Its a bit ugly and its probably less efficient because it requires running capitalize() on every word even if it is less then three characters.

>>> def capbigwords(st):
......return ' '.join([t[len(t[1])>3] for t in [(word, word.capitalize()) for word in ts.split(' ')]])

Re: Solutions here

Posted by jenn at 2006-08-16 16:09
Boy, we really need a discussion tool that handles some kind of formatting. :P

http://mountainbunker.org/~jenn/python-challenges/python-challenge-5.txt

Not that these plain text files are much better, really....

Re: Solutions here

Posted by cbearden at 2006-08-17 09:18
My solution is at:

http://mountainbunker.org/~cbearden/PythonChallenge/challenge05.py.txt

Re: Re: Solutions here

Posted by cbearden at 2006-08-21 09:21
I forgot to mention that my solution is not necessarily generalizable. My monkey patch ensures that the first letter of the supplied string is upper-cased, even if the word it is a part of is <= 3 letters long. If you aren't operating on whole sentences, my solution would probably be wrong, since it would capitalize some <= 3-letter-words in the middle of sentences.