Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This expression calculates the position of the alphabet by subtracting 96 from its Unicode code point. Since lowercase 'a' has a Unicode code point of 97, subtracting 96 gives 1, which represents the position of 'a'. Similarly, 'b' would be 2, 'c' would be 3, and so forth.
can someone explain me why -96 please?
The problem asked to use the filter function. How did this code past the "filter in code" test?
str
turns the integer into a string. It's the difference between the number55
and the string"55"
. To check whether the string"5"
is within the number, you first have to convert the number to a string by usingstr
.This comment is hidden because it contains spoiler information about the solution
That's why this kata is easy, and this one is not: https://www.codewars.com/kata/621f89cc94d4e3001bb99ef4
exactly what I thought.
How can you solve it without creating an array?
In isolation using
96
is fine, but structuring it the way Kosmarik proposed is more extensible in the future (for example, if you wanted to extend the function to return a character's position relative to any other character, it'd be easier to implement that by just replacing'a'
with a function argument).why would it be better? Every dev knows the ascii table and where to look it up in seconds and everyone who reads the solution instantly knows why to subtract exactly 96.
Your solution is much worse to read
You can do alphabet.lower()
doesn't work for uppercase.
I did this to practice using reverse (which I always confuse with reversed) to help myself differentiate them, although list slicing would have been simpler
This comment is hidden because it contains spoiler information about the solution
Better than mine.