Created
October 22, 2015 17:35
-
-
Save topisani/accbcb70fe7dd0e32929 to your computer and use it in GitHub Desktop.
Ruby script for a weird point system with words.
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
require 'yaml' | |
YAML::ENGINE.yamler='psych' | |
#Fordi ASCII-koderne tæller W med, og det har jane nok ikke gjort. Ellers må man tilføge dem. | |
LETTERS = { | |
'a' => 1, | |
'b' => 2, | |
'c' => 3, | |
'd' => 4, | |
'e' => 5, | |
'f' => 6, | |
'g' => 7, | |
'h' => 8, | |
'i' => 9, | |
'j' => 10, | |
'k' => 11, | |
'l' => 12, | |
'm' => 13, | |
'n' => 14, | |
'o' => 15, | |
'p' => 16, | |
'q' => 17, | |
'r' => 18, | |
's' => 19, | |
't' => 20, | |
'u' => 21, | |
'v' => 22, | |
'w' => 22, #jeg har givet W samme tal som V | |
'x' => 23, | |
'y' => 24, | |
'z' => 25, | |
'æ' => 26, | |
'ø' => 27, | |
'å' => 28 | |
} | |
def make | |
puts "Vent venligst" | |
infile = File.open("./words-da").read | |
outfile = "./wordpoints.yml" | |
infile.gsub!(/\r\n?/, "\n") | |
words = {} | |
infile.each_line do |line| | |
words[line] = processWord line | |
end | |
File.open(outfile, "w") do |file| | |
file.write words.to_yaml( ) | |
end | |
puts "Færdig!" | |
end | |
# Alle ord med score points | |
def findWords( score ) | |
file = YAML::load_file "./wordpoints.yml" | |
words = [] | |
file.each do |k, v| | |
if v == score | |
words.push k | |
end | |
end | |
return words | |
end | |
def findTopWords | |
file = YAML::load_file "./wordpoints.yml" | |
file.sort_by { |k,v| v } | |
end | |
# Points for et ord | |
def processWord( word ) | |
points = 0 | |
word.downcase.split("").each do |l| | |
points += LETTERS[l] if LETTERS[l] != nil | |
end | |
return points | |
end | |
# Kør funktioner | |
if ARGV[0] == "top" | |
words = findTopWords | |
l = words.length | |
ARGV[1] ||= 10 | |
words[(l-ARGV[1].to_i)..l].each do |k , v| | |
puts "#{k}: #{v}" | |
end | |
elsif ARGV[0] == "make" | |
make | |
elsif ARGV[0] =~ /\A[-+]?[0-9]+\z/ | |
words = findWords ARGV[0].to_i | |
if words.length > 0 | |
puts "Ord med #{ARGV[0]} point: \n" | |
words.each do |word| | |
puts word + "\n" | |
end | |
else | |
puts "Der er ingen danske ord med #{ARGV[0]} point!" | |
end | |
else | |
points = processWord ARGV[0] | |
puts "#{ARGV[0]} har #{points} point" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment