]> git.deb.at Git - debienna.git/blob - Archive/Kata4Lösungen/CSharp/index.mdwn
Adding another question for the next meet
[debienna.git] / Archive / Kata4Lösungen / CSharp / index.mdwn
1
2
3 # Part 1
4
5
6 [[!format txt """
7 using System;
8 using System.IO;
9 using System.Text.RegularExpressions;
10
11 namespace Kata4
12 {
13         class Kata4
14         {
15                 public static void Main(string[] argv)
16                 {
17                         FileStream fs = new FileStream("K4Weather.txt", FileMode.Open, FileAccess.Read);
18                         StreamReader r = new StreamReader(fs);
19                         int min_spread = Int32.MaxValue;
20                         int spread_day = -1;
21                         while(r.Peek() > -1)
22                         {
23                                 string s = r.ReadLine();
24                                 Match fields = Regex.Match(s, "^ +([0-9]+) +([0-9]+)[^ ]* *([0-9]+).*");
25                                 try {
26                                         int day = Convert.ToInt32(fields.Groups[1].ToString());
27                                         int min = Convert.ToInt32(fields.Groups[3].ToString());
28                                         int max = Convert.ToInt32(fields.Groups[2].ToString());
29                                         int spread = max - min;
30                                         if (spread <= min_spread)
31                                         {
32                                                 spread_day = day;
33                                                 min_spread = spread;
34                                         }
35                                 } catch {
36                                 }
37                         }
38                         Console.WriteLine(spread_day);
39                 }
40         }
41 }
42
43
44 """]]
45
46 # Part 2
47
48
49 [[!format txt """
50 using System;
51 using System.IO;
52 using System.Text.RegularExpressions;
53
54 namespace Kata4
55 {
56         class Kata4_2
57         {
58                 public static void Main(string[] argv)
59                 {
60                         FileStream fs = new FileStream("K4Soccer.txt", FileMode.Open, FileAccess.Read);
61                         StreamReader r = new StreamReader(fs);
62                         int min_spread = Int32.MaxValue;
63                         string spread_day = "";
64                         while(r.Peek() > -1)
65                         {
66                                 string s = r.ReadLine();
67                                 Match fields = Regex.Match(s, "^ +[0-9]+\\. ([A-Za-z_]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +- +([0-9]+).*");
68                                 try {
69                                         string day = fields.Groups[1].ToString();
70                                         Console.Write("Testing");
71                                         Console.WriteLine(day);
72                                         int min = Convert.ToInt32(fields.Groups[2].ToString());
73                                         int max = Convert.ToInt32(fields.Groups[3].ToString());
74                                         int spread = Math.Abs( max - min );
75                                         if (spread <= min_spread)
76                                         {
77                                                 spread_day = day;
78                                                 min_spread = spread;
79                                         }
80                                 } catch {
81                                 }
82                         }
83                         Console.WriteLine(spread_day);
84                 }
85         }
86 }
87 """]]
88
89 # Part 3
90
91
92 [[!format txt """
93 using System;
94 using System.IO;
95 using System.Text.RegularExpressions;
96
97 namespace Kata4
98 {
99         class Kata4_2
100         {
101                 public static void Main(string[] args)
102                 {
103                         Console.Write("part1: ");
104                         DM("K4Weather.txt", "^ +([0-9]+) +([0-9]+)[^ ]* *([0-9]+).*");
105                         Console.Write("part2: ");
106                         DM("K4Soccer.txt", "^ +[0-9]+\\. ([A-Za-z_]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +- +([0-9]+).*");
107                 }
108
109                 public static void DM(string file, string pattern)
110                 {
111                         FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
112                         StreamReader r = new StreamReader(fs);
113                         int min_spread = Int32.MaxValue;
114                         string spread_day = "";
115                         while(r.Peek() > -1)
116                         {
117                                 string s = r.ReadLine();
118                                 Match fields = Regex.Match(s, pattern);
119                                 try {
120                                         string day = fields.Groups[1].ToString();
121                                         int min = Convert.ToInt32(fields.Groups[2].ToString());
122                                         int max = Convert.ToInt32(fields.Groups[3].ToString());
123                                         int spread = Math.Abs( max - min );
124                                         if (spread <= min_spread)
125                                         {
126                                                 spread_day = day;
127                                                 min_spread = spread;
128                                         }
129                                 } catch {
130                                 }
131                         }
132                         Console.WriteLine(spread_day);
133                 }
134         }
135 }
136 """]]
137
138
139  [[!tag CategoryCodeSnippets]]