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.
Good thing the input isn't unknown then.
I'm responding to a comment on the actual answer, so I don't understand how I'm spoiling anything?
This comment is hidden because it contains spoiler information about the solution
It's a function using its single argument to create a new function, only to apply it to that same argument. You could rewrite it using do-notation as such:
I guess the tricky part is realizing that
head
will get applied to the same value thatxs
represents, similar to how in theIO
monad, you always write to the same enivironment you read from, and thanks to monads, this all happens implicitly. I generally think of monads as computations that can be composed/sequenced within a specific environment that they can interface with, without explicit references, a kind of meta-argument given at the start of the computation, in this case just a simpleString
.As for the
(. tail) . (:) . toUpper
part, it might be helpful to think of function composition as any other binary operator. Composition is defined as:So once it has an argument, it'll first get applied to function right of
.
, and its result gets applied to the function left of it. For simplicity, let's definecons = (:)
and rewrite the partially applied(. tail)
to(\f -> f . tail)
, then we can work out the following:So we have a function that capitalizes a
Char
, and "conses" it to the tail of aString
, and because of(head >>=)
, theChar
will materialize from the sameString
tail
will get applied to, giving us an overly complicated yet succint way to capitalize the first letter of a word.I'm not sure why I actually wrote it like that though, if I were to redo it, I'd use:
Which does the exact same thing, but for different reasons and without monads.
This comment is hidden because it contains spoiler information about the solution
I think you meant to refer to the "pigeonhole principle" instead of the birthday paradox.
A polynomial time solution to a linear time problem should never be considered "Best Practices"
It uses the mathematical understanding of a set, but you can think of it as a hashmap with only keys and no values, so unordered (can't use sort) and no duplicates.
Did you actually look at the code? They didn't use encode.
If I were to make a translation, I would include division by zero in the tests, and make sure it throws an exception, because that's the only acceptable result for it.
Saying it's zero is literally no different than saying 1 equals 2.
What the hell, this is straight up wrong.
1 / 0
should not return0
.You're not being smart for "handling" division by zero this way, because that would imply mathematicians, calculators, computers are dumb for not allowing it.
If
1 / 0 == 0
, then1 == 0 * 0
, which is obviously bullocks. And if2 / 0 == 0
, then2 == 0 * 0 == 1
, who wants to use a calculator that literally thinks1 == 2
?The distinct & sorted StringOps methods have been deprecated in Scala 2.13, which wouldn't be much of an issue, except that right now this solution prints itself to STDERR when hitting submit, allowing the users to view it before fully solving it themselves.
The Scala solution uses deprecated StringOps methods which get printed to STDERR whenever you hit submit, basically giving itself away to the user.
This comment is hidden because it contains spoiler information about the solution
I had heard that
foldr
can work with infinite lists, but I think this is the first time I've seen a practical application for that (as in it doesn't have to traverse the entire string to start comparing it to the empty one, like I initially thought).Loading more items...