Sum to 100: Difference between revisions

→‎{{header|UNIX Shell}}: Improve formatting of output, add version requirements.
(→‎{{header|UNIX Shell}}: Add implementation.)
(→‎{{header|UNIX Shell}}: Improve formatting of output, add version requirements.)
Line 6,059:
 
=={{header|UNIX Shell}}==
{{works with|Korn Shell|93+}}
{{works with|Bourne Again SHell|4.0+}}
{{works with|Z Shell|4.0+}}
 
<syntaxhighlight lang=bash>sumto100() {
typeset expr sum max_count=0 i n sum max_values i
typeset -A histogram
printf 'Strings that evaluate to 100:\n'
for nexpr in {,-}1{,+,-}2{,+,-}3{,+,-}4{,+,-}5{,+,-}6{,+,-}7{,+,-}8{,+,-}9
do
(( sum = nexpr ))
if (( sum == 100 )); then
printf '\t%s\n' "$nexpr"
fi
histogram[$sum]=$(( ${histogram[$sum]:-0}+1 ))
Line 6,081:
fi
done
echoprintf "Most'\nMost solutions for any number is $max_count,%d: for' "${max_values[@]}max_count"
if [[ -n $ZSH_VERSION ]]; then
printf '%s\n\n' "${(j:, :)max_values}"
else
printf '%s' "${max_values[0]}"
printf ', %s' "${max_values[@]:1}"
printf '\n\n'
fi
 
for (( i=1; i<123456789; ++i )); do
if (( !histogram[$i] )); then
echoprintf "Lowest positive sum that can't be expressed: %d\n\n" "$i"
break
fi
Line 6,090 ⟶ 6,098:
printf 'Ten highest reachable numbers:\n';
if [[ -n $ZSH_VERSION ]]; then
printf '\t%s9d\n' "${(k@)histogram}"
else
printf '\t%s9d\n' "${!histogram[@]}"
fi | sort -nr | head -n 10
}
 
sumto100</syntaxhighlight>
</syntaxhighlight>
{{Out}}
<pre>Strings that evaluate to 100:
123+45-67+8-9
123+4-5+67-89
123-45-67+89
123-4-5-6-7+8-9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
1+23-4+56+7+8+9
1+23-4+5+6+78-9
1+2+34-5+67-8+9
1+2+3-4+5+6+78+9
-1+2-3+4+5+6+78+9
 
Most solutions for any number is 46, for: 9, -9
 
Lowest positive sum that can't be expressed: 211
 
Ten highest reachable numbers:
123456789
23456790
23456788
12345687
12345669
3456801
3456792
3456790
3456788
3456786
 
</pre>
 
=={{header|Visual Basic .NET}}==
Of course, one could just code-convert the existing C# example, but I thought this could be written with some simpler constructs. The point of doing this is to make the code more compatible with other BASIC languages. Not every language has something similar to the <i>Enumerable Range</i> construct. I also found the <i>Dictionary</i> construct could be implemented with something more primitive.
1,480

edits