From: Rick DeNatale Date: 2008-01-06T12:06:41+09:00 Subject: Re: Chain, Chain, Chain (was Re: zed shaw zed shaw zed shaw) On Jan 5, 2008 5:05 PM, Joel VanderWerf wrote: > Rick DeNatale wrote: > > Okay, I've been playing with doing this programatically. I'm not yet > > convinced that it's bug free, but I've found some rather long movie > > title chains using the non-imdb list posted earlier: > > > > 10 ITEMS OR THAN FOR AND OF > > > WILL OF THUNDER > > Why didn't it find Thunder Road[1]? (And then maybe Road Trip to > Bountiful...) Well first of all, because thunder road isn't on that list. Second, because it's the longest title the program found in the time I gave to run it. As I said the program isn't of the quality that I'd prefer to share, but what the heck. It's a pretty dumb search, and I have it print a chain only the first time it finds one longer (in terms of the number of movies) than any it had found before. It's full of "the simplest thing that could possibly work" decisions with the idea of making it work before trying to make it fast. require 'net/http' class Title @first_words = Hash.new {|h,k| h[k] = []} def self.register(title) @first_words[title.first_word] << title if title.first_word end def self.process_lines(string) string.each_line do |line| self.new(line) end end def self.generate_titles result = [] @first_words.keys.sort.each do |fw| all_starting_with(fw).each do |title| title.chains do | chain | result << chain end end end end def self.all_starting_with(word) @first_words[word] end def print_chain(chain) puts "#{chain.length}: #{chain.inject(chain.first.first_word) { |chained_title, title| title.chain_to(chained_title)}}" end def chain_to(string) "#{string.sub(/\s(\S+)$/,'<\1>')} #{@words[1...@words.length].join(' ')}" end def self.max_chain?(chain) @max ||= 0 if chain.length > @max @max = chain.length true else false end end def chains(chain = []) root_chain = chain << self raise "Duplicate title #{print_chain(root_chain)}" unless root_chain.length == root_chain.uniq.length print_chain(root_chain) if self.class.max_chain?(root_chain) result = [] self.class.all_starting_with(self.last_word).each do |title| unless root_chain.include?(title) title.chains(root_chain.dup).each do |chain| result << chain end end end result end def initialize(title_string) @words = title_string.strip.split self.class.register(self) end def first_word @words.first end def last_word @words.last end def to_s @words.join(' ') end def inspect to_s end end _,x = Net::HTTP.new("itafullsite.dev.neptuneweb.com").get("/careers/puzzles/MOVIES.LST") Title.process_lines(x) p Title.generate_titles -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/