]> git.deb.at Git - debienna.git/blob - Kata4Lösungen/Perl/index.mdwn
use unix newlines everywhere
[debienna.git] / Kata4Lösungen / Perl / index.mdwn
1 = Part 1 =
2
3 {{{
4 #!/usr/bin/perl
5
6 while(<>)
7 {
8         split;
9         next unless ($_[0] > 0 and $_[0] < 31);
10         $day = $_[0];
11         $spread = $_[1] - $_[2];
12         unless (defined $min_spread) 
13         {
14                 $spread_day = $day;
15                 $min_spread = $spread;
16         }
17         if ($spread <= $min_spread)
18         {
19                 $spread_day = $day;
20                 $min_spread = $spread;
21         }
22 }
23 print $spread_day, "\n";
24 }}}
25
26 = Part 2 =
27
28 {{{
29 #!/usr/bin/perl
30
31 while(<>)
32 {
33         split;
34         next unless ($_[0] > 0 and $_[0] < 21);
35         $team = $_[1];
36         $spread = abs($_[6] - $_[8]);
37         unless (defined $min_spread)
38         {
39                 $spread_team = $team;
40                 $min_spread = $spread;
41         }
42         if ($spread <= $min_spread)
43         {
44                 $spread_team = $team;
45                 $min_spread = $spread;
46         }
47 }
48 print $spread_team, "\n";
49 }}}
50
51 = Part 3 =
52
53 {{{
54 #!/usr/bin/perl
55
56 sub dm($$$$)
57 {
58         my $file = shift;
59         my $label_col = shift;
60         my $alpha_col = shift;
61         my $beta_col = shift;
62
63         my ($spread_label, $min_spread) = ("", 100000);
64
65         open (IN, "<$file");
66         while(<IN>)
67         {
68                 split;
69                 next unless ($_[$alpha_col] =~ /^[0-9]+/);
70                 next unless ($_[$beta_col] =~ /^[0-9]+/);
71                 my $label = $_[$label_col];
72                 my $spread = abs($_[$alpha_col] - $_[$beta_col]);
73                 unless (defined $min_spread)
74                 {
75                         print ".";
76                         $spread_label = $label;
77                         $min_spread = $spread;
78                 }
79                 if ($spread <= $min_spread)
80                 {
81                         $spread_label = $label;
82                         $min_spread = $spread;
83                 }
84         }
85         close (IN);
86         $spread_label;
87 }
88
89 print "part1: ", dm("../K4Weather.txt", 0, 1, 2), "\n";
90 print "part2: ", dm("../K4Soccer.txt", 1, 6, 8), "\n";
91 }}}
92 ----
93 CategoryCodeSnippets