fork download
  1. # ファイルから問題を読み込む
  2. def load_problems(filename)
  3. problems = File.open(filename, "r"){ |f|
  4. f.map{ |line| line.strip.split(' ') }
  5. }
  6. return problems
  7. end
  8.  
  9.  
  10. # 三角形の判定
  11. #  あ)正三角形である。
  12. #  い)二等辺三角形である。(ただし、正三角形であるとはいえない。)
  13. #  う)二等辺三角形ではない。または、二等辺三角形かどうかわからない。
  14. def judge_triangle(sides, angles)
  15.  
  16. answers = ["あ", "い", "う"]
  17.  
  18. # う)二等辺三角形ではない。または、二等辺三角形かどうかわからない。
  19. answer = answers[2]
  20.  
  21. # い)二等辺三角形である。(ただし、正三角形であるとはいえない。)
  22. # 二辺の長さが等しい
  23. answer = answers[1] if sides.length != sides.uniq.length
  24. # 二角が等しい
  25. answer = answers[1] if angles.length != angles.uniq.length
  26.  
  27. # あ)正三角形である。
  28. # 三辺の長さが等しい
  29. answer = answers[0] if sides.length == 3 && sides.uniq.length == 1
  30. # 二角が60度
  31. answer = answers[0] if angles.count(60) >= 2
  32. # 二辺の長さが等しく、一角が60度
  33. answer = answers[0] if sides.length == 2 && sides.uniq.length == 1 && angles.include?(60)
  34.  
  35. return answer
  36. end
  37.  
  38.  
  39. problems = load_problems("data.utf8.txt")
  40.  
  41. wrong_data_id = []
  42. problems.each do |problem|
  43. # データID、空欄Aの内容、空欄Aの内容に対応した答え
  44. data_id, condition, answer = problem[0], problem[1], problem[2]
  45. # 空欄Aの内容から、長さの数値を取り出し、配列に格納する
  46. sides = condition.scan(/(\d+)cm/).flatten.collect{|item| item.to_i()}
  47. # 空欄Aの内容から、角度の数値を取り出し、配列に格納する
  48. angles = condition.scan(/(\d+)/).flatten.collect{|item| item.to_i()}
  49. # 角度の数値が2つであれば、残りの角度を求め、配列に格納する
  50. angles << 180 - angles[0] - angles[1] if angles.length == 2
  51.  
  52. # 空欄Aの内容に対応した答えになっていないとき
  53. if answer != judge_triangle(sides, angles)
  54. wrong_data_id << data_id
  55. end
  56. end
  57. # 結果の出力
  58. puts wrong_data_id.join(",")
  59.  
Runtime error #stdin #stdout #stderr 0.01s 7452KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog.rb:16: invalid multibyte char (US-ASCII)
prog.rb:16: invalid multibyte char (US-ASCII)
prog.rb:16: syntax error, unexpected $end, expecting ']'
  answers = ["あ", "い", "う"]
                ^