Jump to content

Averages/Arithmetic mean: Difference between revisions

Fix wrong use of 'tee' in [[UnixPipes. Add UNIX Shell
No edit summary
(Fix wrong use of 'tee' in [[UnixPipes. Add UNIX Shell)
Line 1,360:
 
<lang ti89b>Define rcmean(nums) = when(dim(nums) = 0, 0, mean(nums))</lang>
 
=={{header|Trith}}==
<lang trith>: mean dup empty? [drop 0] [dup [+] foldl1 swap length /] branch ;
 
[3 1 4 1 5 9] mean</lang>
 
=={{header|UnixPipes}}==
''Caution:'' This solution overwrites the file named <tt>count</tt> in the current directory.
 
<lang bash>term() {
b=$1;res=$2
echo "scale=5;$res+$b" | bc
}
}</lang>
 
<lang bash>sum() {
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
}
}</lang>
 
<lang bash>fold() {
func=$1
(while read a ; do
fold $func | $func $a
done)
}
}</lang>
 
<lang bash>mean() {
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(catwc -l < count) ") * " | bc
}
}</lang>
 
<lang bash>(echo 3; echo 1; echo 4) | mean</lang>
 
=={{header|TrithUNIX Shell}}==
This example uses <tt>expr</tt>, which only works with integers. This example checks that each string in the list is an integer.
<lang trith>: mean dup empty? [drop 0] [dup [+] foldl1 swap length /] branch ;
 
<lang bash>mean() {
[3 1 4 1 5 9] mean</lang>
if expr $# >/dev/null; then
(count=0
sum=0
while expr $# \> 0 >/dev/null; do
sum=`expr $sum + "$1"`
result=$?
expr $result \> 1 >/dev/null && exit $result
 
count=`expr $count + 1`
shift
done
expr $sum / $count) || exit $?
else
echo 0
fi
}
 
printf "test 1: "; mean # 0
printf "test 2: "; mean 300 # 300
printf "test 3: "; mean 300 100 400 # 266
printf "test 4: "; mean -400 200 # -100
printf "test 5: "; mean - # expr: syntax error</lang>
 
=={{header|Ursala}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.