temp_stats.pas in Delphi
A week of daily highs reduced to coolest, warmest and mean, with a bar chart per day.
program TempStats;
uses SysUtils;
const
Days: array[0..6] of string =
('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
Highs: array[0..6] of Double =
(18.5, 21.0, 24.2, 27.8, 26.1, 19.4, 16.9);
// One hash per whole degree above the coolest day of the week.
function Bar(Value, Floor: Double): string;
var
Width: Integer;
begin
Width := Round(Value - Floor);
if Width < 0 then
Width := 0;
Result := StringOfChar('#', Width);
end;
var
I, WarmDays: Integer;
Sum, Mean, Coolest, Warmest: Double;
Mark: string;
begin
Sum := 0;
Coolest := Highs[0];
Warmest := Highs[0];
WarmDays := 0;
for I := 0 to High(Highs) do
begin
Sum := Sum + Highs[I];
if Highs[I] < Coolest then
Coolest := Highs[I];
if Highs[I] > Warmest then
Warmest := Highs[I];
end;
Mean := Sum / Length(Highs);
WriteLn(Format('%-4s %6s %3s %s', ['day', 'high', 'vs', 'chart']));
for I := 0 to High(Highs) do
begin
if Highs[I] >= Mean then
begin
Mark := '+';
Inc(WarmDays);
end
else
Mark := '-';
WriteLn(Format('%-4s %6.1f %3s %s',
[Days[I], Highs[I], Mark, Bar(Highs[I], Coolest)]));
end;
WriteLn(StringOfChar('-', 34));
WriteLn(Format('mean %.1fC over %d days, spread %.1fC',
[Mean, Length(Highs), Warmest - Coolest]));
WriteLn(Format('%d days at or above the mean, coolest %.1fC',
[WarmDays, Coolest]));
end.
How it works
- One pass over
HighsbuildsSumand tracksCoolestandWarmestas it goes. Barturns the gap aboveCoolestinto hashes, clampingWidthto zero on the coolest day.- Days at or above
Meantake a+mark and count intoWarmDaysfor the closing summary.
Keywords and builtins used here
BarDoubleFloorFormatHighIncIntegerLengthMeanResultRoundStringOfCharSumWriteLnarraybeginconstdoelseendforfunctionifofprogramstringthentousesvar
The run, in numbers
- Lines
- 61
- Characters to type
- 1278
- Tokens
- 401
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 301 seconds.
Step 3 of 3 in Encore, step 27 of 27 in Language basics.