All pastes #1966202 Raw Edit

friends.rb

public ruby v1 · immutable
#1966202 ·published 2010-10-18 20:45 UTC
rendered paste body
$match_hash = Hash.new {|h,k| h[k] = nil}def read(filename)   $names = File.read(filename).split("\n")enddef first_frequencies   first = $names.map {|line| line.split(" ")[0]}   first_freq = (first.map {|name| [name, first.count(name)]}).uniq   return first_freqenddef last_frequencies   last = $names.map {|line| line.split(" ")[-1]}   last_freq = (last.map {|name| [name, last.count(name)]}).uniq   return last_freqenddef matches(search)   if $match_hash[search] == nil      #puts "matches(#{search}) = ?"      $match_hash[search] = $names.select {|el| match?(el, search)}   end   return $match_hash[search]enddef match?(name, search)   #name.downcase.split(" ").inject(false) {|bool, n| bool or n.include?(search.downcase)}   name.downcase.include? search.downcaseenddef strings(length)   if length <= 0 or length.to_i != length      return [""]   end   ret = []   for str in strings(length-1)      for ch in "a".."z"         ret << str + ch      end   end   return retenddef unique_generators_by_length(length)   print "mapping... "   $stdout.flush   list = strings(length).map {|str| [[str], matches(str)]}   puts "mapped"   $stdout.flush   print "selecting..."   $stdout.flush   list = list.select {|el| el[1].length == 1}   puts "selected"   $stdout.flush   return listenddef consolidate_bad(generators)   # takes a list of [search, name] pairs and consolidates searches that lead   # to the same name   names = generators.map {|el| el[1]}.uniq   searches = generators.map {|el| el[0]}   ret = names.map {|name| [searches.select {|el| generators.include?([el, name])}, name]}   return retenddef consolidate(generators)   hash = Hash.new {|h, k| h[k] = 0}   for name in generators.map {|el| el[1]}      hash[name] += 1   end   ret = []   for pair in generators      puts "working on #{pair.inspect}"      next if pair == nil      if hash[pair[1]] == 1         ret << pair      else         searches = []         for i in 0...generators.length            next if generators[i] == nil            if generators[i][1] == pair[1]               searches << generators[i][0][0]               generators[i] = nil            end         end         ret << [searches, pair[1]]      end   end   return retenddef all_generators   result = Hash.new {|h,k| h[k] = 0}   for name in $names      for word in name.split(" ")         for len in 0...(word.length)            for i in 0...(word.length-len)               search = word[i..i+len]               result[search] = matches(search).length            end         end      end   end   return resultenddef minimal_generators(name)   ret = []   found = false   for word in name.split(" ")      for len in 0...word.length         break if found         for i in 0...word.length-len            search = word[i..i+len]            if matches(search).length == 1               ret << search               found = true            end         end      end   end   return retenddef minimal_generating_length(name)   temp = minimal_generators(name)   if temp.length == 0      return -1   else      return temp[0].length   endendread("friends.txt")temp = all_generatorsp temp.length#puts matches("nic")#q = temp.keys.select {|el| temp[el] == 1}#p q#puts q.length#temp = $names.map {|name| [name, minimal_generators(name)]}.reject {|el| el[1] == []}#p temp#puts temp.length#freq = Hash.new {|h,k| h[k] = 0}#for elem in temp#   freq[elem[1]] += 1#end##p freq