typestar

temp_stats.odin in Odin

A week of daily highs reduced to low, high and mean, with a scaled bar per day.

// One week of daily highs: spread, mean, and a scaled bar chart.
package main

import "core:fmt"

days := [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
temps := [7]int{61, 68, 74, 79, 72, 65, 58}

main :: proc() {
    lo, hi, sum := temps[0], temps[0], 0
    for t in temps {
        lo = min(lo, t)
        hi = max(hi, t)
        sum += t
    }
    mean := f32(sum) / len(temps)
    span := max(hi - lo, 1)

    fmt.println("day temp range                 diff")
    fmt.println("------------------------------------")
    // Bars scale to the week's spread; the +1 keeps Sunday visible.
    bars := "#####################"
    warm_days := 0
    for t, i in temps {
        width := 20 * (t - lo) / span + 1
        diff := f32(t) - mean
        if diff > 0 do warm_days += 1
        sign := "+" if diff >= 0 else ""
        fmt.printfln("%s  %d  %-21s %s%.1f", days[i], t, bars[:width],
            sign, diff)
    }
    fmt.println("------------------------------------")
    fmt.printfln("low %d, high %d, mean %.1f", lo, hi, mean)
    fmt.println("days above the mean:", warm_days)
}

How it works

  1. One pass tracks the coolest and warmest days with builtin min and max.
  2. Bars scale to the spread by slicing a string of hashes.
  3. The ternary "+" if diff >= 0 else "" picks the sign by hand.

Keywords and builtins used here

The run, in numbers

Lines
35
Characters to type
968
Tokens
273
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 252 seconds.

Type this snippet

Step 3 of 3 in Encore, step 27 of 27 in Language basics.

← Previous