Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:26:34+00:00 2026-06-11T20:26:34+00:00

I’ve been having a play with Ruby recently and I’ve just completed the Anagrams

  • 0

I’ve been having a play with Ruby recently and I’ve just completed the Anagrams Code Kata from http://codekata.pragprog.com.

The solution was test driven and utilises the unique prime factorisation theorem, however it seems to run incredibly slow. Just on the 45k file it’s been running for about 10 minutes so far. Can anyone give me any pointers on improving the performance of my code?

class AnagramFinder
def initialize
    @words = self.LoadWordsFromFile("dict45k.txt")
end

def OutputAnagrams
    hash = self.CalculatePrimeValueHash

    @words.each_index{|i|
        word = @words[i]
        wordvalue = hash[i]
        matches = hash.select{|key,value| value == wordvalue}
        if(matches.length > 1)
            puts("--------------")
            matches.each{|key,value|
                puts(@words[key])
            }
        end         
    }

end

def CalculatePrimeValueHash     
    hash = Hash.new
    @words.each_index{|i|
        word = @words[i]
        value = self.CalculatePrimeWordValue(word)
        hash[i] = value
    }

    hash
end

def CalculatePrimeWordValue(word)
    total = 1
    hash = self.GetPrimeAlphabetHash
    word.downcase.each_char {|c|
        value = hash[c]
        total = total * value
    }
    total
end

def LoadWordsFromFile(filename)

    contentsArray = []
    f = File.open(filename)

    f.each_line {|line|
        line = line.gsub(/[^a-z]/i, '')
        contentsArray.push line
    }

    contentsArray
end

def GetPrimeAlphabetHash
    hash = { "a" => 2, "b" => 3, "c" => 5, "d" => 7, "e" => 11, "f" => 13, "g" =>17, "h" =>19, "i" => 23, "j" => 29, "k" => 31, "l" => 37, "m" => 41, "n" =>43, "o" =>47, "p" => 53, "q" =>59, "r" => 61, "s" => 67, "t" => 71, "u" => 73, "v" => 79, "w" => 83, "x" => 89, "y" => 97, "z" => 101 }
end 
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:26:35+00:00Added an answer on June 11, 2026 at 8:26 pm

    Frederick Cheung has a few good points, but I thought I might provide you with a few descriptive examples.

    I think your main problem is that you create your index in a way that forces you to do linear searches in it.

    Your word list (@words) seems to look something like this:

    [
      "ink",
      "foo",
      "kin"
    ]
    

    That is, it is just an array of words.

    Then you create your hash index with CalculatePrimeValueHash, with hash keys being equal to the word’s index in @words.

    {
      0 => 30659, # 23 * 43 * 31, matching "ink"
      1 => 28717, # 13 * 47 * 47, matching "foo"
      2 => 30659  # 31 * 23 * 43, matching "kin"
    }
    

    I would consider this a good start, but the thing is if you keep it like this, you will have to iterate through the hash to find what hash keys (i.e. indexes in @words) that belong together, and then iterate through those to join them. That is, the basic problem here is that you do things too granularly.

    If you instead were to build this hash with the prime values as hash keys, and have them point to an array of the words with that key, you would get a hash index like this instead:

    {
      30659 => ["ink", "kin"],
      28717 => ["foo"]
    }
    

    With this kind of structure, the only thing you have to do to write your output, is to just iterate over the hash values and print them, since they are already grouped.

    Another thing with your code, is that it seems to generate a whole bunch of throwaway objects , which will make sure to keep your garbarge collector busy, and that is generally quite a big choke point in ruby.

    It might also be a good thing to go find either a benchmark tool and/or a profiler to analyze your code and see where it could be approved upon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.