Skip to content

Instantly share code, notes, and snippets.

@thata
Last active August 17, 2022 15:06
Show Gist options
  • Save thata/c75c61438115dd46709a4cca97adc994 to your computer and use it in GitHub Desktop.
Save thata/c75c61438115dd46709a4cca97adc994 to your computer and use it in GitHub Desktop.
件のJavaのコード
# 例のJavaのコードをRubyへ移植
#
# usage: echo "3\nfoo foo\nbar ber\nbuzz buzzz" | ruby main.rb
# #=> 3
# 渡された二つの文字列の差異の数を返す
def num_of_difference(word1, word2)
return word1.size if word1.size != word2.size
pairs = word1.split(//).zip(word2.split(//))
pairs.map {|pair|
(pair[0] == pair[1]) ? 0 : 1
}.sum
end
def score(word1, word2)
n = num_of_difference(word1, word2)
if n == 0
# 差異が0なら2点
2
elsif n == 1
# 差異が1なら1点
1
else
# それ以外は0点
0
end
end
lines = ARGF.readlines
n = lines[0].to_i
puts lines[1..n].map { |line|
w1, w2 = line.split
score(w1, w2)
}.sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment