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 solution does not honour "If there are less than n occurrences of the substring, return -1". It will fail e.g. for
FindOccurrence.findNthOccurrence("a", "aaa", 5)
where it returns0
instead of-1
.This comment is hidden because it contains spoiler information about the solution
If you want to go functional - do it the whole way. The loop and the mutable result array are very ugly...
I would not call this "best practice" at all.
Looks like a solution you'd see in Haskell, JS or Ruby with the use of map and filter. I don't usually think of Java as functional, but I'm obviously wrong. Very nice.
The algorithmic complexity of the code is atleast linear, but the required complexity is lower than linear.
This comment is hidden because it contains spoiler information about the solution
Why O(n) doesn't fast enough?
For most katas, may be. But there are katas with certain demands to the code itself. Maybe I just overthought it. Anyway - there is a mistake in the placeholder.
UPD: I managed to put the solution between 'return string' and ';', so it is possible to solve without changing the placeholder, thus the task is correct, but a bit tricky.
Oh, the code you see in "Initial Solution" box is meant to be completely replaced by your solution. The fact that it returns the same reference it accepts does not mean that you have to do it this way, it's just an example and placeholder. You can do it any way which passes tests, and as you noticed, in Java you cannot mutate the original string. You have to create new one.
This:
class Kata {
static String alternateCase(final String string) {
return string;
}
}
UPD: However, I've made some research and learned something from this, so it's not in vain.
You are right, you cannot mutate a string in Java. This kata cannot be solved by mutating the string neither in Java, nor in many other languages, but it can in some others
Which part of the task makes you think that this have to be exactly the solution here?
Java
Given is the final String and the same String to return. So, accoring to the task, we have to mutate given string.
I didn't find the way how to do that and looked at the solutions. All of them return another Strings. And they are considered done!
It's so frustrating.
Question to the author: How to mutate final String in Java if Strings are initially immutable?
I forgot to delete the row reverseRoute[0] = "Begin " + arr[arr.length-1].split(" ", 2)[1]; so I initialized reverseRoute[0] twice. I'm not sure whether there is an option to edit solution or only add an alternative.
This comment is hidden because it contains spoiler information about the solution
I just tried your code in a fork and never had any problems, though... :o (apart from the fact that I don't know how you did submit this version since it doesn't compile (unsafe opeartion because you didn't decalre the type the list is holding) x)
Loading more items...