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ᵏ⁼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