[QUIZ][SOLUTION] Scrabble Stems (#12)

Discussion in 'Ruby' started by Martin DeMello, Dec 19, 2004.

  1. Here's my solution. I used the sowpods wordlist available here:
    http://www.isc.ro/commands/lists.html (preprocessed to be all-lowercase,
    though it doesn't really matter as long as the case is consistent).

    It takes entirely too long to run (12 seconds on my P4/3GHz); I'll poke
    at it some more if I get the time and see what's slowing it down.

    --------------------------------------------------------------------------
    $stems = {}
    $seen = {}
    $cutoff = ARGV[0].to_i

    IO.foreach("sowpods.txt") {|word|
    next if word.length != 8
    word.chomp!
    letters = word.split(//).sort
    alpha = letters.join
    next if $seen[alpha]
    $seen[alpha] = true
    remove = letters.uniq
    remove.each {|i|
    stem = alpha.sub(i, '')
    $stems[stem] ||= {}
    $stems[stem] = true
    }
    }

    $stem_length = {}
    $stems.each {|k,v| $stems[k] = v.keys.sort}
    $stems.reject! {|k,v|
    n = v.length
    $stem_length[k] = n
    n < $cutoff
    }

    results = $stems.keys.sort_by {|i| -$stem_length}
    results.each {|s| p [s, $stem_length, $stems.join]}
    Martin DeMello, Dec 19, 2004
    #1
    1. Advertising

  2. Here's mine ... doesn't seem to fair too badly. Using the YAWL list
    (264,061 words), it finds 25 bingo-stems that combine with n >= 20 letters.
    (about 16 seconds on a 666MHz).

    regards,
    andrew

    #!/usr/bin/ruby -w

    n = ARGV[0].to_i
    wfile = ARGV[1]

    w7sigs = {}
    w6sigs = {}

    File.open(wfile,'r') do |f|
    f.each do |line|
    next unless line.size == 8
    line.chomp!
    line.downcase!
    sig = line.unpack('aaaaaaa').sort.join("")
    next if w7sigs[sig]
    w7sigs[sig] = 1
    end
    end

    w7sigs.each_key do |k|
    7.times do |i|
    ns = k.dup
    ll = ns.slice!(i,1)
    w6sigs[ns] ||= []
    w6sigs[ns] << ll
    end
    end

    w6sigs.each {|k,v| w6sigs[k].uniq!}
    w6sigs.reject!{|k,v|v.size < n}
    w6sigs.sort_by{|a|a[1].size}.reverse.each do |it|
    puts "#{it[0]} #{it[1].size}"
    end
    __END__
    Andrew Johnson, Dec 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruby Quiz

    [QUIZ] Scrabble Stems (#12)

    Ruby Quiz, Dec 17, 2004, in forum: Ruby
    Replies:
    16
    Views:
    247
    James Edward Gray II
    Dec 20, 2004
  2. Ruby Quiz

    [SUMMARY] Scrabble Stems (#12)

    Ruby Quiz, Dec 23, 2004, in forum: Ruby
    Replies:
    0
    Views:
    95
    Ruby Quiz
    Dec 23, 2004
  3. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    16
    Views:
    268
    David Tran
    Jan 10, 2005
  4. David Tran
    Replies:
    9
    Views:
    173
    David Tran
    Jan 21, 2005
  5. MenTaLguY
    Replies:
    12
    Views:
    272
    MenTaLguY
    Jun 7, 2007
Loading...

Share This Page