Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- #use like this:
- #$ practice.rb file.txt 50
- class Entry
- def initialize(line)
- if line =~ /:/
- arr = line.split ":"
- else
- arr = line.split " ";
- end
- @question = arr.shift;
- @answers = arr.join " ";
- end
- def check(ans)
- ans.chomp!;
- return false if(ans == "");
- @answers.include? ans;
- end
- def ask
- print @question," ";
- end
- def ans
- @answers;
- end
- end
- class Test
- def initialize(file)
- @entries=[]
- import(file)
- end
- def import(file)
- file.each do |line|
- @entries.push Entry.new(line);
- end
- end
- def test(n)
- count = n;
- score = 0;
- while n > 0
- entry = @entries[rand(@entries.size)];
- entry.ask;
- if entry.check(STDIN.gets)
- score += 1;
- print "ok #{entry.ans}\n";
- else
- print "no #{entry.ans}\n";
- end
- n -= 1;
- end
- mesg = "#{ARGV[0]} #{ARGV[1]}: #{score}/#{count} = #{score.to_f / count.to_f * 100.0}%\n";
- print mesg
- File.open("practicelog","a") { |logfile|
- logfile.puts mesg;
- }
- end
- end
- n=ARGV[1].to_i
- test=Test.new(File.open(ARGV[0]))
- test.test(n)
Advertisement
Add Comment
Please, Sign In to add comment