line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Math::Tutor::Cmd::Unit::Cmd::Add; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4616
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use vars qw(@ISA $VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
71
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
App::Math::Tutor::Cmd::Unit::Cmd::Add - Plugin for addition and subtraction of numbers with units |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.005'; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
4
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
17
|
1
|
|
|
1
|
|
364
|
use MooX::Cmd; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
18
|
1
|
|
|
1
|
|
1547
|
use MooX::Options; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has template_filename => ( |
21
|
|
|
|
|
|
|
is => "ro", |
22
|
|
|
|
|
|
|
default => "twocols" |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
with "App::Math::Tutor::Role::UnitExercise"; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build_exercises |
28
|
|
|
|
|
|
|
{ |
29
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
30
|
0
|
|
|
|
|
|
my (@tasks); |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
foreach my $i ( 1 .. $self->quantity ) |
33
|
|
|
|
|
|
|
{ |
34
|
0
|
|
|
|
|
|
my @line; |
35
|
0
|
|
|
|
|
|
foreach my $j ( 0 .. 1 ) |
36
|
|
|
|
|
|
|
{ |
37
|
0
|
|
|
|
|
|
my ( $a, $b ) = $self->get_unit_numbers(2); |
38
|
0
|
|
|
|
|
|
push @line, [ $a, $b ]; |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
|
push @tasks, \@line; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $exercises = { |
44
|
|
|
|
|
|
|
section => "Unit addition / subtraction", |
45
|
|
|
|
|
|
|
caption => 'Units', |
46
|
|
|
|
|
|
|
label => 'unit_addition', |
47
|
|
|
|
|
|
|
header => [ [ 'Unit Addition', 'Unit Subtraction' ] ], |
48
|
|
|
|
|
|
|
solutions => [], |
49
|
|
|
|
|
|
|
challenges => [], |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
foreach my $line (@tasks) |
53
|
|
|
|
|
|
|
{ |
54
|
0
|
|
|
|
|
|
my ( @solution, @challenge ); |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
foreach my $i ( 0 .. 1 ) |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
|
|
|
|
|
my ( $a, $b ) = @{ $line->[$i] }; |
|
0
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
my $op = $i ? '-' : '+'; |
60
|
0
|
0
|
0
|
|
|
|
$op eq '-' and $a < $b and ( $b, $a ) = ( $a, $b ); |
61
|
0
|
|
|
|
|
|
push( @challenge, "\$ $a $op $b = \$" ); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my @way; # remember Frank Sinatra :) |
64
|
0
|
|
|
|
|
|
push( @way, "$a $op $b" ); |
65
|
0
|
0
|
|
|
|
|
my $beg = $a->begin < $b->begin ? $a->begin : $b->begin; |
66
|
0
|
0
|
|
|
|
|
my $end = $a->end > $b->end ? $a->end : $b->end; |
67
|
0
|
|
|
|
|
|
my @ap = @{ $a->parts }; |
|
0
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my @bp = @{ $b->parts }; |
|
0
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my ( @cparts, @dparts ); |
70
|
0
|
|
|
|
|
|
for my $i ( $beg .. $end ) |
71
|
|
|
|
|
|
|
{ |
72
|
0
|
|
|
|
|
|
my @cps; |
73
|
0
|
0
|
0
|
|
|
|
$i >= $a->begin and $i <= $a->end and push( @cps, shift @ap ); |
74
|
0
|
0
|
0
|
|
|
|
$i >= $b->begin and $i <= $b->end and push( @cps, shift @bp ); |
75
|
0
|
0
|
|
|
|
|
scalar @cps or next; |
76
|
0
|
|
|
|
|
|
my $cp = join( " $op ", @cps ); |
77
|
0
|
|
|
|
|
|
my $dp = eval "$cp;"; |
78
|
0
|
0
|
0
|
|
|
|
if ( $dp < 0 ) |
|
|
0
|
|
|
|
|
|
79
|
|
|
|
|
|
|
{ |
80
|
0
|
|
|
|
|
|
--$dparts[-1]; |
81
|
0
|
|
|
|
|
|
$dp += $a->type->{spectrum}->[$i]->{max} + 1; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif ( defined $a->type->{spectrum}->[$i]->{max} |
84
|
|
|
|
|
|
|
and $dp > $a->type->{spectrum}->[$i]->{max} ) |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
0
|
|
|
|
|
@dparts and ++$dparts[-1]; |
87
|
0
|
0
|
|
|
|
|
@dparts or push @dparts, 1; |
88
|
0
|
|
|
|
|
|
$dp -= $a->type->{spectrum}->[$i]->{max} + 1; |
89
|
|
|
|
|
|
|
} |
90
|
0
|
|
|
|
|
|
push( @cparts, $cp ); |
91
|
0
|
|
|
|
|
|
push( @dparts, $dp ); |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
my $c = Unit->new( |
94
|
|
|
|
|
|
|
type => $a->type, |
95
|
|
|
|
|
|
|
begin => $beg, |
96
|
|
|
|
|
|
|
end => $end, |
97
|
|
|
|
|
|
|
parts => \@cparts |
98
|
|
|
|
|
|
|
); |
99
|
0
|
|
|
|
|
|
my $d = Unit->new( |
100
|
|
|
|
|
|
|
type => $a->type, |
101
|
|
|
|
|
|
|
begin => $beg - ( scalar @cparts - scalar @dparts ), |
102
|
|
|
|
|
|
|
end => $end, |
103
|
|
|
|
|
|
|
parts => \@dparts |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
push( @way, "$c" ); |
107
|
0
|
|
|
|
|
|
push( @way, "$d" ); |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
push( @solution, '$ ' . join( " = ", @way ) . ' $' ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
push( @{ $exercises->{solutions} }, \@solution ); |
|
0
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
push( @{ $exercises->{challenges} }, \@challenge ); |
|
0
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$exercises; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Copyright 2010-2014 Jens Rehsack. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
124
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
125
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |