sourcelibs/Check/demos/grading.xtl
1⍝!/usr/bin/env xetal
2⍝│ Grading a function with Check: a student's median, tested against
3⍝│ cases with known answers. Run with "just demo Check grading".
4
5"ch:" u̲se< "Check"
6
7⍝ The student's attempt: sort, then take the middle item.
8ᵘm̲edian ← { v → s ← s̲ort v◆ f̲irst (1 + (t̲ally v) d̲iv 2) s̲elect s }
9
10⍝ The cases. An even count has two middle items: the median is their
11⍝ mean, which the attempt misses.
12a ← "odd count" ᶜʰt̲est 3 ᶜʰn̲ear ᵘm̲edian 5 3 1
13b ← "unsorted" ᶜʰt̲est 4 ᶜʰn̲ear ᵘm̲edian 9 4 1 7 2
14c ← "one item" ᶜʰt̲est 7 ᶜʰn̲ear ᵘm̲edian 7
15d ← "even count" ᶜʰt̲est 2.5 ᶜʰn̲ear ᵘm̲edian 1 2 3 4 ⍝ shows failures
16ᶜʰr̲eport a ᶜʰa̲nd b ᶜʰa̲nd c ᶜʰa̲nd d