in reply to Efficient Rating of Test Answers
use strict; sub rank { local $_ = shift; tr/OBAFGKM/1-7/; $_; } # Enter questions and scores and answers # Enter answers as a range of values. my %scores = ( 1 => { 1 => { O9=>'B1', K1=>'K2', }, 0.5 => { B4=>'B5', B7=>'B7', }, }, 2 => { 1 => { K8=>'K9', }, 0.5 => { F1=>'F2', }, }, ); my %ranges; for my $question (keys %scores) { my $scores = $scores{$question}; my @ranges; for my $score (keys %$scores) { my $ranges = $scores->{$score}; push @ranges, map { [ $score, rank($_), rank($ranges->{$_}) ] } keys %$ranges; } @ranges = sort {$a->[1] cmp $b->[1]} @ranges; $ranges{$question} = \@ranges; } my %answers = (1 => 'K2', 2=>'F1'); my $total = 0; while (my ($qu, $ans) = each %answers) { $ans = rank($ans); my ($aref) = grep { $_->[1] le $ans and $ans le $_->[2] } @{$ranges{$qu}}; $total += $aref->[0] if defined $aref; } print $total,"\n";
|
|---|