Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: [QUIZ] Count and Say (#138
James Koppel
9/8/2007 9:52:00 PM
This week's quiz was fairly easy. The hardest part was finding and choosing a way to turn the numbers into their English equivalents. Initially I used an array that extended to TWENTY, but that was insufficient. I had solved Ruby Quiz #25 and some of the other old ones myself a while ago, but lost it due to a harddrive crash and couldn't find a copy. I then just picked someone else's solution to Ruby Quiz #25 that added a to_en method to Integer.Here's my solution, minus the Integer#to_en method. I commented out the code to print out the elements of the cycle, as it turns out the cycle is about 430 elements long!def count_and_say(str) ('A'..'Z').map{|l| (str.count(l) > 0) ? [str.count(l).to_en.upcase, l] : ""}.join(' ').squeeze(' ')endorder = ARGV[0].chomp.to_iprev_results = {}element = "LOOK AND SAY"for n in (0..order) if prev_results[element] puts "Cycle of length #{n-prev_results[element]} starting" + " at element #{prev_results[element]}" #puts "Cycle's elements are:" #puts (prev_results[element]...n).to_a.map{|n| prev_results.invert[n]} break else prev_results[element] = n end element = count_and_say(element)end----- Original Message ----From: Ruby Quiz <james@grayproductions.net>To: ruby-talk ML <ruby-talk@ruby-lang.org>Sent: Thursday, September 6, 2007 7:00:20 AMSubject: [QUIZ] Count and Say (#138)The three rules of Ruby Quiz:1. Please do not post any solutions or spoiler discussion for this quiz until48 hours have passed from the time on this message.2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.ruby...
. Enjoy!Suggestion: A [QUIZ] in the subject of emails about the problem helps everyoneon Ruby Talk follow the discussion. Please reply to the original quiz message,if you can.-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=by Martin DeMelloConway's "Look and Say" sequence(
http://en.wikipedia.org/wiki/Look-and-sa...
) is a sequence of numbers inwhich each term "reads aloud" the digits of the previous term. For instance, thecanonical L&S sequence starts off 1, 11, 21, 1211, 111221, ..., because: * 1 is read off as "one 1" or 11. * 11 is read off as "two 1's" or 21. * 21 is read off as "one 2, then one 1" or 1211. * 1211 is read off as "one 1, then one 2, then two 1's" or 111221. * 111221 is read off as "three 1, then two 2, then one 1" or 312211.Over on recpuzzles, Eric A. proposed a variant in which the letters of asentence are grouped, then "counted aloud", omitting the "s"s for the pluralform. Thus, seeding the sequence with "LOOK AND SAY", we get: 0. LOOK AND SAY 1. TWO A ONE D ONE K ONE L ONE N TWO O ONE S ONE Y 2. ONE A ONE D SIX E ONE K ONE L SEVEN N NINE O ONE S TWO T TWO W ONE Y 3. ONE A ONE D TEN E TWO I ONE K ONE L TEN N NINE O THREE S THREE T ONE V THREE W ONE X ONE Yand so on. (Note the difference between this and the L&S sequence--the lettersare counted rather than read in order). Eric wants to know when the sequenceenters a cycle, and how long that cycle is. Well? ____________________________________________________________________________________Sick sense of humor? Visit Yahoo! TV's Comedy with an Edge to see what's on, when.
http://tv.yahoo.com/colle...
1 Answer
Brad Ediger
9/8/2007 10:20:00 PM
0
Here's my solution. Coincidentally, it uses the same
Integer#to_english method that JEG2 posted from quiz 25 (not
included, as integer_to_english.rb).
--Usage:
$ ./138_count_and_say.rb LOOK AND SAY
Took 179 cycles to enter a cycle of length 429
--
#!/usr/bin/env ruby -rubygems
# integer_to_english.rb ->
http://blade.nagaokaut.ac.j...
scat.rb/ruby/ruby-talk/135449
%w(facet/string/chars facet/enumerable/injecting facet/symbol/to_proc
integer_to_english).each(&method(:require))
class String
def letter_histogram
upcase.gsub(/[^A-Z]/,'').chars.injecting(Hash.new(0)){|h, l| h
[l] += 1}
end
def count_and_say
letter_histogram.sort_by{|l,n| l}.map{|(l, n)| "#
{n.to_english.upcase} #{l}"}.join(" ")
end
end
class Object
def detect_cycles
ary = [self]
loop do
val = yield(ary.last)
if ary.include? val
return [ary.index(val)+1, ary.length - ary.index(val)]
end
ary << val
end
end
end
if __FILE__ == $PROGRAM_NAME
tail, cycle_length = ARGV.join.detect_cycles(&:count_and_say)
puts "Took #{tail} cycles to enter a cycle of length #{cycle_length}"
end
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: [QUIZ] Count and Say (#138
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password