Guest User

practice.rb

a guest
Jan 20th, 2011
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.07 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. #use like this:
  3. #$ practice.rb file.txt 50
  4. class Entry
  5.     def initialize(line)
  6.         if line =~ /:/
  7.             arr = line.split ":"
  8.         else
  9.             arr = line.split " ";
  10.         end
  11.         @question = arr.shift;
  12.         @answers = arr.join " ";
  13.     end
  14.     def check(ans)
  15.         ans.chomp!;
  16.         return false if(ans == "");
  17.         @answers.include? ans;
  18.     end
  19.     def ask
  20.         print @question," ";
  21.     end
  22.     def ans
  23.         @answers;
  24.     end
  25. end
  26.  
  27. class Test
  28.     def initialize(file)
  29.         @entries=[]
  30.         import(file)
  31.     end
  32.     def import(file)
  33.         file.each do |line|
  34.             @entries.push Entry.new(line);
  35.         end
  36.     end
  37.     def test(n)
  38.         count = n;
  39.         score = 0;
  40.         while n > 0
  41.             entry = @entries[rand(@entries.size)];
  42.             entry.ask;
  43.             if entry.check(STDIN.gets)
  44.                 score += 1;
  45.                 print "ok #{entry.ans}\n";
  46.             else
  47.                 print "no #{entry.ans}\n";
  48.             end
  49.             n -= 1;
  50.         end
  51.         mesg = "#{ARGV[0]} #{ARGV[1]}: #{score}/#{count} = #{score.to_f / count.to_f * 100.0}%\n";
  52.         print mesg
  53.         File.open("practicelog","a") { |logfile|
  54.             logfile.puts mesg;
  55.         }
  56.     end
  57. end
  58. n=ARGV[1].to_i
  59. test=Test.new(File.open(ARGV[0]))
  60. test.test(n)
Advertisement
Add Comment
Please, Sign In to add comment