[Week Eight] Programming Contest - Array Modes

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:

  1. Adding a char
  2. Changing a char
  3. 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? :smiley:

Oh … damn xD Ok, that’s cool :o

It’s still a good golf challenge, just helps to have some reading.

1 Like

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 “( .+ )”

Congratulations to @jus1in for winning this weeks programming challenge, and another honorable mention to @eah

The next one will be up shortly, it will be @Blue’s suggestion. For future ideas please suggest them on the main thread!

1 Like