Advertisement
cd62131

count string matches

Dec 19th, 2018
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.72 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. def check(correct, user_input)
  3.   copy = correct.dup
  4.   size = correct.size
  5.   hit = 0
  6.   user_input.chars.each do |c|
  7.     begin
  8.       copy[/#{c}/] = ''
  9.       hit += 1
  10.     rescue
  11.       break
  12.     end
  13.   end
  14.   hit * 100 / size
  15. end
  16. def qestion
  17.   @word ||=<<-'EOS'.gsub(/\s+/,'').chars
  18.     あいうえおぁぃぅぇぉかきくけこがぎぐげごさしすせそ
  19.     ざじずぜぞたちつってとだぢづでどなにぬねのはひふへほ
  20.     ばびぶべぼまみむめもやゆよわをん
  21.   EOS
  22.   5.times.map { @word.sample 1 }.flatten.join
  23. end
  24. def main
  25.   1.times do
  26.     q = qestion
  27.     puts q
  28.     user_input = gets.chomp
  29.     puts "#{check(q, user_input)} point"
  30.   end
  31. end
  32. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement