Skip to content

Instantly share code, notes, and snippets.

@M0ses
Created November 1, 2017 14:21
Show Gist options
  • Save M0ses/6a49d170998a57d399d94ed3741798bd to your computer and use it in GitHub Desktop.
Save M0ses/6a49d170998a57d399d94ed3741798bd to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
cls();
our $all_results = [];
our $counter_all = 0;
our $counter_error = 0;
our $stime = 0;
my $input = 0;
my $mp = calc_math_problem();
print_math_problem($mp);
while (1) {
print "Ergebnis: ";
$input = <STDIN>;
if ($input !~ /^[0-9]{1,2}$/ ) {
print "Fehlerhafte Eingabe! Versuche es noch einmal.\n";
} else {
if ($input != $mp->{result}) {
print "Falsch! Versuche es noch einmal.\n";
$mp->{retries}++;
} else {
$mp->{etime} = time();
$mp->{duration} = $mp->{etime} - $mp->{stime};
print "Zeit: $mp->{duration}\n";
print "Richtig! Weitermachen (J|n)? ";
my $exit = <STDIN>;
if ($exit eq "n\n") {
last;
}
$mp = calc_math_problem();
cls();
print_math_problem($mp);
}
}
}
print "Anzahl der Aufgaben: $counter_all\n";
print "Anzahl der Fehler: $counter_error\n";
print_detailed();
print_statistics();
exit 0;
sub print_statistics {
my @time_sorted = sort { $a->{duration} <=> $b->{duration} } @$all_results;
# Calc Totals
my $total_time;
$total_time += $_->{duration} for (@$all_results);
my $total_retries;
$total_retries += $_->{retries} for (@$all_results);
my $total_points;
$total_points += ($_->{points} / ( ($_->{retries}+1) * $_->{duration} )) for (@$all_results);
# Calc Min/Max
my $max_time = pop(@time_sorted)->{duration};
my $min_time = shift(@time_sorted)->{duration};
# Calc AVG
my $avg_time = $total_time / @$all_results;
my $avg_points = $total_points / @$all_results;
print "\n";
print "#### AUSWERTUNG ####\n";
print "Aufgaben insgesamt: ".@$all_results."\n";
print "Punkte insgesamt: ".int($total_points)."\n";
print "Fehler insgesamt: $total_retries\n";
print "Zeit insgesamt: $total_time\n";
print "\n";
print "Maximale Lösungsdauer: $max_time\n";
print "Minimale Lösungsdauer: $min_time\n";
print "\n";
printf("Durchschnittliche Lösungsdauer: %.1f\n",$avg_time);
printf("Durchschnittliche Punkte: %d\n",$avg_points);
print "\n";
}
sub calc_points {
my ($mp) = @_;
if ($mp->{result} < 10 ) {
$mp->{points} = 100;
} elsif ( $mp->{result} < 50 ) {
$mp->{points} = 1000;
} elsif ( $mp->{result} < 80 ) {
$mp->{points} = 2000;
} else {
$mp->{points} = 3000;
}
}
sub print_detailed {
print "
#### Übersicht der Einzelaufgaben ####
";
for (@$all_results) {
print "Aufgabe: $_->{num1} * $_->{num2} = $_->{result}\tLösungszeit: $_->{duration}\tPunkte: $_->{points}\tFehlversuche: $_->{retries}\n";
}
}
sub cls {
print "\033c";
}
sub calc_math_problem {
$counter_all++;
my $mp = {
num1 => int(rand(10)),
num2 => int(rand(10)),
retries => 0,
};
push(@$all_results, $mp);
$mp->{result} = $mp->{num1} * $mp->{num2};
calc_points($mp);
return $mp;
}
sub print_math_problem {
my ($mp) = @_;
print "Aufgabe " . (@$all_results + 1)."\n";
print "$mp->{num1} * $mp->{num2} = \n";
$mp->{stime}=time();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment