æ²ãã¨ã®å®å ¨ãªãã¹ãåæããæ¹éã«æ¸ãæãã¦ãåä¸ã®æ²ãé¸ã°ããªãããã«ãã¦ã¿ã¾ããããã¨ã¯ç´°ããå¤æ´ãããã¤ãã
fname = "songs_.txt" open(fname) {|file| # create path data target_char = [] for a in "A".."Z" do target_char << a.to_s end for a in 0 .. 9 do target_char << a.to_s end # ãããããpathã«ç©ºé åãå²ãå½ã¦ã¦ãããã¨ã«ãããã£ã¦ãåã®ãã¼ã¸ã§ã³ã¯é½åãããã°ããªãã£ãã¿ããã§ãã⦠path = {} target_char.each do |t| path[t] = [] end songdb = {} while line = file.gets line.chomp! len = line.length upper = line.upcase first = upper[0] last = upper[len - 1] if first =~ /[A-Z0-9]/ && last =~ /[A-Z0-9]/ if !path[first].include?(last) path[first] << last end key = first + last if songdb[key] == nil songdb[key] = [line] else songdb[key] << line end # puts "#{first} -> #{last}" end end # å ¨ãã¹æ¢ç´¢ solutions = [] target_char.each do |first_char| path[first_char].each do |second_char| path[second_char].each do |third_char| path[third_char].each do |fourth_char| if path[fourth_char].include?(first_char) solutions << [first_char, second_char, third_char, fourth_char] end end end end end # ããããå¾®å¦ã«æ¸ãæã patterns = {} target_char.each do |ch| patterns[ch] = [] end solutions.each do |sol| key1 = sol[0] + sol[1] key2 = sol[1] + sol[2] key3 = sol[2] + sol[3] key4 = sol[3] + sol[0] # ããªã¼ãå解ãããã§uniq.lengthã4ã®ãã®ã®ã¿æ®ã songdb[key1].each do |s1| songdb[key2].each do |s2| songdb[key3].each do |s3| songdb[key4].each do |s4| if [s1, s2, s3, s4].uniq.length == 4 patterns[key1[0]] << [s1, s2, s3, s4] end end end end end # åã®ãã¼ã¸ã§ã³ã®é·ã£ããããã¨ãããjoinã§æ¸ãæã # songs1 = "(" + songdb[key1].join(" / ") + ")" # songs2 = "(" + songdb[key2].join(" / ") + ")" # songs3 = "(" + songdb[key3].join(" / ") + ")" # songs4 = "(" + songdb[key4].join(" / ") + ")" # puts songs1 + " -> " + songs2 + " -> " + songs3 + " -> " + songs4 end # åºåãåã¾ã§ã¯æ¨æºåºåããªãã¤ã¬ã¯ããã¦ããã¹ãçæãã¦ããã©ä»åããæåãã¨ã«åããã target_char.each do |ch| f = File.open(ch.to_s + ".txt","w") f.puts "**#{ch}**" patterns[ch].each do |ptn| f.puts ptn.join(" -> ") end end }
出力ファイルはこちらになります。23MBã®ZIPã«ãªã£ã¦ãã¾ãã