r/dailyprogrammer • • Jul 14 '12
[7/13/2012] Challenge #76 [easy] (Title case)
Write a function that transforms a string into title case. This mostly means: capitalizing only every first letter of every word in the string. However, there are some non-obvious exceptions to title case which can't easily be hard-coded. Your function must accept, as a second argument, a set or list of words that should not be capitalized. Furthermore, the first word of every title should always have a capital leter. For example:
exceptions = ['jumps', 'the', 'over']
titlecase('the quick brown fox jumps over the lazy dog', exceptions)This should return:
The Quick Brown Fox jumps over the Lazy DogAn example from the Wikipedia page:
exceptions = ['are', 'is', 'in', 'your', 'my']
titlecase('THE vitamins ARE IN my fresh CALIFORNIA raisins', exceptions)Returns:
The Vitamins are in my Fresh California Raisins
30
Upvotes
5
u/benzinonapoloni Jul 14 '12
Somewhat readable python:
1
u/Fapper Jul 24 '12 edited Jul 24 '12
My shot at Ruby! Just started playing with it yesterday (have a C# background). Any tips to make it more "Rubylicious"?
3
u/linning Jul 14 '12
Java
3
u/Zamarok Jul 14 '12 edited Jul 15 '12
CoffeeScript:
3
u/nycthbris Jul 15 '12
Another python answer:
3
u/fripthatfrap Jul 16 '12
C:
2
u/Eddonarth Jul 14 '12
Java implementation:
2
u/gibsonan Jul 15 '12
C++ with Boost
2
u/drb226 0 0 Jul 15 '12
Testing:
2
u/drb226 0 0 Jul 15 '12
I made another Haskell implementation, this time one that correctly preserves punctuation and whitespace. I used the incredible
conduitspackage, the first time I seriously leveraged it for something like this. I learned a lot.2
u/whatdfc Jul 15 '12
Ruby
Any advice/criticism is appreciated. I'd also appreciate it if anyone has a particular recommendation for where I should go to have my Ruby/Rails code critiqued.
2
u/devilsassassin Jul 15 '12
Another Java one, using a hashmap:
output:
2
u/ixid 0 0 Jul 16 '12 edited Jul 16 '12
D version:
One-liner version added because people seem to like them:
3
u/Scroph 0 0 Jul 14 '12 edited Jul 15 '12
PHP one liner :
> The Vitamins are in my Fresh California RaisinsI should be put in jail for writing something this horrible.
Edit : I felt bad so I wrote it in D
Pretty standard. It could have been a lot shorter if only I knew how to use lambda functions in D.
2
u/Ttl Jul 14 '12
Quick Python hack:
2
u/semicolondash Jul 14 '12
C++ implementation. (so much bulkier than all the others. Curse having to define lowercase and split methods and all the explicit looping)
1
u/scurvebeard 0 0 Jul 15 '12 edited Jul 15 '12
So for this one I threw in a list of common exceptions which will apply regardless of the inputted exceptions. It came out to ~20 lines, but I'm not real hip with Python just yet so I used basic commands to carry this one out. No doubt there are faster methods I could have used, but I'm doing my damnedest with what I got.
I accidentally made everything into a list of words, so the last bit of this code is a little redundant due to my poor planning and ignorance as to how best to fix it. Frankly, I'm just glad to have completed this one at all.
1
u/[deleted] Jul 15 '12
Java private void titleCase(String string, String[] exceptions){
1
u/eine_person Jul 15 '12
Ruby! =)
1
u/Mysidic Jul 15 '12 edited Jul 15 '12
Common LISP: I'm looking for some criticism on my code, since LISP is so diffrent from anything I've done before.
1
u/AlexDiru Jul 15 '12
C# - bit messy with all of the trims
1
u/tvorryn Jul 16 '12
Racket that preserves whitespace and uses regexes:
And tests:
1
u/tvorryn Jul 16 '12 edited Jul 16 '12
Racket one liner shorter than the PHP one:
1
u/Intolerable Jul 16 '12
1
u/rickster88 Jul 17 '12
Python:
1
u/Krohnos Jul 17 '12
Java:
1
u/5outh 1 0 Jul 18 '12
My verbose Haskell solution that I don't like:
1
u/XeroxAlto Jul 23 '12
Python
1
u/[deleted] Jul 26 '12
Not the cleanest, but it works (Python).
1
u/taion809 Aug 01 '12
mine in php, if i can improve it lemme know :)
1
u/Lazyfaith Aug 08 '12
Powershell V2 'cus this is what I've been learning at my internship.
Any comments on it please?
1
u/swarage 0 0 Aug 25 '12
http://pastebin.com/AvNa3M0u
1
u/phoric Dec 26 '12 edited Dec 26 '12
Python