r/dailyprogrammer • u/rya11111 3 1 • May 14 '12
[5/14/2012] Challenge #52 [easy]
Imagine each letter and its position within the alphabet. Now assign each letter its corresponding value ie a=1, b=2,... z=26. When given a list of words, order the words by the sum of the values of the letters in their names.
Example: Shoe and Hat
Hat: 8+1+20 = 29
Shoe: 19+8+15+5 = 47
So the order would be Hat, Shoe.
For extra points, divide by the sum by the number of letters in that word and then rank them.
thanks to SpontaneousHam for the challenge at /r/dailyprogrammer_ideas .. link
Please note that [difficult] challenge has been changed since it was already asked
http://www.reddit.com/r/dailyprogrammer/comments/tmnfn/5142012_challenge_52_difficult/
fortunately, someone informed it very early :)
4
u/SpontaneousHam 0 0 May 14 '12
Hey, my challenge!
3
u/jonzo1 0 0 May 14 '12
Clojure:
3
u/ret0 May 14 '12
Python3:
2
u/luxgladius 0 0 May 14 '12
Perl
Output
2
u/loonybean 0 0 May 14 '12 edited May 14 '12
Javascript:
The answer to the bonus question is hardly any different:
1
u/drb226 0 0 May 14 '12
1
u/[deleted] May 14 '12
1
u/ret0 May 14 '12
C:
Compile with:
Run with:
1
u/bh3 May 14 '12
Python:
1
u/StorkBaby 0 0 May 14 '12 edited May 14 '12
Python, probably too much code here. edit: space before the code.
1
u/atheistscansuckit May 14 '12
One liner(each) in python. Wouldn't be hard to take the list from a file and keep it a one liner
1
u/emcoffey3 0 0 May 14 '12
Using C# and LINQ:
1
u/[deleted] May 14 '12
C. Solves the bonus question if
BONUS_QUESTIONis defined when compiling, using a preprocessor trick.1
u/Rapptz 0 0 May 15 '12 edited May 15 '12
1
u/totallygeek May 15 '12 edited May 15 '12
Edit Put in the code for extra points
Bash:
Output(s):
1
u/HazzyPls 0 0 May 15 '12
C
Not sure I add anything that hasn't been done before. Tis the nature of "easy" challenges, isn't it?
I briefly considered caching results from letter_sum, since it's called 53 times in my code. But I doubt you could measure a speed increase.
1
u/Medicalizawhat May 15 '12
Shitty Ruby:
1
u/crawphish May 15 '12
Python:
This is my first time using python (i started learning last thursday), feedback would be appreciated :)
1
u/[deleted] May 15 '12
Scala one liner. I was rather impressed at getting it so small.
1
u/robin-gvx 0 2 May 15 '12
Déjà Vu:
1
u/robin-gvx 0 2 May 15 '12
Déjà Vu:
1
u/TweenageDream May 15 '12
Ruby:
1
u/lsv20 May 15 '12
PHP
Usage
php Challenge #52 [easy].php" hat shoe sexy tornado php
Result
Word: tornado Sum: 20 + 15 + 18 + 14 + 1 + 4 + 15 = 87
Word: sexy Sum: 19 + 5 + 24 + 25 = 73
Word: shoe Sum: 19 + 8 + 15 + 5 = 47
Word: php Sum: 16 + 8 + 16 = 40
Word: hat Sum: 8 + 1 + 20 = 29
1
u/itsCarraldo May 15 '12
Java:
1
u/joeyGibson May 16 '12
Here's my Clojure solution:
And the results:
1
u/SwimmingPastaDevil 0 0 May 16 '12 edited May 16 '12
This was interesting. Learnt about the
ordfunction today and used that.Bonus:
Edit: added bonus part.
1
u/JerMenKoO 0 0 May 16 '12
Python:
1
u/bubinhead May 16 '12
I'm new to Haskell.
1
u/dinosaur_porkchop 0 0 Jul 31 '12 edited Aug 01 '12
PHP:
Output:
29, 47
1
u/[deleted] Oct 06 '12 edited Oct 06 '12
JavaScript (evil eval)
ch52("Hat") + ch52("Shoe"); // outputs 76
1
u/RaihanHA Dec 05 '21
this prolly the oldest post i’ve ever seen on reddit
0
u/Sturmi12 May 14 '12
C:
I reused a self-sorting list which I made for an assignment. So the sorting part was already done.