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!
Discussion
A bit like @nicolas Hervé, using the
reduce
method:[...a]
transforms the string to an array with every character (["l", "o", "v", "e"]
)reduce
methods will loop through every character, get its charCode, subtract96
to it (the charCode associated with"a"
+ 1), and sum everythingThanks to @ynndvn for showing me
reduce
method, it exist too in Swift language and it's awesome !!!This is my solution in Swift :
Why the
compactMap
, you are mapping each letter to itself, works fine without that too.Also when I tried your solution with the input string "love🐈" it returned -42 since the way you handle the
Optional
results in non-ASCII characters having a value of -96. The following version simply ignores such characters instead:Oh nice! That's better !
I forgot
String
are a table ofCharacter
so you rightcompactMap
is useless.I have totally ignored emojis because the challenger treat only letter
a
toz
So thank you for your feedback, your optimisation is truly better 👌
True, but you did add some code to deal with the case were a character may not have an
asciiValue
, I just offered an alternative to that :-)I started picking up ruby for rails, so here is my shining gem.
Try it out here
word.rb
word_test.rb
Just a friendly tip:
alphabet
really shouldn't be a class variable@@
, they are very seldom what you want in Ruby:railstips.org/blog/archives/2006/1...
In this specific case a constant would be the most common:
Ah okay gotcha, thank ya for the tip!
("a".."z").to_a is nifty!
Well, my first attempt had me iterating calling
.index
on("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... :)Ruby:
For fun a second version that builds a lookup table with an endless range (introduced in Ruby 2.6):
Here is the simple solution with Python:
A little solution for Ruby :) this was fun!
Nice to see some Prolog :-)
In PHP
EDIT:
Haskell
Playground
Here.
The prelude defines
subtract
, so you don't needflip
here:Another relatively common workaround is to use a right-section that adds a negative number, i.e.
Thanks for your insight. I didn't know about subtract and when the second solution is kind of obscure to me it seems very cool and short.
Since
-
is both subtraction and unary minus one can’t use a right-section like(-1)
, but considering that subtracting is the same as adding a negative number the slightly obscure form makes sense. I’d still usesubtract
though.My solution to the challenge using map and reduce in Python. If anyone has any suggestions on how to improve it, I would love to hear! :)
Here is my fun solution using Kotlin:
c#, one liner using linq, ignoring non-letters chars:
My Python Algorithm: