olwdelah eb yht amne
- hallowed be thy amen
- hallowed be thy mane
- hallowed be thy mean
- hallowed be thy name
| #encoding: UTF-8 | |
| words = File.new('/usr/share/dict/words').read.split("\n"); | |
| puts "Sözlük yüklendi"; | |
| cache = {}; | |
| scrambled = gets.split(" "); | |
| start = Time.now | |
| @possibilities = []; | |
| scrambled.each do |scramble| | |
| sorted = scramble.split("").map{ |s| s.downcase; }.sort | |
| @possibilities.push(words.select{ |x| | |
| next if sorted.length != x.length || !sorted.include?(x.slice(0, 1).downcase); # Tek seferlik işlemleri hızlandırmak için uzunluk ve ilk harf kotrolü, kod düzenlenip while döngüsüne falan alınırsa işlem, sadece ilk istekte yavaş, daha sonra cache'den işlem yapması için bu satır kaldırılabilir. | |
| (cache[x] || (cache[x] = x.split("").map{ |s| s.downcase; }.sort)) == sorted | |
| }); | |
| if @possibilities.last.length == 0 then @possibilities[@possibilities.length - 1] = [scramble]; end | |
| end | |
| # Multi-dimensional Array Kombinasyonlarını yazdırma | |
| total = 1; | |
| sizeArray = @possibilities.map{ |a| total *= a.length; [a.length, 0]; }; | |
| for q in total.downto(1) | |
| current = ""; | |
| for i in 0..sizeArray.length - 1 | |
| current += " " + @possibilities[i][sizeArray[i][1]]; | |
| end | |
| puts current.strip; | |
| for i in (sizeArray.length - 1).downto(0) | |
| if sizeArray[i][1] + 1 < sizeArray[i][0] | |
| sizeArray[i][1] += 1; | |
| break; | |
| end | |
| sizeArray[i][1] = 0; | |
| end | |
| end | |
| puts "#{Time.now - start} saniyede calculate edildi." |