Created
December 27, 2010 18:14
-
-
Save folsen/756376 to your computer and use it in GitHub Desktop.
A word-problem game from a swedish newspaper solved in very poor (old) ruby code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def init_wordlist() | |
@wordlist = {} | |
file = File.new("sv_SE.dic", "r") | |
while content = file.gets | |
@wordlist[content.split("/")[0]] = 1 | |
end | |
file.close | |
end | |
def get_permutations(letters) | |
permutations = [] | |
if letters.size == 1 | |
permutations << letters.first | |
else | |
letters.each_with_index do |letter, i| | |
surrounding_letters = letters.dup; surrounding_letters.delete_at(i) | |
permutations += get_permutations(surrounding_letters).map { |permutation| letter + permutation } | |
end | |
end | |
return permutations | |
end | |
def generate_combinations(arr) | |
combinationshash = {} | |
for i in (4..9) | |
combinationshash[i] = [] | |
end | |
for i in (0..arr.size-8) | |
for j in (i+1..arr.size-7) | |
for k in (j+1..arr.size-6) | |
for l in (k+1..arr.size-5) | |
for m in (l+1..arr.size-4) | |
for n in (m+1..arr.size-3) | |
for o in (n+1..arr.size-2) | |
for p in (o+1..arr.size-1) | |
combinationshash[8] << get_permutations([arr[i], arr[j], arr[k], arr[l], arr[m], arr[n], arr[o], arr[p]]) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
for j in (0..arr.size-7) | |
for k in (j+1..arr.size-6) | |
for l in (k+1..arr.size-5) | |
for m in (l+1..arr.size-4) | |
for n in (m+1..arr.size-3) | |
for o in (n+1..arr.size-2) | |
for p in (o+1..arr.size-1) | |
combinationshash[7] << get_permutations([arr[j], arr[k], arr[l], arr[m], arr[n], arr[o], arr[p]]) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
for k in (0..arr.size-6) | |
for l in (k+1..arr.size-5) | |
for m in (l+1..arr.size-4) | |
for n in (m+1..arr.size-3) | |
for o in (n+1..arr.size-2) | |
for p in (o+1..arr.size-1) | |
combinationshash[6] << get_permutations([arr[k], arr[l], arr[m], arr[n], arr[o], arr[p]]) | |
end | |
end | |
end | |
end | |
end | |
end | |
for l in (0..arr.size-5) | |
for m in (l+1..arr.size-4) | |
for n in (m+1..arr.size-3) | |
for o in (n+1..arr.size-2) | |
for p in (o+1..arr.size-1) | |
combinationshash[5] << get_permutations([arr[l], arr[m], arr[n], arr[o], arr[p]]) | |
end | |
end | |
end | |
end | |
end | |
for m in (0..arr.size-4) | |
for n in (m+1..arr.size-3) | |
for o in (n+1..arr.size-2) | |
for p in (o+1..arr.size-1) | |
combinationshash[4] << get_permutations([arr[m], arr[n], arr[o], arr[p]]) | |
end | |
end | |
end | |
end | |
combinationshash[9] = get_permutations(arr) | |
@nmbr_of_words = 0 | |
for i in (4..9) | |
combinationshash[i].flatten! | |
@nmbr_of_words += combinationshash[i].size | |
end | |
return combinationshash | |
end | |
def get_all_words(letters, required) | |
lookups = 0 | |
puts "Beräknar permutationer" | |
permutationhash = generate_combinations(letters) | |
puts @nmbr_of_words.to_s + " permutationer fanns" | |
puts "Slår upp ord" | |
words = [] | |
for i in (4..9) | |
puts "Ord med " + i.to_s + " bokstäver" | |
permutationhash[i].each do |permutation| | |
unless (words.include?(permutation)) | |
lookups += 1 if permutation.include?(required) | |
if(permutation.include?(required) && @wordlist[permutation] != nil) | |
words << permutation | |
end | |
end | |
end | |
end | |
puts "Gjorde " + lookups.to_s + " uppslag i ordlistan\n\n" | |
return words | |
end | |
init_wordlist() | |
puts "Bokstäver:" | |
letters = gets | |
puts "Obligatorisk:" | |
required = gets | |
puts "\n" | |
all_words = get_all_words(letters.split(//)[0..-2], required[0..-2]) | |
puts "Ord som kunde skapas:" | |
all_words.each_with_index do |word, i| | |
puts "" + (i+1).to_s + ": " + word | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment