PROG0353 - Title case

no tags 

Among U.S. book publishers (but not newspaper publishers), it is a common typographic practice to capitalize "important" words in titles and headings. This is an old form of emphasis, similar to the more modern practice of using a larger font or a boldface font for titles. This family of typographic conventions is usually called title case.

title case

The rules for which words to capitalize are not based on any grammatically inherent correct/incorrect distinction and are not universally standardized. They are arbitrary and differ between style guides, although most styles they tend to follow a few strong conventions. In general, most styles capitalize all words except for closed-class words: certain parts of speech such as articles (a, an, the), prepositions (in, to, at, …) and conjunctions (and, but, yet, …). The first word of a sentence is always capped, regardless of part of speech.

Assignment

Write a function titleCase that transforms a given sentence into title case. The function should capitalize the first letter of each word, whereas all other letters retain their original spelling. The words of a sentence are defined as the longest possible sequence of letters, quotes (') and hyphens (-). The given sentence must be passed as an argument to the function. The function also has a second optional parameter, that takes a list of words. Words from this list should never be capitalized, unless they appear as the first word of a sentence. Determining if a word belongs to the given list of words should happen case insensitive. The title cased sentence must be returned by the function.

Example

>>> words = ['above', 'about', 'across', 'against', 'along', 'among', 'around', 'at', 'before', 'behind', 'below', 'beneath', 'beside', 'between', 'beyond', 'by', 'down', 'during', 'except', 'for', 'from', 'in', 'inside', 'into', 'like', 'near', 'of', 'off', 'on', 'since', 'to', 'toward', 'through', 'under', 'until', 'up', 'upon', 'with', 'within', 'a', 'the', 'an', 'and', 'but', 'or', 'nor', 'yet', 'so', 'to']

>>> titleCase('Great minds think of great things.')
'Great Minds Think Of Great Things.'

>>> titleCase('Great minds think of great things.', words)
'Great Minds Think of Great Things.'

>>> titleCase("You and I share the same DNA.", words)
'You and I Share the Same DNA.'

Onder Amerikaanse uitgevers van boeken (maar niet bij de uitgevers van kranten) is het gebruikelijk om "belangrijke" woorden in titels en koppen met een hoofdletter te schrijven. Het is een oude vorm om nadruk te leggen op bepaalde delen van een tekst, vergelijkbaar met het gebruik van een groter en vetter lettertype voor titels in moderne vormgeving. Deze typografische conventie wordt doorgaans title case genoemd.

title case

De regels die aangeven welke woorden met een hoofdletter moeten beginnen zijn niet gebaseerd op een duidelijk grammaticaal onderscheid en er bestaat ook geen universele standaard. De regels zijn eerder arbitrair en durven nogal uiteenlopen tussen verschillende stijlgidsen, alhoewel de meeste wel een aantal geijkte vuistregels gebruiken. Zo zullen de meeste stijlgidsen alle woorden met een hoofdletter schrijven. Een aantal woorden vormen hierop echter een uitzondering en worden nooit met een hoofdletter geschreven tenzij ze het eerste woord in de zin vormen. Doorgaans worden lidwoorden (a, an, the), voorzetsels (in, to, at, …) en voegwoorden (and, but, yet, …) niet met een hoofdletter geschreven, tenzij aan het begin van een zin.

Opgave

Schrijf een functie titleCase die een gegeven zin omvormt naar title case. Hierbij moet telkens de eerste letter van elk woord omgezet worden naar een hoofdletter. De overige letters van het woord moeten hun originele schrijfwijze behouden. De woorden van een zin worden gedefineerd als de langste mogelijke opeenvolging van letters, aanhalingstekens (') en koppeltekens (-). De gegeven zin moet als eerste argument aan de functie doorgegeven worden. De functie heeft ook een tweede optionele parameter, waaraan een lijst van woorden kan doorgegeven worden. Woorden uit deze lijst moeten nooit omgevormd worden zodat ze met een beginhoofdletter geschreven worden, tenzij ze het eerste woord van een zin vormen. Om te bepalen of een gegeven woord tot de zin behoort, mag geen onderscheid gemaakt worden tussen hoofdletters en kleine letters. De omgevormde zin moet door de functie teruggegeven worden.

Voorbeeld

>>> woorden = ['above', 'about', 'across', 'against', 'along', 'among', 'around', 'at', 'before', 'behind', 'below', 'beneath', 'beside', 'between', 'beyond', 'by', 'down', 'during', 'except', 'for', 'from', 'in', 'inside', 'into', 'like', 'near', 'of', 'off', 'on', 'since', 'to', 'toward', 'through', 'under', 'until', 'up', 'upon', 'with', 'within', 'a', 'the', 'an', 'and', 'but', 'or', 'nor', 'yet', 'so', 'to']

>>> titleCase('Great minds think of great things.')
'Great Minds Think Of Great Things.'

>>> titleCase('Great minds think of great things.', woorden)
'Great Minds Think of Great Things.'

>>> titleCase("You and I share the same DNA.", woorden)
'You and I Share the Same DNA.'


Added by:Peter Dawyndt
Date:2013-03-30
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None