Soloway's recurring rainfall: Difference between revisions

Content added Content deleted
(Soloway's Recurring Rainfall in FreeBASIC)
m (→‎{{header|Raku}}: Add a Raku example)
Line 446: Line 446:


</syntaxhighlight>
</syntaxhighlight>

=={{header|Raku}}==
This task seems to be more about following (kind of vague) instructions and anticipating error conditions rather than solving a rather simple problem.

I notice that units are specified for neither measurement; time nor rainfall accumulation. Time is assumed to always increment in units of one, rainfall in some non-negative integer. "Integer" gets a little leeway in this entry. Floating point numbers or Rational numbers that are exactly equal to an Integer are allowed, even if they are technically not integers.

On cursory observation this entry seems to needlessly duplicate the output line, but the second is there to accurately summarize the totals on program exit, even if no entries have been made, and to demonstrate that 999999 does '''not''' get added into the average.
<syntaxhighlight lang="raku" line># Write a program that will read in integers and
# output their average. Stop reading when the
# value 99999 is input.


my ($periods, $accumulation, $rainfall) = 0, 0;

loop {
loop {
$rainfall = prompt 'Integer units of rainfall in this time period? (999999 to finalize and exit)>: ';
last if $rainfall.chars and $rainfall.Numeric !~~ Failure and $rainfall.narrow ~~ Int and $rainfall ≥ 0;
say 'Invalid input, try again.';
}
last if $rainfall == 999999;
++$periods;
$accumulation += $rainfall;
printf "Average rainfall %.2f units over %d time periods.\n", $accumulation / $periods, $periods;
}
</syntaxhighlight>
{{out}}
<pre># Normal operation
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 0
Average rainfall 0.00 units over 1 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 2
Average rainfall 1.00 units over 2 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 4
Average rainfall 2.00 units over 3 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 6
Average rainfall 3.00 units over 4 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 8
Average rainfall 4.00 units over 5 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 999999
Average rainfall 4.00 units over 5 time periods.


# Invalid entries and valid but not traditional "Integer" entries demonstrated
Integer units of rainfall in this time period? (999999 to finalize and exit)>: a
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 1.1
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>:
Invalid input, try again.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 2.0
Average rainfall 2.00 units over 1 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: .3e1
Average rainfall 2.50 units over 2 time periods.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 999999
Average rainfall 2.50 units over 2 time periods.


# Valid summary with no entries demonstrated.
Integer units of rainfall in this time period? (999999 to finalize and exit)>: 999999
Average rainfall 0.00 units over 0 time periods.</pre>


=={{header|Rust}}==
=={{header|Rust}}==