5ce36f73f075f18632c284f56cedd23d

Some code I've written for a challenge, I'm submitting it to learn some refactoring, see how it can be done, so please submit many different solutions!

brew = "rdadadroxmocpronogomocdmqzx"
alphabet = "abcdefghijklmnopqrstuvwxyz"
word = ""

# create an array for each letter
lettercount = {}
alphabet.split(//).each do |letter|
  lettercount[letter] = 0
end

# count the incidence of each letter
brew.split(//).each do |letter|
  if (lettercount[letter] += 1) == 3 then
    word += letter
    lettercount[letter] = 0
  end
end

# so every third letter gets printed, 
# the outcome is 'droom', which is
# Dutch for 'dream'

print word + "\n"

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

December 23, 2008, December 23, 2008 16:37, permalink

2 ratings. Login to rate!
class Word < Struct.new(:brew)
  def to_s
    count = Hash.new(0)
    brew.split(//).inject('') do |word,character|
      word << character if (count[character] += 1) % 3 == 0
      word
    end
  end
end

puts Word.new('rdadadroxmocpronogomocdmqzx')
5ce36f73f075f18632c284f56cedd23d

redpidgin

December 23, 2008, December 23, 2008 16:44, permalink

No rating. Login to rate!

thank you very much, works great. how does it work from brew.split? in other word, how do the letters stay in the right order, since they just get counted on line 5

D7a31f22c11776898826f7c1ed0ff80a

mischamolhoek.myopenid.com

December 23, 2008, December 23, 2008 17:37, permalink

No rating. Login to rate!

if you like oneliners.... :) (but i would not call this clean)

require 'enumerable'
brew = "rdadadroxmocpronogomocdmqzx"
brew = brew.split(//);
puts brew.sort.uniq.inject({}){|h,n| h[n]= brew.map_with_index{|b,i|i if b==n}.compact; h}.reject{|a,b|b.size < 3}.map{|a,b| b.select{|n| (b.index(n)+1)%3==0}.map{|c| [c,a]} }.sort_by{|n|n[0][0]}.flatten.select{|n| n.is_a?(String)}.join
A8d3f35baafdaea851914b17dae9e1fc

Adam

December 23, 2008, December 23, 2008 17:57, permalink

No rating. Login to rate!

@redpidgin - I'm not sure what you mean about the right order? The code does the same thing as your original version.
@mischamolhoek - that breaks when you change the brew.

Be072eb0e9f6f81c20541150cabce3ab

Ollie

December 23, 2008, December 23, 2008 22:55, permalink

No rating. Login to rate!

I couldn't do any better than Adam's.

D7a31f22c11776898826f7c1ed0ff80a

mischamolhoek.myopenid.com

December 24, 2008, December 24, 2008 08:02, permalink

No rating. Login to rate!

@adam - your right, it was a joke anyway :).
I like your code so much better, yours is a seriously clean solution. bravo! :)

@redpidgin - lookup how inject works, you'll get it :)

puts Word.new('rdapepa epdemrtrfx,feyefryeemcprt,cn ct, zaaaxdam')
D41d8cd98f00b204e9800998ecf8427e

bob

December 26, 2008, December 26, 2008 08:30, permalink

No rating. Login to rate!
class Word < Struct.new(:brew)
  def to_s
    count = Hash.new(0)
    brew.split(//).select do |character|
      (count[character] += 1) % 3 == 0
    end.join
  end
end

puts Word.new('rdadadroxmocpronogomocdmqzx')

Your refactoring





Format Copy from initial code

or Cancel