Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13077
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'string.': 0.04; '3.2': 0.05; 'passes': 0.05; '"""': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; '(there': 0.09; '__name__': 0.09; 'guys.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'def': 0.15; '"__main__":': 0.16; 'reedy': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'jan': 0.19; 'stuff': 0.22; 'header:In-Reply-To:1': 0.22; 'starts': 0.24; 'saying': 0.26; 'string': 0.26; 'code.': 0.26; "i'm": 0.27; 'skip:[ 10': 0.27; 'separate': 0.28; 'bit': 0.28; 'import': 0.28; 'differently': 0.30; 'hiding': 0.30; "skip:' 10": 0.30; 'least': 0.31; 'certainly': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; 'instead': 0.33; 'header:User-Agent:1': 0.34; 'elegant': 0.34; 'header:X-Complaints-To:1': 0.35; 'unless': 0.36; 'issue': 0.36; 'rest': 0.37; 'but': 0.37; 'think': 0.38; 'received:org': 0.38; 'some': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'correctly': 0.39; 'ways': 0.39; 'help': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'skip:d 20': 0.39; 'stop': 0.61; 'skip:n 30': 0.62; 'here': 0.65; 'here.': 0.66; 'it)': 0.67; 'further,': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Doctest failing |
| Date | Sat, 10 Sep 2011 13:59:02 -0400 |
| References | <678b89fd-82f4-4080-8b36-e4d0ba30cdbf@o9g2000vbo.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-74-109-121-73.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 |
| In-Reply-To | <678b89fd-82f4-4080-8b36-e4d0ba30cdbf@o9g2000vbo.googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.943.1315677614.27778.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1315677614 news.xs4all.nl 2519 [2001:888:2000:d::a6]:43066 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:13077 |
Show key headers only | View raw
On 9/10/2011 7:20 AM, Tigerstyle wrote:
> Hi guys.
>
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
We appreciate you saying so instead of hiding that this is homework.
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
>
> def book_title(title):
> """ Takes a string and returns a title-case string.
> All words EXCEPT for small words are made title case
> unless the string starts with a preposition, in which
> case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
The key issue is that you want to treat the first word one way (.title
it) and the rest differently (conditionally .title or not) . So
immediately separate the first from the rest and then process each.
There are at least three ways to do the split. Perhaps I should stop
with this hint, and certainly you should think a bit before reading
further, but here is what I consider to be the most elegant 3.2 code.
.
,
,
,
.
.
.
first, *rest = title.strip().lower().split()
new_title = [first.title()]
for word in rest:
if word not in small_words:
word = word.title()
new_title.append(word)
> return(' '.join(new_title))
doctest.testmod() now passes (there is no 'refactory' here)
> def _test():
> import doctest, refactory
> return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-10 04:20 -0700
Re: Doctest failing Mel <mwilson@the-wire.com> - 2011-09-10 07:43 -0400
Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:36 -0700
Re: Doctest failing Peter Otten <__peter__@web.de> - 2011-09-10 13:47 +0200
Re: Doctest failing ting@thsu.org - 2011-09-10 19:12 -0700
Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:42 -0700
Re: Doctest failing Thomas Jollans <t@jollybox.de> - 2011-09-10 13:50 +0200
Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:39 -0700
Re: Doctest failing Alister Ware <alister.ware@ntlworld.com> - 2011-09-10 12:24 +0000
Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-11 01:56 +1000
Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:40 -0700
Re: Doctest failing Ethan Furman <ethan@stoneleaf.us> - 2011-09-11 11:43 -0700
Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-12 11:03 +1000
Re: Doctest failing Ben Finney <ben+python@benfinney.id.au> - 2011-09-12 11:37 +1000
Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-12 13:06 +1000
Re: Doctest failing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-12 14:29 +1000
Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-10 13:59 -0400
Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 04:46 -0700
Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-11 13:03 -0400
Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-10 15:36 -0400
Re: Doctest failing Peter Otten <__peter__@web.de> - 2011-09-10 22:49 +0200
Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-10 16:25 -0700
Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-10 23:18 -0700
Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:43 -0700
Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-11 12:37 -0700
csiph-web