Skip to content

Instantly share code, notes, and snippets.

@tomatrow
Created October 30, 2017 02:08
Show Gist options
  • Save tomatrow/dd17aa6e230246b0fdaf7585020503c9 to your computer and use it in GitHub Desktop.
Save tomatrow/dd17aa6e230246b0fdaf7585020503c9 to your computer and use it in GitHub Desktop.
name date gender period grams ethnicity
0 1 50 25 10 15
conrad 2 girl pm 3175.15 greek
nic 2 girl am 3373.59 nic
alisha 2 girl pm 3685.44 white
brad 3 boy am 4592.62 greek
aj 4 girl am 2409.71 asian
#! /usr/local/bin/fish
set file 'answers.txt'
set keys (cat $file | head -n 1 | tr -s ' ' | string split ' ')
set weights (cat $file | sed 1d | head -n 1 | tr -s ' ' | string split ' ')
for i in (seq 1 (count $keys))
dict.set keyweight\[$keys[$i]\] $weights[$i]
end
for line in (cat $file | sed '1,2d')
# For some reasone, using sed gives me a bunch of blank lines.
if test (echo $line | string length) -eq 0
continue
end
set answers (echo $line | tr -s ' ' | string split ' ')
set names $names $answers[1]
for i in (seq 1 (count $keys))
# name[key] value
dict.set $answers[1]\[$keys[$i]\] $answers[$i]
end
end
function max -a x y
if math -s10 "$x <= $y" > /dev/null
echo $y
else
true # reset status
echo $x
end
end
function abs -a x
if math -s10 "$x >= 0" >/dev/null
echo $x
else
true
math -s10 "$x * (-1)"
end
end
function distance -a x y
set difference (math -s10 "$x - $y")
set dist (abs $difference)
echo $dist
end
set true_weight 3000
set max_dist 0
for name in $names
set guess (dict.get $name\[grams\])
set dist (distance $guess $true_weight)
set max_dist (max $max_dist $dist)
end
function score -a field answer
switch $field;
case name
echo 0.0
case gender;
switch $answer;
case boy;
echo 1.0
case girl;
echo 0.0
end
case period;
switch $answer;
case am;
echo 1.0
case pm;
echo 0.0
end
case grams;
# we will normalize it
# let max_dist = max |guess - true_weight| where guess ranges over our weight guesses
# return $answer / $max_dist
set dist (distance $answer $true_weight)
math -s10 "$dist / $max_dist"
case ethnicity;
switch $answer;
case asian;
echo 0.5
case nic;
echo 0.0
case greek;
echo 0.4
case white;
echo 0.3
end
end
end
function babyscore
for name in $names
set sum 0
for key in $keys
set answer (dict.get $name\[$key\])
set scr (score $key $answer)
set weight (dict.get keyweight[$key])
set part (math "$scr * $weight")
set sum (math "$sum + $part")
end
echo $name $sum
end
end
babyscore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment