2 Apr 2006 22:21

### [QUIZ] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```IMPORTANT: Please do not post solutions, hints, or other spoilers
until at least 60 hours after the date of this message.  Thanks.

Cooking often involves multiplication and rounding of fractions, at
least for those of us stuck under the tyranny of U.S. customary units.
For instance, to make oatmeal, I use 7 parts water to 4 parts
thick-cut rolled oats.  If I have 1 2/3 cups oats, about how much
water should I use?  5/3 * 7/4 = 35/12 = ~3 cups.

Your task: create a script that prompts for a ratio of two integers
and a quantity given as a whole number and/or a fraction (e.g. "1
3/5", "12", "3/7") and print out a rounded result of multiplying the
quantity by the ratio in the same format.

The result should be rounded to the nearest half, third, or fourth,
whichever is most accurate (in cases exactly between two quantities,
using the lowest denominator).  You may assume none of the integers are
overly large.

```
5 Apr 2006 09:05

### Re: [QUIZ] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```Hi All,

Here's my suggestion.

Cheers,
Sven

#!/usr/bin/perl -w
#
# Name:         quiz_fractions.pl
#
# Description:   Your task: create a script that prompts for a ratio of two integers and a quantity
#                given as a whole number and/or a fraction (e.g. "1 3/5", "12", "3/7") and print out
#                a rounded result of multiplying the quantity by the ratio in the same format.
#
#                The result should be rounded to the nearest half, third, or fourth, whichever is most
#                accurate (in cases exactly between two quantities, using the lowest denominator).  You
#                may assume none of the integers are overly large.
#
# Author:       Sven Hergenhahn
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use strict;

# set a lookup hash for the remainder
my %fractions = (0 => '0', 1/4 => '1/4', 1/3 => '1/3', 1/2 => '1/2', 2/3 => '2/3', 3/4 => '3/4', 1 => '1');

# Get input (not checked here!)
print 'Please enter a fraction: ';
my \$frac_in = <STDIN>; chomp \$frac_in;
```

5 Apr 2006 13:24

### [Solution] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```Hi,

I have written a solution to the running quest of the week (rounded
fractions).
I hope I am not too soon with this solution.

My solution has six functions which I want to introduce here shortly:

1) sub main

This function is called at the very beginning of the program with the given
command line arguments in  <at> ARGV.

If there are not exactly two command line arguments, the program asks the
user
for input, just as it was asked for in the decription of the qotw.

If there is usefull input, the calculation in the function calculate is
called
and the result is printed.

2) sub calculate

It gets the quantity and the multiplier as parameters.
It analyses both of them with help of the function analyse and then
multiplies
them with the function multiply.

At last it constructs the result string dependig on the value of the result.
This can be "1", "2/3" or "4 1/3". Or "0", don't miss this case
```

5 Apr 2006 13:33

### Re: [Solution] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```On Wed, Apr 05, 2006 at 01:24:45PM +0200, "Christian D?hl" wrote:

>I have written a solution to the running quest of the week (rounded
>fractions).

It does more or less the same as my version, except that I stored them
internally to vulgar fractions rather than keeping a separate
whole-number part.

R

```
5 Apr 2006 17:23

### Re: [QUIZ] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```Solution is attached.

--

--
ROCK ON code warrior!!!
```
Attachment (round.pl): text/x-perl, 3484 bytes
5 Apr 2006 20:22

### Re: [QUIZ] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```#!/usr/bin/perl -w

use strict;

my %mean = (
0           => '0',
( 1/4 +  0  ) / 2 => '1/4',
( 1/3 + 1/4 ) / 2 => '1/3',
( 1/2 + 1/3 ) / 2 => '1/2',
( 2/3 + 1/2 ) / 2 => '2/3',
( 3/4 + 2/3 ) / 2 => '3/4',
);

# IO::Prompt would be well used here, but I'm assuming we're not using
anything not in the core
my \$got_first = my \$got_second = 0;

my ( \$op1, \$op2 );

while ( ! \$got_first ) {
print "\nPlease enter first fraction: ";
chomp ( \$op1 = <STDIN> );
\$got_first = \$op1 =~ m!^\d*\s+?\d+/\d+\$!;
print "Whole numbers or fractions only, please." unless \$got_first;
}

while ( ! \$got_second ) {
print "\nPlease enter second fraction: ";
chomp ( \$op2 = <STDIN> );
\$got_second = \$op2 =~ m!^\d*\s+?\d+/\d+\$!;
```

6 Apr 2006 11:11

### Re: [QUIZ] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```Alan Young writes:

> # IO::Prompt would be well used here, but I'm assuming we're not using
> anything not in the core

Why are you assuming that?  Given that Cpan is often quoted as one of
Perl's great strengths this would seem a very limiting, and artifical,
constraint.

Smylers

```
6 Apr 2006 18:42

### Re: [QUIZ] Perl 'Easy' Quiz of the Week #2006-04-02 - Rounded Fractions

```Here's my solution:

#!/usr/bin/perl

use strict;
use warnings;

eval {
main ();
};
if ( my \$err = \$ <at>  ) {
print STDERR "# \$0: \$err\n";
exit 1 ;
}
exit 0;

sub main {
my \$in_frac1 = shift  <at> ARGV or die "need two inputs";
my \$in_frac2 = shift  <at> ARGV or die "need two inputs";

my \$dec1 = frac2dec( \$in_frac1 );
my \$dec2 = frac2dec( \$in_frac2 );
my \$frac = dec2frac( \$dec1 * \$dec2 );

print "\$in_frac1 X \$in_frac2 = \$frac\n",
"\$dec1 X \$dec2 = ", \$dec1 * \$dec2, "\n";
}

sub dec2frac {
my \$dec = shift () or die "no input in dec2frac";
```