Skip to content

Commit 443a1d2

Browse files
ClémentClément
authored andcommitted
Improved solution for TempStats.
1 parent 4e23312 commit 443a1d2

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

source/code/projects/TempStats/TempStats/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static void Main()
2222
{
2323
Console.WriteLine(ex.Message);
2424
}
25+
2526
try
2627
{
2728
double[] coldest = { -1000000 };
@@ -34,6 +35,7 @@ static void Main()
3435
{
3536
Console.WriteLine(ex.Message);
3637
}
38+
3739
try
3840
{
3941
double[] improper = { 10, -5, 100 }; // This data is not sorted.
@@ -47,6 +49,19 @@ static void Main()
4749
Console.WriteLine(ex.Message);
4850
}
4951

52+
try
53+
{
54+
double[] empty = null;
55+
TempStats emptyTemperatures = new TempStats(
56+
empty,
57+
"Empty"
58+
);
59+
}
60+
catch (Exception ex)
61+
{
62+
Console.WriteLine(ex.Message);
63+
}
64+
5065
try
5166
{
5267
Console.WriteLine("Mode: " + test.Mode);

source/code/projects/TempStats/TempStats/TempStats.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
class TempStats
44
{
5+
private const double LOWEST_TEMPERATURE = -128.6;
6+
7+
// https://en.wikipedia.org/wiki/Lowest_temperature_recorded_on_Earth
8+
private const double HIGHEST_TEMPERATURE = 134.1;
9+
10+
// https://en.wikipedia.org/wiki/Highest_temperature_recorded_on_Earth
11+
512
public string Description { get; set; }
613
private double[] temp;
714
public double[] Temp
@@ -11,34 +18,44 @@ public double[] Temp
1118
bool sortedSoFar = true;
1219
int index = 0;
1320

21+
if (value == null)
22+
{
23+
throw new ArgumentException(
24+
"Array of temperatures cannot be null."
25+
);
26+
}
27+
1428
while (index + 1 < value.Length && sortedSoFar)
1529
{
1630
if (value[index] > value[index + 1])
1731
sortedSoFar = false;
1832
index++;
1933
}
34+
2035
if (!sortedSoFar)
2136
{
2237
throw new ArgumentException(
2338
"Your data is not sorted."
2439
);
2540
}
26-
foreach (double i in value)
41+
42+
// At this point, we know the array is sorted (increasingly).
43+
// We can check only the first value against LOWEST_TEMPERATURE,
44+
// since we know all other values are greater.
45+
// Similarly, we can check only the last value against HIGHEST_TEMPERATURE.
46+
if (value[0] < LOWEST_TEMPERATURE)
2747
{
28-
if (i < -128.6)
29-
{
30-
throw new ArgumentOutOfRangeException(
31-
"That is colder than the coldest ever recorded on Earth!"
32-
);
33-
// https://en.wikipedia.org/wiki/Lowest_temperature_recorded_on_Earth
34-
}
35-
else if (i > 134.1)
36-
{
37-
throw new ArgumentOutOfRangeException(
38-
"That is hotter than the hottest ever recorded on Earth!"
39-
);
40-
// https://en.wikipedia.org/wiki/Highest_temperature_recorded_on_Earth
41-
}
48+
throw new ArgumentOutOfRangeException(
49+
"That is colder than the coldest ever recorded on Earth!"
50+
);
51+
}
52+
else if (
53+
value[value.Length - 1] > HIGHEST_TEMPERATURE
54+
)
55+
{
56+
throw new ArgumentOutOfRangeException(
57+
"That is hotter than the hottest ever recorded on Earth!"
58+
);
4259
}
4360
temp = value;
4461
}

0 commit comments

Comments
 (0)