Thursday, December 4, 2008

DevCore's Summer Of Code - Competition!!! Competition!! Competition!!


Anagrams
are words that contain the same letters, not necessarily in the same order.

For example, "loop", "pool", and "polo" are all anagrams of each other, because each contains one "l", two "o"s,
and one "p".

Any word is considered to be an anagram of itself.

The task at hand is to come up with code in a language of your own choice that tests whether two strings are anagrams of each other.

The rules of the game are as follows:

1) Your source code must consist of only a function definition (in the case of procedural languages), or a method definition (in the case of object oriented languages) in a form that may be similar to the template to be given below.
2) the function or method must only take two parameters or arguments of data type string, or two string arrays, or pointers to string arrays.
3) It must return a boolean or equivalent data type that represents a true of false state meaning that the two strings passed are anagrams of each other or not.
4) The body of the method can contain any number of lines of code, but the definition of the method must be overally be similar to the following

public boolean areAnagramsOfEachOther(String string1,String string2){
//Method body

//
}

The GOAL of this competition is to be able to come up with an ALGORITHM that will stand out not only as the best, but also the most OPTIMIZED code which will be qualified by such attributes as:
a) Minimal code redundancy and repetition
b) Fewer lines of code
c) Execution speed and usage of resources ie. The code that will essentially execute faster and use less resources for it's execution.
d) The code that has less memory leaks if any, or any hidden bugs.

The WINNER gets the exclusive right to be PROMOTED to be an ADMINISTRATOR, with non-restrictive permissions to our BLOG DEVCORE.BLOGSPOT.COM.

C'mon guys, I know we all got pesky work to do, but this is a refreshing challenge.


8 comments:

  1. 1st attempt in java...

    boolean areAnagrams(String a, String b){
      if (a == null || b == null || a.length() != b.length()){
        return false;
      }
      for (int cnt = 0; cnt < a.length();cnt++){
        if (b.indexOf(a.charAt(cnt)) < 0){ //letter at a[cnt] not found
          return false;
        } else {
          b.replaceFirst(Character.toString(a.charAt(cnt)), "\0");
        }
      }
      return true;
    }

    something tells me it can be done in one line using Perl regexp. Maybe i'l give it a go later on

    ReplyDelete
  2. 1st Attempt Python ......

    def areAnagramsOfEachOther(string1,string2):
    a = sorted(string1.lower())
    b = sorted(string2.lower())
    return (a == b)


    you can call it as below:

    if(areAnagramsOfEachOther("one","two")):
    print "anagrams"
    else:
    print "they aint"

    THE BEAUTY OF PYTHON

    ReplyDelete
  3. 2nd Attempt in Perl....

    sub areAnagramsOfEachOther{
    $string1 = sort split('',$_[0]);
    $string2 = sort split('',$_[1]);
    for (my $i = 0; $i < @$string1; $i++) {
    return 0 if $string1->[$i] ne $string2->[$i];
    }
    return 1;
    }


    I guess it wud be shorter with regexp but ugly

    ReplyDelete
  4. c#
    if ((string1 == null || string2 == null) || (string1.Length != string2.Length) || (string1.Length==0))
    return false;
    string1 = string1.ToUpper();
    string2 = string2.ToUpper();
    foreach (char c in string2)
    if (!string1.Contains(c))
    {
    return false;
    }
    return true;

    ReplyDelete
  5. Where are the others? Ray? Mateks? Dunfunk?...
    Where are your contributions?

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. using cha[] in java

    public static boolean areAnagrams(char[] first,char[] second){
    boolean z= false;
    int x=0; int y=0;
    while(y < second.length & x < first.length){
    if(first[x]==second[y]){
    second[y]=0;
    x++; y=0; z=true;
    }
    else{
    y++; z=false;
    }
    }
    return z;
    }
    }

    ReplyDelete
  8. hmm. Dirk, what would your algorithm say about "polo" & "poll"?? I don't have a C# environment but a dry-run leads me to suspect the method would say the words are anagrams. infact, string1="polo" and string2="pppp" would (incorectly) pass as anagrams as well!

    ReplyDelete