If a = 1, b = 2, c = 3 ... z = 26
Then l + o + v + e = 54
and f + r + i + e + n + d + s + h + i + p = 108
So friendship is twice as strong as love :-)
Write a function to find the "strength" of each of these values.
Test cases:
wordsToMarks(attitude)
wordsToMarks(friends)
wordsToMarks(family)
wordsToMarks(selflessness)
wordsToMarks(knowledge)
Good luck!
This challenge comes from J or nor J on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (20)
A bit like @nicolas Hervé, using the
reducemethod:[...a]transforms the string to an array with every character (["l", "o", "v", "e"])reducemethods will loop through every character, get its charCode, subtract96to it (the charCode associated with"a"+ 1), and sum everythingThanks to @ynndvn for showing me
reducemethod, it exist too in Swift language and it's awesome !!!This is my solution in Swift :
I started picking up ruby for rails, so here is my shining gem.
Try it out here
word.rb
word_test.rb
("a".."z").to_a is nifty!
Well, my first attempt had me iterating calling
.indexon("a".."z")only to find out that you can't call index on a range, so in trying to work around that I realized I could just convert the range into an array... :)Here is the simple solution with Python:
A little solution for Ruby :) this was fun!
Oh nice! That's better !
I forgot
Stringare a table ofCharacterso you rightcompactMapis useless.I have totally ignored emojis because the challenger treat only letter
atozSo thank you for your feedback, your optimisation is truly better 👌