Last active
August 29, 2015 14:04
-
-
Save daddydanielt/70cbb0ae827766a01bd7 to your computer and use it in GitHub Desktop.
CodeEval - challenge coding snippets
This file contains 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
# BEAUTIFUL_STRINGS (Facebook Hacker Cup 2013 Hackathon) | |
File.open(ARGV[0]).each_line do |line| | |
next if line.chomp.empty? | |
single_word_character_statistic =[] | |
l=line.chomp | |
l.split.each_with_index do |v,i| | |
s_v=v.chars.sort { |x,y| x.downcase <=> y.downcase } | |
r={} | |
s_v.map{|i| i.downcase}.uniq.each do |v| | |
group_count = 0 | |
s_v.each {|i| (group_count += 1) if i.downcase == v.downcase } | |
if v =~ /[a-zA-Z]/ # by pass character which is not belong to [a-zA-Z] | |
r[v]= group_count | |
else | |
r[v]= 0 | |
end | |
end | |
t_h= Hash[r.sort_by {|k,v| v}.reverse] | |
single_word_character_statistic.push(t_h) | |
end | |
# merge evry words statistic | |
letter_statistic={} | |
single_word_character_statistic.each_with_index do |v,i| | |
if i == 0 | |
letter_statistic = Hash[v.to_a] # copy v to letter_statistic | |
else | |
letter_statistic.merge!(v) { |k,v1,v2| v1 + v2 } | |
end | |
end | |
letter_statistic=Hash[letter_statistic.sort_by { |k,v| v}.reverse] | |
# assign beauty value | |
b_value=26 | |
letter_beauty_value = letter_statistic.inject({}) do |h, (k, v)| | |
h[k] = b_value | |
b_value -= 1 | |
h | |
end | |
max_beauty_value = 0 | |
single_word_character_statistic.inject({}) do |h,v| | |
sum = v.inject(s = 0) do |s, (k,v)| | |
if k =~ /[a-zA-Z]/ # k must be the [a-zA-Z] | |
s += letter_beauty_value[k] * v; | |
#puts "debug: sum=#{s}, (#{k})#{letter_beauty_value[k]} * #{v}" | |
end | |
s | |
end | |
max_beauty_value += sum | |
end | |
#printf "\nsingle_word_character_statistic= #{single_word_character_statistic}\n\n" | |
#puts "" | |
#puts "total_words_characters_statistic: letter_beauty_value" | |
#puts letter_statistic | |
#puts letter_beauty_value | |
#puts "single_word_character_statistic: character count" | |
#puts single_word_character_statistic | |
#puts "max_beauty_value:" | |
puts max_beauty_value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment