temp_stats.pas en Delphi
Una semana de máximas diarias reducida a la más fría, la más cálida y la media, con barras por día.
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.
Cómo funciona
- Una sola pasada por
HighsarmaSumy va siguiendoCoolestyWarmest. Barconvierte en numerales la diferencia sobreCoolest, y fijaWidthen cero el día más frío.- Los días en o sobre
Meanllevan una marca+y cuentan enWarmDayspara el resumen final.
Palabras clave y builtins usados aquí
DoubleFloorFormatHighIncIntegerLengthMeanResultRoundStringOfCharSumWriteLnarraybeginconstdoelseendforfunctionifofprogramstringthentousesvar
El intento, en números
- Líneas
- 61
- Caracteres a escribir
- 1278
- Tokens
- 401
- Ritmo de tres estrellas
- 80 tpm
Al ritmo de tres estrellas de 80 tokens por minuto, este intento toma unos 301 segundos.
Paso 3 de 3 en Bis; paso 27 de 27 en Fundamentos del lenguaje.