Scrabble Stems Ruby Quiz question.
- From: Mark Woodward <markonlinux@xxxxxxxxxxxxxxxx>
- Date: Sun, 2 Dec 2007 13:48:43 +1100
Hi all,
could someone explain the line:
(STEMS[stem] ||= {})[letter] = 1
in the code below?
This is a solution (Carlos) from the Scrabble Stems Ruby quiz.
---------------------------------------------------------------
DICT = "/usr/share/dict/words"
CUTOFF = ARGV[0].to_i
STEMS = {}
File.open(DICT) do |f|
f.each do |word|
word.chomp!
next if word.length != 7
word.downcase!
letters = word.split(//).sort!
uniques = letters.uniq
word = letters.join
uniques.each do |letter|
stem = word.sub(letter, "")
(STEMS[stem] ||= {})[letter] = 1
end
end
end
result = STEMS.delete_if { |k,v| v.size < CUTOFF }.
sort_by { |k,v| v.size }.
reverse!.
collect! { |k,v| [k, v.size] }
result.each do |stem, combining| puts "#{stem} #{combining}" end
---------------------------------------------------------------
cheers,
--
Mark
.
- Follow-Ups:
- Re: Scrabble Stems Ruby Quiz question.
- From: yermej
- Re: Scrabble Stems Ruby Quiz question.
- Prev by Date: Re: Why are "Array#push" and "pop" not "push!" and "pop!"?
- Next by Date: Re: Identifying a volume as being an iPod
- Previous by thread: irb and unix shells
- Next by thread: Re: Scrabble Stems Ruby Quiz question.
- Index(es):