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