This is my response to The Weekly Challenge #401.
Input: $expr = "1/2+3/2"
Output: "2/1"
1/2 + 3/2 => 4/2 => 2
As specified by the rules, an integer result must be represented as a
fraction with denominator 1, yielding "2/1".
Example 2:
Input: $expr = "-5/2-3/2"
Output: "-4/1"
-5/2 - 3/2 => -8/2 => -4 => "-4/1".
Example 3:
Input: $expr = "5/3-5/3"
Output: "0/1"
5/3 - 5/3 => 0/3
Zero divided by any valid non-zero denominator reduces to "0/1".
Example 4:
Input: $expr = "10/10-1/10+2/5"
Output: "13/10"
10/10 - 1/10 + 2/5 => 9/10 + 4/10 => 13/10.
13/10 is already irreducible, so the output is "13/10".
Example 5:
Input: $expr = "1/6+1/6+1/6"
Output: "1/2"
1/6 + 1/6 + 1/6 => 3/6 => 1/2
This would normally require keeping track of the denomiators manually,
but Raku's Numeric type Num keeps track of that for us automatically.
#! /usr/bin/env raku
unit sub MAIN (Str $expr where $expr
~~ /^ "-"? \d+ \/ \d+ [ <[+-]> \d+ \/ \d+ ] * $/,
:v(:$verbose));
my @parts = $expr.comb(/ <[+-]>? \d+ \/ \d+ /)
.map({ my ($n, $d) = .split('/'); $n / $d; });
say ": Fractions: { @parts>>.nude>>.join("/").join(" + ") }"
if $verbose;
say @parts.sum.nude.join("/");
[3] Ensure legal input, a list of fractions with + or - between
them. The first one can have an optional minus sign in front.
[7] Split the input into a list of strings, one for each fraction (with
comb). Then we use map on each string to get the two parts (the
numerator and denominator), and finally we do the division to calculate the
value.
[13] Add the fractions, which are of the Num type, extract
the numerator and denominator (as two values) with nude, join them with
an /, and print the resulting string.
See docs.raku.org/routine/nude for more information about nude.
Running it:
$ ./fractional-addition-substraction "1/2+3/2"
2/1
$ ./fractional-addition-substraction -- "-5/2-3/2"
-4/1
$ ./fractional-addition-substraction "5/3-5/3"
0/1
$ ./fractional-addition-substraction "10/10-1/10+2/5"
13/10
$ ./fractional-addition-substraction "1/6+1/6+1/6"
1/2
Looking good.
With verbose mode:
$ ./fractional-addition-substraction -v "1/2+3/2"
: Fractions: 1/2 + 3/2
2/1
$ ./fractional-addition-substraction -v -- "-5/2-3/2"
: Fractions: -5/2 + -3/2
-4/1
$ ./fractional-addition-substraction -v "5/3-5/3"
: Fractions: 5/3 + -5/3
0/1
$ ./fractional-addition-substraction -v "10/10-1/10+2/5"
: Fractions: 1/1 + -1/10 + 2/5
13/10
$ ./fractional-addition-substraction -v "1/6+1/6+1/6"
: Fractions: 1/6 + 1/6 + 1/6
1/2
Input: $str = "zroezerotwotwotwo"
Output: "00222"
'z' appears two times => two "zero"
'w' appears three times => three "two"
Example 2:
Input: $str = "sveeeinght"
Output: "78"
'g' appears one time => one "eight"
Removing the letters of "eight" leaves "sveen".
Now 'v' appears one time => one "seven"
Example 3:
Input: $str = "onenineninefive"
Output: "1599"
'f' appears one time => one "five"
After removing "five", 'o' appears one time => one "one".
The remaining letters are "ninenine", where 'i' appears two times =>
two "nine".
Example 4:
Input: $str = "zeroonetwothreeeightfivesixsevenninefour"
Output: "0123456789"
The input contains the scrambled letters of every single digit from 0
through 9 exactly once:
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine".
Example 5:
Input: $str = "ninenineninenine"
Output: "9999"
Character counts: 'n' = 8, 'i' = 4, 'e' = 4.
Every set of letters matches "nine", giving four 9s.
#! /usr/bin/env raku
unit sub MAIN (Str $str where $str ~~ /^ <lower> + $/,
:v(:$verbose));
my %c = $str.comb.Bag;
my @digits;
while %c<z> && %c<e> && %c<r> && %c<o>
{
%c<z>--; %c<e>--; %c<r>--; %c<o>--;
@digits.push: 0;
say ": Extracted zero" if $verbose;
}
while %c<o> && %c<n> && %c<e>
{
%c<o>--; %c<n>--; %c<e>--;
@digits.push: 1;
say ": Extracted one" if $verbose;
}
while %c<t> && %c<w> && %c<o>
{
%c<t>--; %c<w>--; %c<o>--;
@digits.push: 2;
say ": Extracted two" if $verbose;
}
while %c<t> && %c<h> && %c<r> && %c<e> && %c<e> > 1
{
%c<t>--; %c<h>--; %c<r>--; %c<e>-=2;
@digits.push: 3;
say ": Extracted three" if $verbose;
}
while %c<f> && %c<o> && %c<u> && %c<r>
{
%c<f>--; %c<o>--; %c<u>--; %c<r>--;
@digits.push: 4;
say ": Extracted four" if $verbose;
}
while %c<f> && %c<i> && %c<v> && %c<e>
{
%c<f>--; %c<i>--; %c<v>--; %c<e>--;
@digits.push: 5;
say ": Extracted five" if $verbose;
}
while %c<s> && %c<i> && %c<x>
{
%c<s>--; %c<i>--; %c<x>--;
@digits.push: 6;
say ": Extracted six" if $verbose;
}
while %c<s> && %c<e> && %c<e> > 1 && %c<v> && %c<n>
{
%c<s>--; %c<e>-=2; %c<v>--; %c<n>--;
@digits.push: 7;
say ": Extracted seven" if $verbose;
}
while %c<e> && %c<i> && %c<g> && %c<h> && %c<t>
{
%c<e>--; %c<i>--; %c<g>--; %c<h>--; %c<t>--;
@digits.push: 8;
say ": Extracted eight" if $verbose;
}
while %c<n> && %c<n> > 1 && %c<i> && %c<e>
{
%c<n>-=2; %c<i>--; %c<e>--;
@digits.push: 9;
say ": Extracted nine" if $verbose;
}
say ": Junk: { %c.grep({ .value > 0}).map({ .key x .value}).
sort.join }" if $verbose;
say @digits.join;
[3] Ensure lowercase letters only, using the Unicode definition of letters.
[6] Count the different letters.
[8] The result will end up here.
The blocks for each digit are built up the same way, so let us look at the one for zero (no repeated characters) and 3 (repeated characters) only.
[10] As long as we have at least one set of letters to form «zero»,
[12] Remove those letters from the count.
[13] Add the digit to the result.
[31] Three has two «e»s, so we must have at least two of them left.
[33] And we have to remove two of them as well.
[80] Report any leftover letters, but only in verbose mode.
Neither the challenge nor the examples say what to do with leftover letters, so I have chosen to ignore them.
[83] The digits are pre sorted, so joining them is fine.
Running it:
$ ./reconstruct-digits zroezerotwotwotwo
00222
$ ./reconstruct-digits sveeeinght
78
$ ./reconstruct-digits onenineninefive
1599
$ ./reconstruct-digits zeroonetwothreeeightfivesixsevenninefour
0123456789
$ ./reconstruct-digits ninenineninenine
9999
Looking good.
With verbose mode:
$ ./reconstruct-digits -v zroezerotwotwotwo
: Extracted zero
: Extracted zero
: Extracted two
: Extracted two
: Extracted two
00222
$ ./reconstruct-digits -v sveeeinght
: Extracted eight
: Extracted seven
78
$ ./reconstruct-digits -v onenineninefive
: Extracted five
: Extracted one
: Extracted nine
: Extracted nine
1599
$ ./reconstruct-digits -v zeroonetwothreeeightfivesixsevenninefour
: Extracted zero
: Extracted two
: Extracted four
: Extracted six
: Extracted eight
: Extracted five
: Extracted three
: Extracted seven
: Extracted one
: Extracted nine
0123456789
$ ./reconstruct-digits -v ninenineninenine
: Extracted nine
: Extracted nine
: Extracted nine
: Extracted nine
9999
Some examples with leftover letters:
$ ./reconstruct-digits -v zro
: Junk: orz
$ ./reconstruct-digits -v zorroevafu
: Extracted zero
: Extracted four
: Junk: av
04
And that's it.