Incorrect, run my entry in an IDE, it works. Default Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
For some next context I’d be really interested in something like:
A method that takes two Strings and returns the smallest possible number of changes you need to turn the first String into the second. Possible changes would be:
- Adding a char
- Changing a char
- Removing a char
So compare("chllrnge", "challenge")
would return 2
(Add an ‘a’ and change ‘r’ to ‘e’)
or compare("muimecraf", "minecraft")
would return 3
(Remove an ‘u’ and change ‘m’ to ‘n’ and add an ‘t’)
What do you think? Could this be a good challenge?
Oh … damn xD Ok, that’s cool :o
It’s still a good golf challenge, just helps to have some reading.
I’d thought I would try a different approach,
public static int m(int[]a){int i=1;for(String m=".*( .+ ).*\\1.*";Arrays.toString(a).replaceAll("[\\[\\]\\, ]"," ").matches(m);m+="\\1.*")i++;return i;}
153 characters
I tried using regex to solve the challenge. This solution will work for any printable array as long as the Object’s toString() doesn’t contain any ‘[’, ‘]’ or ’ ’
public static int mode(int[] a){
int i = 1;
String s = Arrays.toString(a).replaceAll("[\\[\\]\\, ]"," ");
for(String m=".*( .+ ).*\\1.*";s.matches(m);m+="\\1.*")i++;
return i;
}
Here’s the one for String[]:
public static int mode(String[] a){
int i = 1;
String s = Arrays.toString(a).replaceAll("[\\[\\]\\, ]"," ");
for(String m=".*( .+ ).*\\1.*";s.matches(m);m+="\\1.*")i++;
return i;
}
It works by making a string with a space before and after each number then checks how many backreferences there are to the regex “( .+ )”