diff options
Diffstat (limited to 'misc')
-rwxr-xr-x | misc/randomize-test.pl | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/misc/randomize-test.pl b/misc/randomize-test.pl index 2db3998..e569adb 100755 --- a/misc/randomize-test.pl +++ b/misc/randomize-test.pl @@ -11,10 +11,15 @@ my %opts; getopts("c:N:n:", \%opts); -my @preamble = (); -my @postamble = (); -my @questions = (); -my $nbvarid = 0; +my @preamble = (); # Preamble lines +my @postamble = (); # Postamble lines +my @questions = (); # Array of question hashrefs +# Each question hash has keys: +# {question}: arrayref with question lines +# {answers}: arrayref of arrayrefs with answer lines, correct answer first +# {varid}: idnex in qvars array +my @qvars = (); # Array of question variants +# Each entry is an arrayref of indexes in the questions array ### READ INPUT FILE @@ -45,23 +50,23 @@ while ($_ = <>) { die "wrong placement" if $in_preamble || $in_postamble || $in_qvar || defined($curqn); die "bad format" unless $_ eq "\\begin\{qvar\}\n"; $in_qvar = 1; - $varid = ($nbvarid++); + push @qvars, []; next LINELOOP; } elsif ( $_ =~ m/\\end\{qvar\}/ ) { die "wrong placement" if $in_preamble || $in_postamble || (!$in_qvar) || defined($curqn); die "bad format" unless $_ eq "\\end\{qvar\}\n"; $in_qvar = 0; - $varid = undef; next LINELOOP; } elsif ( $_ =~ m/\\begin\{question\}/ ) { die "wrong placement" if $in_preamble || $in_postamble || defined($curqn); die "bad format" unless $_ eq "\\begin\{question\}\n"; my %qn = (); - $varid = ($nbvarid++) unless $in_qvar; - $qn{varid} = $varid; + push @qvars, [] unless $in_qvar; + $qn{varid} = $#qvars; $qn{question} = []; $qn{answers} = []; push @questions, \%qn; + push @{$qvars[$#qvars]}, $#questions; $listref = $qn{question}; $curqn = \%qn; next LINELOOP; @@ -90,14 +95,12 @@ while ($_ = <>) { } -# printf STDERR "nbvarid=%d (total=%d)\n", $nbvarid, scalar(@questions); - ### RANDOMIZE my $commonseed = $opts{N} // ""; my $seed = $opts{n} // ""; -my $nbqn = $opts{c} // int(($nbvarid+1)/2); +my $nbqn = $opts{c} // int((scalar(@qvars)+1)/2); my @questab = (); my @quesanstab = (); @@ -106,24 +109,21 @@ if ( 1 ) { ## Keep following variables local my @hashlist; -for ( my $i=0 ; $i<scalar(@questions) ; $i++ ) { - push @hashlist, sha256("${commonseed}\n${seed}\nQ\n${i}\n"); +for ( my $u=0 ; $u<scalar(@qvars) ; $u++ ) { + push @hashlist, sha256("${commonseed}\n${seed}\nQV\n${u}\n"); } -my @prequestab = sort { $hashlist[$a] cmp $hashlist[$b] } (0..(scalar(@questions)-1)); -my %varid_punch; -PREQUESLOOP: -for ( my $k=0 ; $k<scalar(@prequestab) ; $k++ ) { - last if scalar(@questab) > $nbqn; - my $i = $prequestab[$k]; - my $qn = $questions[$i]; - my $varid = $qn->{varid}; - if ( defined($varid) ) { - if ( defined($varid_punch{$varid}) ) { - next PREQUESLOOP; - } else { - $varid_punch{$varid} = $i; - } +my @qvartab = sort { $hashlist[$a] cmp $hashlist[$b] } (0..(scalar(@qvars)-1)); + +for ( my $k=0 ; $k<scalar(@qvartab) && $k<$nbqn ; $k++ ) { + my $u = $qvartab[$k]; + @hashlist = (); + for ( my $kv=0 ; $kv<scalar(@{$qvars[$u]}) ; $kv++ ) { + my $i = $qvars[$u]->[$kv]; + push @hashlist, sha256("${commonseed}\n${seed}\nQ\n${i}\n"); } + my $kv = (sort { $hashlist[$a] cmp $hashlist[$b] } (0..(scalar(@{$qvars[$u]})-1)))[0]; + my $i = $qvars[$u]->[$kv]; + die "this is impossible" unless $questions[$i]->{varid} == $u; push @questab, $i; } @@ -158,13 +158,13 @@ for ( my $k=0 ; $k<scalar(@questab) ; $k++ ) { foreach my $l ( @{$qn->{question}} ) { print $l; } - for ( my $kk=0 ; $kk<scalar(@{$quesanstab[$i]}) ; $kk++ ) { - my $j = $quesanstab[$i]->[$kk]; + for ( my $kj=0 ; $kj<scalar(@{$quesanstab[$i]}) ; $kj++ ) { + my $j = $quesanstab[$i]->[$kj]; my $a = $qn->{answers}->[$j]; foreach my $l ( @{$a} ) { print $l; } - push @correct, sprintf("%d%s", $k+1, chr(ord("A")+$kk)) if $j==0; + push @correct, sprintf("%d%s", $k+1, chr(ord("A")+$kj)) if $j==0; } print "\\end\{question\}\n\n"; } |