Programming Riddles

Two words are anagrams if and only if they contain the exact same letters with the exact same frequency (for example, “name” and “mean” are anagrams, but “red” and “deer” are not).
Given two strings S1 and S2, which each only contain the lowercase letters a through z, write a program to determine if S1 and S2 are anagrams.

Rule: You can use any language you want. Best algorithm will win.

Re: Programming Riddles

.

formatting gone for a tosss…let me see how I can reformat it

Re: Programming Riddles



one
 two
  three
    four




use


 tag i.e.  code] / code] like this ... remove spaces

Re: Programming Riddles

Ok I wrote this small function to find anagrams . Anyone with better algo ?



private static bool IsAnagrams(string one, string two)
        {
            Dictionary<char, int> dictionaryOne = new Dictionary<char, int>();
            Dictionary<char, int> dictionaryTwo = new Dictionary<char, int>();


            if (one.Length != two.Length) return false;


            foreach (var c in one)
            {
                dictionaryOne[c] = dictionaryOne.ContainsKey(c) ? dictionaryOne[c] + 1 : 1;
            }


            foreach (var c in two)
            {
                dictionaryTwo[c] = dictionaryTwo.ContainsKey(c) ? dictionaryTwo[c] + 1 : 1;
            }


            foreach (var item in dictionaryOne)
            {
                if (item.Value != dictionaryTwo[item.Key]) return false;
            }


            return true;
        }


Re: Programming Riddles

umm :konfused:

Re: Programming Riddles

Heck , am I the only geek here . Where are all the programming enthusiasts ?

Re: Programming Riddles






Module modAnagram
    Public Sub Main(ByVal cmdArgs() As String)

        'sort the strings to arrange the chars
        Dim s1, S2 As String
        s1 = SortString(cmdArgs(0))
        S2 = SortString(cmdArgs(1))
        Dim bAnagramTrue As Boolean
        bAnagramTrue = True
        If s1.Length = S2.Length Then
            For nCounter As Integer = 0 To s1.Length - 1
                If s1.Substring(nCounter, 1) <> S2.Substring(nCounter, 1) Then
                    bAnagramTrue = False
                    nCounter = s1.Length
                End If
            Next
        Else
            bAnagramTrue = False
        End If
        If bAnagramTrue Then
            Console.WriteLine("The words " + cmdArgs(1) + " & " + cmdArgs(2)+ " are anagrams")
        Else
            Console.WriteLine("The words " + cmdArgs(1) + " & " + cmdArgs(2) + " are not anagrams")
        End If
    End Sub

    Function SortString(S1 As String) As String
        Dim chrArray() As Char
        chrArray = S1.ToCharArray()
        Array.Sort(chrArray)
        'Console.WriteLine(New String(chrArray))
        SortString = New String(chrArray)
    End Function

End Module





Re: Programming Riddles

My only suggest would be to pass in two character arrays to SortString by reference and then after sorting use them in your main loop for comparison. This way you won’t have to convert them to string again in SortString function and second you won’t have to use Substring in the main method. It will double the performance of this algorithm.

Re: Programming Riddles

I forgot EVERYTHING I learnt 6 yrs back :eek:

Re: Programming Riddles

I am a learner. To be honest, I don’t know these. :bummer:

Re: Programming Riddles

LP, if you are still working in the field of IT, I am sure you must remember?

Re: Programming Riddles

Nopes…never worked in IT…
I learnt C++ around 10-11 yrs back & I learnt C about 6 yrs back.After that I closed the books & never opened them again :bummer:

Re: Programming Riddles

Anagram Pseudocode

IsAnagram(string s1,string s2){
if(s1.size!=s2.size)
return not anagram;
else
{
int counter1[26]={0,0,0…};
int counter2[26]={0,0,0…};

foreach character c in s1
counter1[ascii value of c -97]++;
foreach character c in s2
counter2[ascii value of c -97]++;

for(int i=0;i<26;i++)
if(counter2*!=counter1*)
return not anagram;

return anagram;
}
}

Re: Programming Riddles

Great jobs guys for your effort . Thanks for participating in it . Now I have another one.

Write a program to print a diamond shape. You should use (*) to print this shape. The max shape width will be passed in by the user.

Re: Programming Riddles

Ok I gave it a shot to print a diamond.



static void PrintDiamond(int maxSize)
        {
            maxSize = maxSize % 2 == 0 ? maxSize : maxSize + 1;
            maxSize = maxSize / 2;


            PrintSpace(maxSize); PrintStar(1); Console.WriteLine();


            for (int i = 1; i <= maxSize; i++)
            {
                PrintSpace(maxSize - i); PrintStar(i + 1);
                PrintStar(i);
                Console.WriteLine();
            }


            for (int i = maxSize - 1; i >= 1; i--)
            {
                PrintSpace(maxSize - i); PrintStar(i + 1);
                PrintStar(i);
                Console.WriteLine();
            }


            PrintSpace(maxSize); PrintStar(1); Console.WriteLine();
        }


and it will print a diamond like this



        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
 ***************
  *************
   ***********
    *********
     *******
      *****
       ***
        *


Re: Programming Riddles

which programming language is this code in?

Re: Programming Riddles

C#

Re: Programming Riddles

ok I am going to write a program which erase it self.



// start here 7.58 monday 2013
//this is a free software and it comes
//with absolute no guarantee. 

//pre processors 



//main body




//loop





//tada



Re: Programming Riddles

see it worked other then comments it erased it self. Very secure program. :cool:

Re: Programming Riddles

yo programming genuises, help me find a tool that converts PDF to WORD doc,
conditions: No online tools, need something to be downloaded on my machine. Paid for tools are welcome. Crackable versions even better… Torrent links, direct download links, all cool.