Solving coin problems: Difference between revisions

m
(Added Go)
m (→‎{{header|Wren}}: Minor tidy)
 
(19 intermediate revisions by 5 users not shown)
Line 29:
 
=={{header|Go}}==
This relatively simple program can only solve problems with 2 types of coins (or other objects) andor consequently3 doesn'ttypes of coins (but not other objects) without the need for an equation solver. However, it is able to solve theall 1928 problems of thisthese typetypes which were originally listed in the Perl entry before it was restricted to the subset of those problems (24) involving coins and bills.
<langsyntaxhighlight lang="go">package main
 
import (
"fmt"
"math"
"regexp"
"sort"
Line 44 ⟶ 45:
value float64
number int
}
 
// variable1 = constant1 * variable2 + constant2
type relation struct {
variable1 string
variable2 string
constant1 float64
constant2 float64
}
 
var nums = map[string]string{
"zeroone-half": "0 times", "one": "1", "two": "2", "three": "3", "four": "4", "five": "5",
"six": "6", "seven": "7", "eight": "8", "nine": "9", "ten": "10", "eleven": "11", "twelve": "12",
"thirteen": "13", "fourteen": "14", "fifteen": "15", "sixteen": "16", "seventeen": "17",
Line 59 ⟶ 68:
 
var coins = map[string]float64{
"pennies": 0.01, "nickels": 0.05, "dimes": 0.10, "quarters": 0.25, "half-dollardollars": 0.50,
"one-dollar": 1.00, "two-dollar": 2.00, "five-dollar": 5.00, "ten-dollar": 10.00}
 
var bills = map[string]string{
"$1": "one-dollar", "$2": "two-dollar", "$5": "five-dollar", "$10": "ten-dollar"}
 
var (
Line 66 ⟶ 78:
rx2 = regexp.MustCompile(`\b(pennies|nickels|dimes|quarters|half-dollar|one-dollar|two-dollar|five-dollar|ten-dollar)\b`)
rx3 = regexp.MustCompile(`\s(\d+)\s`)
rx4 = regexp.MustCompile(`(\d+) times as many ([-\w]+) as (s?he (does|has) )?([-\w]+)`)
rx5 = regexp.MustCompile(`(\d+) more ([-\w]+) than (s?he (does|has) )?([-\w]+)`)
rx6 = regexp.MustCompile(`(\d+) less ([-\w]+) than (s?he (does|has) )?([-\w]+)`)
rx7 = regexp.MustCompile(`(\d+) dollars`)
)
Line 115 ⟶ 127:
 
// Gets the 'kinds' for the problem.
func getKinds(amasa [][]string) (int, []kind) {
a := amas[0]
num, _ := strconv.Atoi(a[1])
kinds := []kind{{a[2], 0, 0}, {a[45], 0, 0}}
areCoins := false
for i := range kinds {
Line 130 ⟶ 141:
}
return num, kinds
}
 
// Checks if the problem involves 3 coins and
// also returns their names and the names of the coin which occurs most.
func hasThreeCoins(q string) ([]string, []string, bool) {
q = strings.ReplaceAll(q, ".", "")
q = strings.ReplaceAll(q, ",", "")
words := strings.Split(q, " ")
coinMap := make(map[string]int)
for _, word := range words {
if _, ok := coins[word]; ok {
coinMap[word]++
}
}
if len(coinMap) != 3 {
return nil, []string{}, false
}
maxNum := 0
var maxNames []string
var names []string
for k, v := range coinMap {
names = append(names, k)
if v > maxNum {
maxNum = v
maxNames = maxNames[:0]
maxNames = append(maxNames, k)
} else if v == maxNum {
maxNames = append(maxNames, k)
}
}
return names, maxNames, true
}
 
// Processes a problem which involves 3 coins.
func threeCoins(p, q string, names, maxNames []string) {
var relations []relation
am := rx4.FindAllStringSubmatch(q, -1)
for i := 0; i < len(am); i++ {
mult, kinds := getKinds(am[i])
relations = append(relations, relation{kinds[0].name, kinds[1].name, float64(mult), 0})
}
mt := rx5.FindAllStringSubmatch(q, -1)
for i := 0; i < len(mt); i++ {
plus, kinds := getKinds(mt[i])
relations = append(relations, relation{kinds[0].name, kinds[1].name, 1, float64(plus)})
}
lt := rx6.FindAllStringSubmatch(q, -1)
for i := 0; i < len(lt); i++ {
minus, kinds := getKinds(lt[i])
relations = append(relations, relation{kinds[0].name, kinds[1].name, 1, -float64(minus)})
}
le := len(relations)
if le > 2 {
errorMsg(p)
return
}
if le == 0 { // numbers of each coin must be the same
sum := 0.0
for _, name := range names {
sum += coins[name]
}
res := getValues(q)
tv := res[len(res)-1]
n := int(tv/sum + 0.5)
var kinds []kind
for _, name := range names {
kinds = append(kinds, kind{name, 0, n})
}
printAnswers(p, kinds)
} else {
res := getValues(q)
totalValue := res[len(res)-1]
for _, maxName := range maxNames {
for i := 0; i < le; i++ {
if relations[i].constant1 == 0 {
relations[i].constant1 = 0.5 // deals with 'one-half' cases
}
if le == 2 && maxName == relations[i].variable1 {
v := relations[i].variable2
relations[i].variable1, relations[i].variable2 = v, maxName
relations[i].constant1 = 1 / relations[i].constant1
relations[i].constant2 = -relations[i].constant2
}
}
tv := totalValue
var v1, v2, v3 string
var n1, n2, n3 int
if le == 2 {
tmc := coins[relations[0].variable1]*relations[0].constant1 +
coins[relations[1].variable1]*relations[1].constant1 + coins[maxName]
tv -= coins[relations[0].variable1]*relations[0].constant2 +
coins[relations[1].variable1]*relations[1].constant2
v1, v2, v3 = maxName, relations[0].variable1, relations[1].variable1
n1 = int(tv/tmc + 0.5)
n2 = int(relations[0].constant1*float64(n1) + relations[0].constant2 + 0.5)
n3 = int(relations[1].constant1*float64(n1) + relations[1].constant2 + 0.5)
} else {
res2 := getNumbers(q)
tn := float64(res2[len(res2)-1])
v1, v2 = relations[0].variable1, relations[0].variable2
for _, name := range names {
if name != v1 && name != v2 {
v3 = name
break
}
}
mult1, mult2, mult3 := coins[v1], coins[v2], coins[v3]
n2 = int(((tn-relations[0].constant2)*mult3-tv+relations[0].constant2*mult1)/
((relations[0].constant1+1)*mult3-relations[0].constant1*mult1-mult2) + 0.5)
n1 = int(float64(n2)*relations[0].constant1 + relations[0].constant2 + 0.5)
n3 = int(tn) - n1 - n2
}
calcValue := float64(n1)*coins[v1] + float64(n2)*coins[v2] + float64(n3)*coins[v3]
if math.Abs(totalValue-calcValue) <= 1e-14 {
kinds := []kind{kind{v1, 0, n1}, kind{v2, 0, n2}, kind{v3, 0, n3}}
printAnswers(p, kinds)
return
}
}
errorMsg(p)
}
return
}
 
func printAnswers(p string, kinds []kind) {
fmt.Println(p)
fmt.Print("ANSWER:")
fmt.Printf("ANSWER: %d %s, %d %s\n\n", kinds[0].number, kinds[0].name, kinds[1].number, kinds[1].name)
for i, kind := range kinds {
if i > 0 {
fmt.Print(",")
}
fmt.Printf(" %d %s", kind.number, kind.name)
}
fmt.Println("\n")
}
 
Line 148 ⟶ 288:
"A small child has 6 more quarters than nickels. If the total amount of coins is $3.00, find the number of nickels and quarters the child has.",
"A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.",
"A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.",
"In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.",
"A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps is $3.74, find the number of each type of the stamps purchased.",
"A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number of each sold.",
"An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and paid $15.71, how many reams of each type of paper did the student purchase?",
"A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?",
"A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?",
"A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?",
"A person bought ten greeting cards consisting of birthday cards costing $1.50 each and anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the number of each kind of card the person bought.",
"A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the number of dimes the person has.",
"A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.",
"A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.",
"Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?",
"A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?",
"A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?",
"Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?",
"Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?",
"In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?",
"Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?",
"A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine?",
"Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?",
"Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each type of coin did she have?",
Line 177 ⟶ 326:
}
for k, v := range nums {
q = strings.ReplaceAll(q, k+" ", v+" ")
}
for k, v := range bills {
q = strings.ReplaceAll(q, k+" ", v+" ")
}
q = strings.ReplaceAll(q, " bills", "")
q = strings.ReplaceAll(q, " bill", "")
// check if there are 3 coins involved
if names, maxNames, ok := hasThreeCoins(q); ok {
threeCoins(p, q, names, maxNames)
continue
}
am := rx4.FindAllStringSubmatch(q, -1)
if len(am) == 1 {
mult, kinds := getKinds(am[0])
if kinds == nil {
errorMsg(p)
Line 197 ⟶ 355:
mt := rx5.FindAllStringSubmatch(q, -1)
if len(mt) == 1 {
plus, kinds := getKinds(mt[0])
if kinds == nil {
errorMsg(p)
Line 212 ⟶ 370:
lt := rx6.FindAllStringSubmatch(q, -1)
if len(lt) == 1 {
minus, kinds := getKinds(lt[0])
if kinds == nil {
errorMsg(p)
Line 270 ⟶ 428:
printAnswers(p, kinds)
}
}</langsyntaxhighlight>
 
{{out}}
Line 285 ⟶ 443:
A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.
ANSWER: 21 nickels, 11 quarters
 
A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
ANSWER: 7 pennies, 14 dimes, 10 nickels
 
In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.
ANSWER: 3 half-dollars, 9 quarters, 9 dimes
 
A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps is $3.74, find the number of each type of the stamps purchased.
Line 294 ⟶ 458:
An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and paid $15.71, how many reams of each type of paper did the student purchase?
ANSWER: 7 $1.59 item, 2 $2.29 item
 
A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?
ANSWER: 5 five-dollar, 10 one-dollar, 4 ten-dollar
 
A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?
ANSWER: 5 dimes, 3 quarters
 
A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?
ANSWER: 3 nickels, 9 dimes, 8 pennies
 
A person bought ten greeting cards consisting of birthday cards costing $1.50 each and anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the number of each kind of card the person bought.
Line 305 ⟶ 475:
 
A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.
ANSWER: 5 $1.00 itemone-dollar, 15 $2.00 itemtwo-dollar
 
A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.
ANSWER: 17 nickels, 25 pennies, 20 dimes
 
Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?
ANSWER: 14 one-dollar, 12 quarters
 
A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?
ANSWER: 18 nickels, 6 quarters, 9 dimes
 
A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?
ANSWER: 9 pennies, 9 nickels, 9 dimes
 
Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?
Line 315 ⟶ 494:
Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?
ANSWER: 20 quarters, 18 dimes
 
In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?
ANSWER: 8 one-dollar, 2 ten-dollar, 3 five-dollar
 
Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?
ANSWER: 12 one-dollar, 4 five-dollar
 
A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine?
ANSWER: 90 dimes, 45 nickels, 120 quarters
 
Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?
Line 332 ⟶ 517:
</pre>
 
=={{header|PerlJulia}}==
Uses the JuMP optimization library as a solver. The examples are from the Go code, and a few regex are from the Perl code.
The following code reads a file containing coin problems line-by-line. It echoes comment lines starting with a hashmark and blank lines. Remaining lines are interpreted as coin problems, one per line.
<syntaxhighlight lang="julia">using JuMP, GLPK
 
const nums = Dict(
The program works by matching the english input against patterns for the parts of the coin schema (a)-(e) in the task description. Patterns are expressed as perl regular expressions. It then translates these parameters into a MAXIMA program. MAXIMA is a free, open-source computer algebra system (CAS). It executes the MAXIMA script, extracts the solution, and prints it. By using perl regexes to read the input and MAXIMA to solve the resulting equations, the program may be kept fairly short.
"zero" => 0, "one" => 1, "two" => 2, "three" => 3,
"four" => 4, "five" => 5, "six" => 6, "seven" => 7,
"eight" => 8, "nine" => 9, "ten" => 10, "eleven" => 11,
"twelve" => 12, "thirteen" => 13, "fourteen" => 14, "fifteen" => 15,
"sixteen" => 16, "seventeen" => 17, "eighteen" => 18, "nineteen" => 19,
"twenty" => 20,
)
 
function coinproblemsolver(text, maxitems=4, verbose=false)
This program has been tested with 28 coin problems (shown below). Here is the program:
coin_problem = Model(GLPK.Optimizer)
<lang perl>
for line in strip.(split(text, r"\n\n"))
# coin.pl
# Description: Solve math word problems involving coins.
# Usage: perl -CDSA coin.pl coin-problems.txt > output.txt
# Algorithm: NLP processor loosely inspired by Bobrow (1964) transforms
# english language coin problems into solvable equations that are
# piped into the Maxima computer algebra system.
# Input: File must be UTF-8 text file. One coin problem per line in english.
# Blank lines and comment lines beginning with hash mark ``#'' are OK.
# Output: STDOUT contains original problem, transformed equations and solutions.
 
# save original version
use strict;
oldline = deepcopy(line)
use utf8;
use List::Util qw(sum);
use List::MoreUtils qw(uniq);
 
# ignore short or commented lines
our $first = 0;
length(line) < 5 && continue
our %nums = (
zero line[1] => 0, one => 1, two =>'#' 2, three =>&& 3,continue
four => 4, five => 5, six => 6, seven => 7,
eight => 8, nine => 9, ten => 10, eleven => 11,
twelve => 12, thirteen => 13, fourteen => 14, fifteen => 15,
sixteen => 16, seventeen => 17, eighteen => 18, nineteen => 19,
twenty => 20, thirty => 30, forty => 40, fifty => 50,
sixty => 60, seventy => 70, eighty => 80, ninety => 90,
hundred => 100, thousand => 1_000, million => 1_000_000,
billion => 1_000_000_000, trillion => 1_000_000_000_000,
# My ActiveState Win32 Perl uses e-notation after 999_999_999_999_999
quadrillion => 1e+015, quintillion => 1e+018);
 
# create the data structures and registration function
# Groupings for thousands, millions, ..., quintillions
monies = Dict(
our $groups = qr/\d{4}|\d{7}|\d{10}|\d{13}|1e\+015|1e\+018/;
"dollar_coin" => 100, "half_dollar" => 50, "quarter" => 25, "dime" => 10,
"nickel" => 5, "penny" => 1,
)
foreach(d -> (monies["_" * "$d" * "_bill"] = 100 * d), [1, 2, 5, 10, 20, 50, 100, 500, 1000])
 
itemnames = String[]
# Numeral or e-notation
itemvalues = Int[]
our $num = qr/\d+|\d+e\+\d+/;
 
function register_variables(vars)
our $float = qr/(?:(?:[1-9][0-9]*\.?[0-9]*)|(?:0?\.[0-9]+))(?:[Ee][+-]?[0-9]+)?/;
indices = Int[]
our $count = 0;
for cap in vars
our $total = 0;
idx = findfirst(x -> cap == x, itemnames)
our @words = ();
if !(idx isa Nothing)
our @eqns = ();
push!(indices, idx)
our @vars = ();
else
our @types = ();
push!(itemnames, cap)
push!(indices, length(itemnames))
push!(itemvalues, get(monies, cap, 0))
end
end
return indices
end
 
sub add_type {
my ($type,$value) = @_;
push @vars, "v_$type: $value";
push @types, $type;
}
 
# set up the optimizer / problem solver
while (<>) {
@variables(coin_problem, begin x[1:maxitems] >= 0 end)
chomp; # chop trailing newline
my $orig = $_;
@words = ();
@eqns = ();
@vars = ();
@types = ();
$count = 0;
$total = 0;
 
s/^(|)// if !$first++; # skip utf8 control seq e.g., "" that starts file
next if /^\s*$/; # skip blank lines
# echo comment lines
if( /^\s*#.*$/ ) {
print $_, "\n";
next;
}
s/-/ /g; # convert hyphens to spaces
s/\s\s+/ /g; # remove duplicate whitespace, convert ws to space
s/ $//g; # remove trailing blank
s/^ //g; # remove leading blank
$_ = lc($_); # convert to lower case
# tokenize sentence boundaries
s/([\.\?\!]) / $1\n/g;
s/([\.\?\!])$/ $1\n/g;
# tokenize other punctuation and symbols
s/\$(.)/\$ $1/g; # prefix
s/(.)([\;\:\%',¢])/$1 $2/g; # suffix
s/\btwice\b/two times/g;
# Fractions
s/half.dollar(s?)/half_dollar/g;
s/\bone half\b/0.5/g;
s/\bhalf\b/0.5/g;
 
# Simplify sentence and standardize quantities
# Remove noise words
s/\b(the|a|to|of|i|is|that|it|on|you|this|for|but|with|are|have|be|at|or|was|so|if|out|not|he|she|they|has|do|does)\b\s*//g;
 
# convert hyphens to spaces, lowercase, newlines to spaces
# Convert English number-names to numbers
line = replace(replace(lowercase(line), "-" => " "), "\n" => " ")
foreach my $key (keys %nums) { s/\b$key\b/$nums{$key}/eg; }
 
# fractions/multipliers to parsable forms
s/(\d) , (\d)/$1 $2/g;
line = replace(line, r"half.dollars?" => "half_dollar")
s/(\d) and (\d)/$1 $2/g;
line = replace(line, r"\bone\s+half\b" => "0.5")
line = replace(line, r"\btwice\b" => "two times")
 
# convert spelled out number to integer text per nums dictionary
s/\b(\d) 100 (\d\d) (\d) (${groups})\b/($1 * 100 + $2 + $3) * $4/eg;
for p in nums
line = replace(line, Regex("\\b" * p[1] * "\\b") => string(p[2]))
end
 
# remove plurals of coinage
s/\b(\d) 100 (\d\d) (${groups})\b/($1 * 100 + $2) * $3/eg;
line = replace(line, r"(quarter|dime|nickel|dollar|coin|bill)s" => s"\1")
s/\b(\d) 100 (\d) (${groups})\b/($1 * 100 + $2) * $3/eg;
line = replace(line, "pennies" => "penny")
s/\b(\d) 100 (${groups})\b/$1 * $2 * 100/eg;
 
# change numerals to quantites and monies
s/\b100 (\d\d) (\d) (${groups})\b/(100 + $1 + $2) * $3/eg;
line = replace(line, r"dollar coin|all dollar" => "dollar_coin")
s/\b100 (\d\d) (${groups})\b/(100 + $1) * $2/eg;
line = replace(line, r"\$(\d+) bill" => s"_\1_bill")
s/\b100 (\d) (${groups})\b/(100 + $1) * $2/eg;
line = replace(line, r"(\d+) dollar bill" => s"_\1_bill")
s/\b100 (${groups})\b/$1 * 100/eg;
line = replace(line, r"((?:\d+\s+)+\d+)" => (s) -> mapreduce(x -> parse(Int, x), +, split(s))) # 20 6 -> 26
 
# remove most unparsed words
s/\b(\d\d) (\d) (${groups})\b/($1 + $2) * $3/eg;
line = replace(line, r"\b(the|a|to|of|i|is|that|it|on|you|this|for|but|with|are|have|be|at|or|was|so|if|out|not|he|she|they|has|do|did|does)\b" => "")
s/\b(\d{1,2}) (${groups})\b/$1 * $2/eg;
# simplify spacing
line = replace(line, r"\s+" => " ")
 
# Add variables and constraints to the problem
s/\b(\d\d) (\d) 100\b/($1 + $2) * 100/eg;
for m in eachmatch(r"([\d\.]+) (?:times )?as many (\w+) as (\w+)", line)
s/\b(\d{1,2}) 100\b/$1 * 100/eg;
indices = register_variables([m[2], m[3]])
@constraints(coin_problem, begin x[indices[1]] == x[indices[2]] * parse(Float64, m[1]) end)
end
for m in eachmatch(r"(\d+) more (\w+) than (\w+)", line)
indices = register_variables([m[2], m[3]])
@constraints(coin_problem, begin x[indices[1]] == x[indices[2]] + parse(Int, m[1]) end)
end
for m in eachmatch(r"(\d+) less (\w+) than (\w+)", line)
indices = register_variables([m[2], m[3]])
@constraints(coin_problem, begin x[indices[1]] == x[indices[2]] - parse(Int, m[1]) end)
end
if (m = match(r"same number (\w+), (\w+),? and (\w+)", line)) != nothing
indices = register_variables(m.captures)
@constraints(coin_problem, begin
x[indices[1]] == x[indices[2]]
x[indices[2]] == x[indices[3]]
end)
end
if (m = match(r"(\d+) (?:\w+ )?(\w+),? consist\D+\$([\d\.]+)\D+\$([\d\.]+)", line)) != nothing
n1, n2 = Int(round(100 * parse(Float64, m[3]))), Int(round(100 * parse(Float64, m[4])))
s1, s2 = m[2] * "_costing_" * string(n1), m[2] * "_costing_" * string(n2)
monies[s1], monies[s2] = n1, n2
indices = register_variables([s1, s2])
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"(\d+) (?:\w+ )?(\w+),? consist\D+([\d\.]+)¢\D+([\d\.]+)¢", line)) != nothing
n1, n2 = parse(Int, m[3]), parse(Int, m[4])
s1, s2 = m[2] * "_costing_" * string(n1), m[2] * "_costing_" * string(n2)
monies[s1], monies[s2] = n1, n2
indices = register_variables([s1, s2])
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"(\d+) (?:coin|bill),? consist(?:s|ing) (\w+) and (\w+)", line)) != nothing
indices = register_variables([m[2], m[3]])
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"(\d+) (?:coin)[^\d\.,]+pocket (\w+) and (\w+)", line)) != nothing
indices = register_variables([m[2], m[3]])
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"(\d+) (?:coin|bill),? consist(?:s|ing) (\w+), (\w+), and (\w+)", line)) != nothing
indices = register_variables([m[2], m[3], m[4]])
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"\$([\d\.]+) ream\D+\$([\d\.]+)\D+(\d+) reams", line)) != nothing
n1, n2 = Int(round(100 * parse(Float64, m[1]))), Int(round(100 * parse(Float64, m[2])))
s1, s2 = "ream_costing_" * string(n1), "ream_costing_" * string(n2)
monies[s1], monies[s2] = n1, n2
indices = register_variables([s1, s2])
@constraints(coin_problem, begin sum(x) == parse(Int, m[3]) end)
end
if (m = match(r"(?:in my wallet,?|only accepts) (\w+), (\w+),? and (\w+)", line)) != nothing
indices = register_variables([m.captures[1], m.captures[2], m.captures[3]])
end
if (m = match(r"(\d+) coin in (\w+) and (\w+) only", line)) != nothing
indices = register_variables([m.captures[2], m.captures[3]])
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"(\d+) coin (?:total|in all)", line)) != nothing
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
if (m = match(r"(?:had only|in) (\w+) and (\w+)[,\.\?]", line)) != nothing
indices = register_variables([m.captures[1], m.captures[2]])
end
if (m = match(r"(?:sold total|all together,? there) (\d+)", line)) != nothing
@constraints(coin_problem, begin sum(x) == parse(Int, m[1]) end)
end
for m in eachmatch(r"cost \$([\d\.]+) each", line)
s = "item_costing_" * m.captures[1]
monies[s] = Int(round(100 * parse(Float64, m.captures[1])))
register_variables([s])
end
 
# find a total
# anomolous cases: nineteen eighty-four and twenty thirteen
m = match(r"add up \$?([\d\.]+)", line)
s/\b(\d{2}) (\d{2})\b/$1 * 100 + $2/eg;
if m isa Nothing
m = match(r"(?:cost stamps|total cost|paid|value|valuing|store made)[^\$]+\$([\d\.]+)", line)
end
if m isa Nothing
m = match(r"(?:total|value of|store made|given)[^\$]+\$([\d\.]+)", line)
end
if m isa Nothing
m = match(r"\$([\d\.]+) (?:coin )?in", line)
end
if !(m isa Nothing)
m1 = m.captures[1][end] == '.' ? m.captures[1][1:end-1] : m.captures[1]
@constraints(coin_problem, begin
sum([itemvalues[i] * x[i] for i in 1:length(itemnames)]) == Int(round(100 * parse(Float64, m1)))
end)
else
m = match(r"total (?:amount coin )([\d\.]+)¢", line)
if !(m isa Nothing)
@constraints(coin_problem, begin
sum([itemvalues[i] * x[i] for i in 1:length(itemnames)]) == parse(Int, m[1])
end)
else
error("Missing or unparsed total funds constraint")
end
end
 
s/((?:${num} )*${num})/sum(split(" ",$1))/eg;
 
# set unused x components to 0
s/dollar coin/dollar_coin/g;
for i in length(itemnames)+1:maxitems
s/quarters/quarter/g;
@constraints(coin_problem, begin x[i] == 0 end)
s/dimes/dime/g;
end
s/nickels/nickel/g;
s/pennies/penny/g;
s/dollars/dollar/g;
s/coins/coin/g;
s/bills/bill/g;
s/(\d+) dollar\b/\$ $1/g;
 
# Rules for coin problems # solve
optimize!(coin_problem)
# Rule triggers are just regexes
verbose && println(line)
verbose && println(coin_problem)
print(oldline, "\nAnswer: ")
for i in eachindex(itemnames)
print(rpad(itemnames[i] * "(s)", 10), ": ", rpad(Int(round(JuMP.value(x[i]))), 10))
end
println("\n")
JuMP.empty!(coin_problem)
end
end
 
const DATA = raw"""
add_type("dollar_coin",100) if /dollar_coin/;
If a person has three times as many quarters as dimes and the total amount of money is $5.95,
add_type("half_dollar",50) if /half_dollar/;
find the number of quarters and dimes.
add_type("quarter",25) if /quarter/;
add_type("dime",10) if /dime/;
add_type("nickel",5) if /nickel/;
add_type("penny",1) if /penny/;
 
A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38¢,
while(/(${float}) (?:times )?as many \$ (\d+) bill as \$ (\d+) bill/g) {
find the number of pennies and nickels.
push @eqns, "n_$2 = $1 * n_$3";
}
 
A small child has 6 more quarters than nickels. If the total amount of coins is $3.00,
while(/(${float}) (?:times )?as many (\w+) as (\w+)/g) {
find the number of nickels and quarters the child has.
push @eqns, "n_$2 = $1 * n_$3";
}
 
A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of
while(/(\d+) more (\w+) than (\w+)/g) {
money is $3.80, find the number of nickels and quarters in the bank.
push @eqns, "n_$2 = n_$3 + $1";
}
 
A person has twice as many dimes as she has pennies and three more nickels than pennies. If
while(/(\d+) less (\w+) than (\w+)/g) {
the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
push @eqns, "n_$2 = n_$3 - $1";
}
 
In a bank, there are three times as many quarters as half dollars and 6 more dimes than
if(/same number (\w+) , (\w+) (?:, )?and (\w+)/){
half dollars. If the total amount of the money in the bank is $4.65, find the number of
push @eqns, "n_$1 = n_$2";
each type of coin in the bank.
push @eqns, "n_$2 = n_$3";
} elsif(/same number (\w+) and (\w+)/){
push @eqns, "n_$1 = n_$2";
}
 
A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps
if(/(\d+) (?:\w+ )*?consists/
is $3.74, find the number of each type of the stamps purchased.
or /(\d+) (?:\w+ )*?,?consisting/
or /total (\d+)(?! ¢)/
or /(?<!\$ )(\d+) coin/
or /[^\$] (\d+) bill/
or /number [^\?\!\.0-9]*?(\d+)/) {
$count = $1;
push @vars, "count: $count";
}
 
A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches
if(/total (?:\w+ )*?\$ (${float})/
cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number
or /valu(?:e|ing) \$ (${float})/
of each sold.
or /\$ (${float}) ((bill|coin) )?in/) {
$total = 100 * $1;
push @vars, "total: $total";
}
 
An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled
if(/total (?:\w+ )*?(${float}) ¢/) {
notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and
$total = $1;
paid $15.71, how many reams of each type of paper did the student purchase?
push @vars, "total: $total";
}
 
A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are
# Rules for stamp problems
twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each
s/stamps/stamp/g;
type of bill are there?
while(/(\d+) ¢ stamp/g) { add_type("$1_stamp", $1); }
if(/cost (?:\w+ )*?\$ (${float}) (?!each)/) { $total = 100 * $1; push @vars, "total: $total"; }
 
A person has 8 coins consisting of quarters and dimes. If the total amount of this change
# Rules for unusual types
is $1.25, how many of each kind of coin are there?
while(/(\w+) cost \$ (${float}) each/g) { add_type($1, 100 * $2); }
if(/made \$ (${float})/) { $total = 100 * $1; push @vars, "total: $total"; }
 
A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the
# Rules for bill problems
total amount of these coins is $1.13, how many of each kind of coin does he have?
while(/\$ (\d+) bill/g) { add_type($1, 100 * $1); }
while(/(\d+) times as many \$ (\d+) bill as \$ (\d+) bill/g) { push @eqns, "n_$2 = $1 * n_$3"; }
while(/(\d+) more \$ (\d+) bill than \$ (\d+) bill/g) { push @eqns, "n_$2 = n_$3 + $1"; }
while(/(\d+) less \$ (\d+) bill than \$ (\d+) bill/g) { push @eqns, "n_$2 = n_$3 - $1"; }
if(/\$ (${float}) in bill/) { $total = 100 * $1; push @vars, "total: $total"; }
 
A person bought ten greeting cards consisting of birthday cards costing $1.50 each and
# Rules for greeting card example
anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the
if(/consisting (.+?)costing \$ (${float}) each and (.+?)costing \$ (${float}) each/) {
number of each kind of card the person bought.
my ($t1,$v1,$t2,$v2) = ($1,$2,$3,$4);
$t1 =~ s/ $//; $t1 =~ s/ /_/g;
$t2 =~ s/ $//; $t2 =~ s/ /_/g;
add_type($t1,100*$v1);
add_type($t2,100*$v2);
}
 
A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the
# Rules for notebook paper example
number of dimes the person has.
if(/sells ([^\$]+)\$ (${float}) (\w+) and ([^\$]+)\$ (${float}) (\w+)/) {
 
my ($t1,$v1,$u1,$t2,$v2,$u2) = ($1,$2,$3,$4,$5,$6);
A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money
$t1 =~ s/ $//; $t1 =~ s/ /_/g;
the person has is $35, find the number of $2 bills the person has.
$t2 =~ s/ $//; $t2 =~ s/ /_/g;
 
add_type($t1,100*$v1);
A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total
add_type($t2,100*$v2);
amount of money in the bank is $3.10, find the number of dimes in the bank.
if($u1 eq $u2){
 
if(/purchased (\d+) ${u1}(?:s|es)? (?:\w+ )*?and paid \$ (${float})/){
Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you
$count = $1; push @vars, "count: $count";
can have all the coins if you can figure out how many of each kind of coin he is carrying.
$total = 100*$2; push @vars, "total: $total";
You're not too interested until he tells you that he's been collecting those gold-tone
}
one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they
add up to seventeen dollars in value. How many of each coin does he have?
 
A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30.
If there are three times as many nickels as quarters, and one-half as many dimes as nickels,
how many coins of each kind are there?
 
A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How
many of each type of coin does the wallet contain?
 
Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of
each coin does he have?
 
Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes
is 38. How many quarters and dimes does Terry have?
 
In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total
amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills.
All together, there are 13 bills in my wallet. How many of each bill do I have?
 
Marsha has three times as many one-dollar bills as she does five dollar bills. She has a
total of $32. How many of each bill does she have?
 
A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts
nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin
are in the machine?
 
Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins
of each kind did he have?
 
Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each
type of coin did she have?
 
Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each
type of coin does he have?
 
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30,
how many of each coin are there?
"""
 
coinproblemsolver(DATA)
 
</syntaxhighlight>{{out}}
<pre>
If a person has three times as many quarters as dimes and the total amount of money is $5.95,
find the number of quarters and dimes.
Answer: quarter(s): 21 dime(s) : 7
 
A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38¢,
find the number of pennies and nickels.
Answer: penny(s) : 13 nickel(s) : 5
 
A small child has 6 more quarters than nickels. If the total amount of coins is $3.00,
find the number of nickels and quarters the child has.
Answer: quarter(s): 11 nickel(s) : 5
 
A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of
money is $3.80, find the number of nickels and quarters in the bank.
Answer: nickel(s) : 21 quarter(s): 11
 
A person has twice as many dimes as she has pennies and three more nickels than pennies. If
the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
Answer: dime(s) : 14 penny(s) : 7 nickel(s) : 10
 
In a bank, there are three times as many quarters as half dollars and 6 more dimes than
half dollars. If the total amount of the money in the bank is $4.65, find the number of
each type of coin in the bank.
Answer: quarter(s): 9 half_dollar(s): 3 dime(s) : 9
 
A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps
is $3.74, find the number of each type of the stamps purchased.
Answer: stamps_costing_37(s): 7 stamps_costing_23(s): 5
 
A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches
cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number
of each sold.
Answer: item_costing_0.69(s): 32 item_costing_0.75(s): 48
 
An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled
notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and
paid $15.71, how many reams of each type of paper did the student purchase?
Answer: ream_costing_159(s): 7 ream_costing_229(s): 2
 
A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are
twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each
type of bill are there?
Answer: _1_bill(s): 10 _5_bill(s): 5 _10_bill(s): 4
 
A person has 8 coins consisting of quarters and dimes. If the total amount of this change
is $1.25, how many of each kind of coin are there?
Answer: quarter(s): 3 dime(s) : 5
 
A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the
total amount of these coins is $1.13, how many of each kind of coin does he have?
Answer: dime(s) : 9 nickel(s) : 3 penny(s) : 8
 
A person bought ten greeting cards consisting of birthday cards costing $1.50 each and
anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the
number of each kind of card the person bought.
Answer: cards_costing_150(s): 6 cards_costing_200(s): 4
 
A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the
number of dimes the person has.
Answer: dime(s) : 11 nickel(s) : 2
 
A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money
the person has is $35, find the number of $2 bills the person has.
Answer: _1_bill(s): 5 _2_bill(s): 15
 
A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total
amount of money in the bank is $3.10, find the number of dimes in the bank.
Answer: penny(s) : 25 nickel(s) : 17 dime(s) : 20
 
Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you
can have all the coins if you can figure out how many of each kind of coin he is carrying.
You're not too interested until he tells you that he's been collecting those gold-tone
one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they
add up to seventeen dollars in value. How many of each coin does he have?
Answer: dollar_coin(s): 14 quarter(s): 12
 
A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30.
If there are three times as many nickels as quarters, and one-half as many dimes as nickels,
how many coins of each kind are there?
Answer: nickel(s) : 18 quarter(s): 6 dime(s) : 9
 
A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How
many of each type of coin does the wallet contain?
Answer: penny(s) : 9 nickel(s) : 9 dime(s) : 9
 
Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of
each coin does he have?
Answer: nickel(s) : 17 dime(s) : 8
 
Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes
is 38. How many quarters and dimes does Terry have?
Answer: quarter(s): 20 dime(s) : 18
 
In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total
amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills.
All together, there are 13 bills in my wallet. How many of each bill do I have?
Answer: _1_bill(s): 8 _10_bill(s): 2 _5_bill(s): 3
 
Marsha has three times as many one-dollar bills as she does five dollar bills. She has a
total of $32. How many of each bill does she have?
Answer: _1_bill(s): 12 _5_bill(s): 4
 
A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts
nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin
are in the machine?
Answer: dime(s) : 90 nickel(s) : 45 quarter(s): 120
 
Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins
of each kind did he have?
Answer: quarter(s): 12 dime(s) : 15
 
Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each
type of coin did she have?
Answer: nickel(s) : 140 quarter(s): 25
 
Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each
type of coin does he have?
Answer: quarter(s): 121 dime(s) : 150
 
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30,
how many of each coin are there?
Answer: dime(s) : 2 penny(s) : 10
</pre>
 
=={{header|Perl}}==
Coin-type 'word problems' are analyzed into their constituent algebraic relationships, in a format suitable for processing by MAXIMA, a free computer algebra system. NB: MAXIMA <b>must</b> be locally installed for this task to function.
<syntaxhighlight lang="perl">use strict;
use warnings;
 
use List::Util qw(sum uniq);
use File::Temp qw(tempfile);
 
my %nums = (
zero => 0, one => 1, two => 2, three => 3,
four => 4, five => 5, six => 6, seven => 7,
eight => 8, nine => 9, ten => 10, eleven => 11,
twelve => 12, thirteen => 13, fourteen => 14, fifteen => 15,
sixteen => 16, seventeen => 17, eighteen => 18, nineteen => 19,
twenty => 20,
);
 
my $decimal = qr/(?:[1-9][0-9]*\.?[0-9]*)|(?:0?\.[0-9]+)/;
 
while (<DATA>) {
chomp;
next if /^\s*$/ or /^\s*#.*$/; # skip blank and comment lines
 
my($count, $total) = (0, 0);
our @words = our @eqns = our @vars = our @types = ();
 
sub add_type {
my($type,$value) = @_;
push @vars, "v_$type: $value";
push @types, $type;
}
}
 
# Step 1: standardize language
# Rules ALL coin problems obey...
# Rule no 1: Total is sum over all coin types of coin value times coin number
# i.e., total = dot product of values and quantities
my $dot = join(" + ", map {"n_$_ * v_$_"} uniq @types);
if($total && scalar @types){ push @eqns, "total = $dot"; }
 
s/-/ /g; # convert hyphens to spaces
# Rule no 2: Count of all coins is sum of counts of each coin type
$_ = lc($_); # convert to lower case
my $trace = join(" + ", map {"n_$_"} uniq @types);
if($count && scalar @types){ push @eqns, "count = $trace"; }
 
# tokenize sentence boundaries, punctuation, symbols
print "original input: $orig\n";
s/([\.\?\!]) / $1\n/g;
s/([\.\?\!])$/ $1\n/g;
s/\$(.)/\$ $1/g; # prefix
s/(.)([\;\:\%',¢])/$1 $2/g; # suffix
 
# fractions/multipliers
# Prepare MAXIMA batch file
s/half.dollars?/half_dollar/g;
my $maxima_vars = join("\$\n", uniq @vars);
s/\b(one )?half\b/0.5/g;
my $maxima_eqns = "[". join(", ", @eqns) . "]";
s/\btwice\b/two times/g;
my $maxima_find = "[". join(", ", map {"n_$_"} @types) . "]";
 
my $maxima_script = "${maxima_vars}\$\nsolve(${maxima_eqns}, ${maxima_find});\n";
# convert English number-names to numbers
print $maxima_script;
foreach my $key (keys %nums) { s/\b$key\b/$nums{$key}/eg }
if(scalar @eqns && scalar @vars) {
 
my $temp = time() . "_" . rand() . ".max";
# remove plurals
open(TEMP, ">$temp") || die "Couldn't open temp file: $!\n";
s/(quarter|dime|nickel|dollar|coin|bill)s/$1/g;
print TEMP $maxima_script;
close TEMPs/pennies/penny/g;
 
open(MAXIMA, "maxima -q -b $temp |") || die "Couldn't open maxima: $!\n";
while(<MAXIMA>)# {misc
s/dollar coin/dollar_coin/g;
# filter out everything but output line with the solution
if(s/\(\%o\d+\) dollar\s+b/\[\[([^\]]+)\]\]$ $1/) {g;
s/((?:\d+ )*\d+)/sum(split(' ',$1))/eg;
print "solution: $1\n";
 
}
# remove non-essential words
s/\b(the|a|to|of|i|is|that|it|on|you|this|for|but|with|are|have|be|at|or|was|so|if|out|not|he|she|they|has|do|did|does)\b\s*//g;
 
# Step 2: assign numeric values to terms
 
add_type('dollar_coin',100) if /dollar_coin/;
add_type('half_dollar',50) if /half_dollar/;
add_type('quarter',25) if /quarter/;
add_type('dime',10) if /dime/;
add_type('nickel',5) if /nickel/;
add_type('penny',1) if /penny/;
add_type($1, 100 * $1) while /\$ (\d+) bill/g;
 
# Step 3: determine algebraic relationships
 
while (/($decimal) (?:times )?as many \$ (\d+) bill as \$ (\d+) bill/g) { push @eqns, "n_$2 = n_$3 * $1" }
while (/($decimal) (?:times )?as many (\w+) as (\w+)/g) { push @eqns, "n_$2 = n_$3 * $1" }
while (/(\d+) more (\w+) than (\w+)/g) { push @eqns, "n_$2 = n_$3 + $1" }
while (/(\d+) less (\w+) than (\w+)/g) { push @eqns, "n_$2 = n_$3 - $1" }
while (/(\d+) less \$ (\d+) bill than \$ (\d+) bill/g) { push @eqns, "n_$2 = n_$3 - $1" }
 
if (/same number (\w+) , (\w+) (?:, )?and (\w+)/) {
push @eqns, "n_$1 = n_$2";
push @eqns, "n_$2 = n_$3";
}
 
close MAXIMA;
if (/(\d+) (?:\w+ )*consists/ or /(?<!\$ )(\d+) coin/ or /[^\$] (\d+) bill/) {
unlink $temp;
$count = $1; push @vars, "count: $count"
} else {
}
print "Couldn't deduce enough information to formulate equations.\n"
 
}
if (/total (?:\w+ )*\$ ($decimal)/ or /valu(?:e|ing) \$ ($decimal)/ or /\$ ($decimal) ((bill|coin) )?in/) {
print "\n\n";
$total = 100 * $1;
push @vars, "total: $total";
}
 
if (/total (?:\w+ )*($decimal)/) {
$total = $1;
push @vars, "total: $total";
}
 
# Step 4: tally final total value, coin count
 
# sum total, dot product of values and quantities
my $dot_product = join(' + ', map {"n_$_ * v_$_"} uniq @types);
push @eqns, "total = $dot_product" if $total and @types;
 
# count of all coins, sum of counts of each coin type
my $trace = join(' + ', map {"n_$_"} uniq @types);
push @eqns, "count = $trace" if $count and @types;
 
# Step 5: prepare batch file for external processing, run 'MAXIMA', output results
 
printf "problem: %s\n", s/\n/ /gr; # condensed problem statement
 
my $maxima_vars = join("\$\n", uniq @vars);
my $maxima_eqns = '['. join(', ', @eqns) . ']';
my $maxima_find = '['. join(', ', map {"n_$_"} @types) . ']';
 
if (@eqns and @vars) {
my ($fh, $maxima_script) = tempfile(UNLINK => 1);
open $fh, '>', $maxima_script or die "Couldn't open temporary file: $!\n";
print $fh <<~"END";
$maxima_vars\$
solve($maxima_eqns, $maxima_find);
END
close $fh;
 
open my $maxima_output, "/opt/local/bin/maxima -q -b $maxima_script |" or die "Couldn't open maxima: $!\n";
while (<$maxima_output>) {
print "solution: $1\n" if /\(\%o\d+\)\s+\[\[([^\]]+)\]\]/; # only display solution
}
close $maxima_output;
} else {
print "Couldn't deduce enough information to formulate equations.\n"
}
print "\n";
}
 
</lang>
__DATA__
The following is an example '''input''' file:
<pre>
# Bluman, Allan G. Math word problems demystified. 2005. The McGraw-Hill Companies Inc. p. 112, problem 1
If a person has three times as many quarters as dimes and the total amount of money is $5.95, find the number of quarters and dimes.
 
# Ibid., p. 112, problem 2
A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38¢, find the number of pennies and nickels.
 
# Ibid., p. 112, problem 3
A small child has 6 more quarters than nickels. If the total amount of coins is $3.00, find the number of nickels and quarters the child has.
 
A childs bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.
# Ibid., p. 112, problem 4
A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.
 
# Ibid., p. 112, problem 5
A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
 
# Ibid., p. 112, problem 6
In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.
 
# Ibid., p. 112, problem 7
A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps is $3.74, find the number of each type of the stamps purchased.
 
# Ibid., p. 112, problem 8
A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number of each sold.
 
# Ibid., p. 112, problem 9
An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and paid $15.71, how many reams of each type of paper did the student purchase?
 
# Ibid., p. 112, problem 10
A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?
 
# Ibid., p. 109
A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?
 
# Ibid., p. 110
A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?
 
# Ibid., p. 111
A person bought ten greeting cards consisting of birthday cards costing $1.50 each and anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the number of each kind of card the person bought.
 
# Ibid., p. 119, problem 8
A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the number of dimes the person has.
 
# Ibid., p. 120, problem 9
A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.
 
# Ibid., p. 120, problem 10
A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.
 
The twenty-six coins in my pocket are all dollar coins and quarters, and they add up to seventeen dollars in value. How many of each coin are there?
# More test problems from around the web...
 
# Source: http://www.purplemath.com/modules/coinprob.htm
# Soln: 12 quarters, 14 dollar coins
Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?
 
# Ibid.
# Soln: Then there are six quarters, and I can work backwards to figure out that there are 9 dimes and 18 nickels.
A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?
 
# Ibid.
# Soln. There are nine of each type of coin in the wallet.
A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?
 
# Source: http://www.algebralab.org/Word/Word.aspx?file=Algebra_CoinProblems.xml
# Soln: Ken has 17 nickels and 8 dimes.
Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?
 
# Ibid.
# Let's Practice
# Question #1
# Note: The original question had an inconsistency in it,
# namely "Terry has 7 more" should be "Terry has 2 more..."
# Soln: Terry has 18 dimes and 20 quarters.
Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?
 
# Ibid.
# Question #2
# Soln: There are 2 ten-dollar bills, 8 one-dollar bills, and 3 five-dollar bills.
In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?
 
# Ibid.
# Try These
# Question #1
Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?
 
# Ibid.
# Question #2
A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine.
 
# Source: http://voices.yahoo.com/how-set-solve-coin-word-problems-algebra-1713709.html
 
# Sample Coin Word Problem 1:
# Soln: Michael has 15 dimes and 12 quarters.
Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?
 
# Ibid.
# Sample Coin Word Problem 2:
# Soln: Lucille has 25 quarters and 140 nickels.
Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each type of coin did she have?
 
# Ibid.
# Sample Coin Word Problem 3:
# Soln: Ben has 150 dimes and 121 quarters.
Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each type of coin does he have?
 
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?</syntaxhighlight>
# Source: http://www.calculatorsoup.com/calculators/wordproblems/algebrawordproblem1.php
{{out}}
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?
<pre>problem: person 3 times as many quarter as dime and total amount money $ 5.95 , find number quarter and dime .
</pre>
And this is the resulting '''output''' file. Each section contains a citation of the source of the input, the original input, the MAXIMA script as formulated by the perl script, and the solution extracted from the MAXIMA output:
<pre>
# Bluman, Allan G. Math word problems demystified. 2005. The McGraw-Hill Companies Inc. p. 112, problem 1
original input: If a person has three times as many quarters as dimes and the total amount of money is $5.95, find the number of quarters and dimes.
v_quarter: 25$
v_dime: 10$
total: 595$
solve([n_quarter = 3 * n_dime, total = n_quarter * v_quarter + n_dime * v_dime], [n_quarter, n_dime]);
solution: n_quarter = 21, n_dime = 7
 
problem: pile 18 coin consists penny and nickel . total amount coin 38 ¢ , find number penny and nickel .
# Ibid., p. 112, problem 2
original input: A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38¢, find the number of pennies and nickels.
v_nickel: 5$
v_penny: 1$
count: 18$
total: 38$
solve([total = n_nickel * v_nickel + n_penny * v_penny, count = n_nickel + n_penny], [n_nickel, n_penny]);
solution: n_nickel = 5, n_penny = 13
 
problem: small child 6 more quarter than nickel . total amount coin $ 3.0 , find number nickel and quarter child .
# Ibid., p. 112, problem 3
original input: A small child has 6 more quarters than nickels. If the total amount of coins is $3.00, find the number of nickels and quarters the child has.
v_quarter: 25$
v_nickel: 5$
total: 300$
solve([n_quarter = n_nickel + 6, total = n_quarter * v_quarter + n_nickel * v_nickel], [n_quarter, n_nickel]);
solution: n_quarter = 11, n_nickel = 5
 
problem: childs bank contains 32 coin consisting nickel and quarter . total amount money $ 3.80 , find number nickel and quarter in bank .
# Ibid., p. 112, problem 4
 
original input: A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.
v_quarter: 25$
v_nickel: 5$
count: 32$
total: 380$
solve([total = n_quarter * v_quarter + n_nickel * v_nickel, count = n_quarter + n_nickel], [n_quarter, n_nickel]);
solution: n_quarter = 11, n_nickel = 21
 
problem: person 2 times as many dime as penny and 3 more nickel than penny . total amount coin $ 1.97 , find numbers each type coin person .
# Ibid., p. 112, problem 5
original input: A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
v_dime: 10$
v_nickel: 5$
v_penny: 1$
total: 197$
solve([n_dime = 2 * n_penny, n_nickel = n_penny + 3, total = n_dime * v_dime + n_nickel * v_nickel + n_penny * v_penny], [n_dime, n_nickel, n_penny]);
solution: n_dime = 14, n_nickel = 10, n_penny = 7
 
problem: in bank , 3 times as many quarter as half_dollar and 6 more dime than half_dollar . total amount money in bank $ 4.65 , find number each type coin in bank .
# Ibid., p. 112, problem 6
original input: In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.
v_half_dollar: 50$
v_quarter: 25$
v_dime: 10$
total: 465$
solve([n_quarter = 3 * n_half_dollar, n_dime = n_half_dollar + 6, total = n_half_dollar * v_half_dollar + n_quarter * v_quarter + n_dime * v_dime], [n_half_dollar, n_quarter, n_dime]);
solution: n_half_dollar = 3, n_quarter = 9, n_dime = 9
 
problem: clerk given $ 75 in bill put in cash drawer start workday . 2 times as many $ 1 bill as $ 5 bill and 1 less $ 10 bill than $ 5 bill . how many each type bill ?
# Ibid., p. 112, problem 7
original input: A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps is $3.74, find the number of each type of the stamps purchased.
count: 12$
v_37_stamp: 37$
v_23_stamp: 23$
total: 374$
solve([total = n_37_stamp * v_37_stamp + n_23_stamp * v_23_stamp, count = n_37_stamp + n_23_stamp], [n_37_stamp, n_23_stamp]);
solution: n_37_stamp = 7, n_23_stamp = 5
 
# Ibid., p. 112, problem 8
original input: A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number of each sold.
count: 80$
v_sandwiches: 69$
v_bars: 75$
total: 5880$
solve([total = n_sandwiches * v_sandwiches + n_bars * v_bars, count = n_sandwiches + n_bars], [n_sandwiches, n_bars]);
solution: n_sandwiches = 20, n_bars = 60
 
# Ibid., p. 112, problem 9
original input: An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and paid $15.71, how many reams of each type of paper did the student purchase?
v_college_ruled_notebook_paper: 159$
v_wide_ruled_notebook_paper: 229$
count: 9$
total: 1571$
solve([total = n_college_ruled_notebook_paper * v_college_ruled_notebook_paper + n_wide_ruled_notebook_paper * v_wide_ruled_notebook_paper, count = n_college_ruled_notebook_paper + n_wide_ruled_notebook_paper], [n_college_ruled_notebook_paper, n_wide_ruled_notebook_paper]);
solution: n_college_ruled_notebook_paper = 7, n_wide_ruled_notebook_paper = 2
 
# Ibid., p. 112, problem 10
original input: A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?
total: 7500$
v_1: 100$
v_5: 500$
v_10: 1000$
solve([n_1 = 2 * n_5, n_1 = 2 * n_5, n_10 = n_5 - 1, total = n_1 * v_1 + n_5 * v_5 + n_10 * v_10], [n_1, n_5, n_10, n_5]);
solution: n_1 = 10, n_10 = 4, n_5 = 5
 
problem: person 8 coin consisting quarter and dime . total amount change $ 1.25 , how many each kind coin ?
# Ibid., p. 109
original input: A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?
v_quarter: 25$
v_dime: 10$
count: 8$
total: 125$
solve([total = n_quarter * v_quarter + n_dime * v_dime, count = n_quarter + n_dime], [n_quarter, n_dime]);
solution: n_quarter = 3, n_dime = 5
 
problem: person 3 times as many dime as nickel and 5 more penny than nickel . total amount these coin $ 1.13 , how many each kind coin ?
# Ibid., p. 110
original input: A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?
v_dime: 10$
v_nickel: 5$
v_penny: 1$
total: 113$
solve([n_dime = 3 * n_nickel, n_penny = n_nickel + 5, total = n_dime * v_dime + n_nickel * v_nickel + n_penny * v_penny], [n_dime, n_nickel, n_penny]);
solution: n_dime = 9, n_nickel = 3, n_penny = 8
 
problem: person 9 more dime than nickel . total amount money $ 1.20 , find number dime person .
# Ibid., p. 111
original input: A person bought ten greeting cards consisting of birthday cards costing $1.50 each and anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the number of each kind of card the person bought.
count: 10$
total: 1700$
v_birthday_cards: 150$
v_anniversary_cards: 200$
solve([total = n_birthday_cards * v_birthday_cards + n_anniversary_cards * v_anniversary_cards, count = n_birthday_cards + n_anniversary_cards], [n_birthday_cards, n_anniversary_cards]);
solution: n_birthday_cards = 6, n_anniversary_cards = 4
 
# Ibid., p. 119, problem 8
original input: A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the number of dimes the person has.
v_dime: 10$
v_nickel: 5$
total: 120$
solve([n_dime = n_nickel + 9, total = n_dime * v_dime + n_nickel * v_nickel], [n_dime, n_nickel]);
solution: n_dime = 11, n_nickel = 2
 
problem: person 20 bill consisting $ 1 bill and $ 2 bill . total amount money person $ 35 , find number $ 2 bill person .
# Ibid., p. 120, problem 9
original input: A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.
count: 20$
total: 3500$
v_1: 100$
v_2: 200$
solve([total = n_1 * v_1 + n_2 * v_2, count = n_1 + n_2], [n_1, n_2, n_2]);
solution: n_1 = 5, n_2 = 15
 
problem: bank contains 8 more penny than nickel and 3 more dime than nickel . total amount money in bank $ 3.10 , find number dime in bank .
# Ibid., p. 120, problem 10
original input: A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.
v_dime: 10$
v_nickel: 5$
v_penny: 1$
total: 310$
solve([n_penny = n_nickel + 8, n_dime = n_nickel + 3, total = n_dime * v_dime + n_nickel * v_nickel + n_penny * v_penny], [n_dime, n_nickel, n_penny]);
solution: n_dime = 20, n_nickel = 17, n_penny = 25
 
problem: 26 coin in my pocket all dollar_coin and quarter , and add up $ 17 in value . how many each coin ?
# More test problems from around the web...
# Source: http://www.purplemath.com/modules/coinprob.htm
# Soln: 12 quarters, 14 dollar coins
original input: Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?
v_dollar_coin: 100$
v_quarter: 25$
count: 26$
total: 1700$
solve([total = n_dollar_coin * v_dollar_coin + n_quarter * v_quarter, count = n_dollar_coin + n_quarter], [n_dollar_coin, n_quarter]);
solution: n_dollar_coin = 14, n_quarter = 12
 
problem: collection 33 coin , consisting nickel , dime , and quarter , value $ 3.30 . 3 times as many nickel as quarter , and 0.5 as many dime as nickel , how many coin each kind ?
# Ibid.
# Soln: Then there are six quarters, and I can work backwards to figure out that there are 9 dimes and 18 nickels.
original input: A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?
v_quarter: 25$
v_dime: 10$
v_nickel: 5$
count: 33$
total: 330$
solve([n_nickel = 3 * n_quarter, n_dime = 0.5 * n_nickel, total = n_quarter * v_quarter + n_dime * v_dime + n_nickel * v_nickel, count = n_quarter + n_dime + n_nickel], [n_quarter, n_dime, n_nickel]);
solution: n_quarter = 6, n_dime = 9, n_nickel = 18
 
problem: wallet contains same number penny , nickel , and dime . coin total $ 1.44 . how many each type coin wallet contain ?
# Ibid.
# Soln. There are nine of each type of coin in the wallet.
original input: A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?
v_dime: 10$
v_nickel: 5$
v_penny: 1$
total: 144$
solve([n_penny = n_nickel, n_nickel = n_dime, total = n_dime * v_dime + n_nickel * v_nickel + n_penny * v_penny], [n_dime, n_nickel, n_penny]);
solution: n_dime = 9, n_nickel = 9, n_penny = 9
 
problem: suppose ken 25 coin in nickel and dime only and total $ 1.65 . how many each coin ?
# Source: http://www.algebralab.org/Word/Word.aspx?file=Algebra_CoinProblems.xml
# Soln: Ken has 17 nickels and 8 dimes.
original input: Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?
v_dime: 10$
v_nickel: 5$
count: 25$
total: 165$
solve([total = n_dime * v_dime + n_nickel * v_nickel, count = n_dime + n_nickel], [n_dime, n_nickel]);
solution: n_dime = 8, n_nickel = 17
 
problem: terry 2 more quarter than dime and total $ 6.80 . number quarter and dime 38 . how many quarter and dime terry ?
# Ibid.
# Let's Practice
# Question #1
# Note: The original question had an inconsistency in it,
# namely "Terry has 7 more" should be "Terry has 2 more..."
# Soln: Terry has 18 dimes and 20 quarters.
original input: Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?
v_quarter: 25$
v_dime: 10$
count: 38$
total: 680$
solve([n_quarter = n_dime + 2, total = n_quarter * v_quarter + n_dime * v_dime, count = n_quarter + n_dime], [n_quarter, n_dime]);
solution: n_quarter = 20, n_dime = 18
 
problem: in my wallet , $ 1 bill , $ 5 bill , and $ 10 bill . total amount in my wallet $ 43 . 4 times as many $ 1 bill as $ 10 bill . all together , 13 bill in my wallet . how many each bill ?
# Ibid.
# Question #2
# Soln: There are 2 ten-dollar bills, 8 one-dollar bills, and 3 five-dollar bills.
original input: In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?
count: 13$
total: 4300$
v_1: 100$
v_5: 500$
v_10: 1000$
solve([n_1 = 4 * n_10, n_1 = 4 * n_10, total = n_1 * v_1 + n_5 * v_5 + n_10 * v_10, count = n_1 + n_5 + n_10], [n_1, n_5, n_10, n_1, n_10]);
solution: n_5 = 3, n_1 = 8, n_10 = 2
 
problem: marsha 3 times as many $ 1 bill as $ 5 bill . total $ 32 . how many each bill ?
# Ibid.
# Try These
# Question #1
original input: Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?
total: 3200$
v_1: 100$
v_5: 500$
solve([n_1 = 3 * n_5, n_1 = 3 * n_5, total = n_1 * v_1 + n_5 * v_5], [n_1, n_5]);
solution: n_1 = 12, n_5 = 4
 
problem: vending machine $ 41.25 in . 255 coin total and machine only accepts nickel , dime and quarter . 2 times as many dime as nickel . how many each coin in machine .
# Ibid.
# Question #2
original input: A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine.
v_quarter: 25$
v_dime: 10$
v_nickel: 5$
count: 255$
total: 4125$
solve([n_dime = 2 * n_nickel, total = n_quarter * v_quarter + n_dime * v_dime + n_nickel * v_nickel, count = n_quarter + n_dime + n_nickel], [n_quarter, n_dime, n_nickel]);
solution: n_quarter = 120, n_dime = 90, n_nickel = 45
 
problem: michael had 27 coin in all , valuing $ 4.50 . had only quarter and dime , how many coin each kind ?
# Source: http://voices.yahoo.com/how-set-solve-coin-word-problems-algebra-1713709.html
# Sample Coin Word Problem 1:
# Soln: Michael has 15 dimes and 12 quarters.
original input: Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?
v_quarter: 25$
v_dime: 10$
count: 27$
total: 450$
solve([total = n_quarter * v_quarter + n_dime * v_dime, count = n_quarter + n_dime], [n_quarter, n_dime]);
solution: n_quarter = 12, n_dime = 15
 
problem: lucille had $ 13.25 in nickel and quarter . had 165 coin in all , how many each type coin ?
# Ibid.
# Sample Coin Word Problem 2:
# Soln: Lucille has 25 quarters and 140 nickels.
original input: Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each type of coin did she have?
v_quarter: 25$
v_nickel: 5$
count: 165$
total: 1325$
solve([total = n_quarter * v_quarter + n_nickel * v_nickel, count = n_quarter + n_nickel], [n_quarter, n_nickel]);
solution: n_quarter = 25, n_nickel = 140
 
problem: ben $ 45.25 in quarter and dime . 29 less quarter than dime , how many each type coin ?
# Ibid.
# Sample Coin Word Problem 3:
# Soln: Ben has 150 dimes and 121 quarters.
original input: Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each type of coin does he have?
v_quarter: 25$
v_dime: 10$
total: 4525$
solve([n_quarter = n_dime - 29, total = n_quarter * v_quarter + n_dime * v_dime], [n_quarter, n_dime]);
solution: n_quarter = 121, n_dime = 150
 
problem: person 12 coin consisting dime and penny . total amount money $ 0.30 , how many each coin ?
# Source: http://www.calculatorsoup.com/calculators/wordproblems/algebrawordproblem1.php
solution: n_dime = 2, n_penny = 10</pre>
original input: A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?
 
v_dime: 10$
=={{header|Phix}}==
v_penny: 1$
The title of this task is solving <i>coin</i> problems, therefore I have ruthlessly eradicated stamps, sandwiches, paper, and cards.
count: 12$
It just adds unnecessary fiddling, along with the need to add custom assets and asset-values, such as 37c stamps, $1.50 cards, etc.
total: 30$
Hence this covers 24/28 of the Perl/Go examples. On the plus side, there is no hard limit on the number of unknowns,
solve([total = n_dime * v_dime + n_penny * v_penny, count = n_dime + n_penny], [n_dime, n_penny]);
though all examples below are for 2 and 3 only. A couple (14 and 17) also sail perilously close to getting a divide by zero.
solution: n_dime = 2, n_penny = 10
This task was quite a bit of fun, once I got stuck in.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Solving_coin_problems.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">source</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
If a person has three times as many quarters as dimes and the total amount of money is $5.95, find the number of quarters and dimes.
--==&gt;expected:quarters = 21, dimes = 7
A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38c, find the number of pennies and nickels.
--==&gt;expected:pennies = 13, nickels = 5
A small child has 6 more quarters than nickels. If the total amount of coins is $3.00, find the number of nickels and quarters the child has.
--==&gt;expected:quarters = 11, nickels = 5
A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.
--==&gt;expected:nickels = 21, quarters = 11
A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
--==&gt;expected:dimes = 14, pennies = 7, nickels = 10
In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.
--==&gt;expected:quarters = 9, half_dollars = 3, dimes = 9
A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?
--==&gt;expected:one_dollar_bills = 10, five_dollar_bills = 5, ten_dollar_bills = 4
A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?
--==&gt;expected:quarters = 3, dimes = 5
A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?
--==&gt;expected:dimes = 9, nickels = 3, pennies = 8
A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the number of dimes the person has.
--==&gt;expected:dimes = 11, nickels = 2
A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.
--==&gt;expected:one_dollar_bills = 5, two_dollar_bills = 15
A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.
--==&gt;expected:pennies = 25, nickels = 17, dimes = 20
Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?
--==&gt;expected:dollars = 14, quarters = 12
A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?
--==&gt;expected:nickels = 18, dimes = 9, quarters = 6
A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?
--==&gt;expected:pennies = 9, nickels = 9, dimes = 9
Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?
--==&gt;expected:nickels = 17, dimes = 8
Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?
--==&gt;expected:quarters = 20, dimes = 18
In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?
--==&gt;expected:one_dollar_bills = 8, five_dollar_bills = 3, ten_dollar_bills = 2
Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?
--==&gt;expected:one_dollar_bills = 12, five_dollar_bills = 4
A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine.
--==&gt;expected:nickels = 45, dimes = 90, quarters = 120
Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?
--==&gt;expected:quarters = 12, dimes = 15
Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each type of coin did she have?
--==&gt;expected:nickels = 140, quarters = 25
Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each type of coin does he have?
--==&gt;expected:quarters = 121, dimes = 150
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?
--==&gt;expected:dimes = 2, pennies = 10"""</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">texts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">replacements</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" ."</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">","</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" ,"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"had"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"has"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"contain?"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"have?"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"there?"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"have?"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"has ."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"have?"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"in the bank ."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"have?"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"in the machine ."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"have?"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"as many"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"asmany"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" , and they add up"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" . total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" , has a value"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" . total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" only and has a total"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" . total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" and has a total"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" . total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"All together ,"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A vending machine has"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"valuing"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"coins in all ,"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"coins ."</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"coins total and"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"coins ."</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"find"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"many"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"consists"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"consisting"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"twenty-six"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"26"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"seventeen dollars in value"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"$17.00"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" one "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" 1 "</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"three"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"four"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"twice"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2 times"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ten"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"10"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" and the total"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" . total"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"half dollars"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"half_dollars"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"$1 bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"$2 bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"$5 bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"$10 bill"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ten_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"one-dollar bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"five-dollar bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"five dollar bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"10-dollar bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ten_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"10_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ten_dollar_bills"</span><span style="color: #0000FF;">}}),</span>
<span style="color: #000000;">noise</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"the|a|to|of|i|is|that|it|on|you|this|for|but|with|are|have|be|at|or|was|so|if|out|not|he|she|they|has|do|does"</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"|in|these|person|small|child|child's|bank|pile|clerk|given|put|there|cash|drawer|start|workday|his|suppose|ken"</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"|terry|how|my|marsha|machine|accepts|michael|lucille|ben|number|type|kind|amount|collection|contains|change"</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"|wallet|did|numbers|pocket"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"|"</span><span style="color: #0000FF;">),</span>
<span style="color: #000080;font-style:italic;">-- one spectacularly irksome preamble containing absolutely no useful information whatsoever...:</span>
<span style="color: #000000;">uncle</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"your uncle walks in , jingling the coins in his pocket . "</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"he grins at you and tells you that you can have all the coins "</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"if you can figure out how many of each kind of coin he is carrying . "</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"you're not too interested until he tells you that he's been collecting "</span><span style="color: #0000FF;">&</span>
<span style="color: #008000;">"those gold-tone one-dollar coins . "</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">vocab</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"times"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"asmany"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"quarters"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"as"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"dimes"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"total"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"money"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"many"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"have?"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"coins"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"consisting"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"pennies"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nickels"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"more"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"less"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"than"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"each"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"coin"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"half_dollars"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bill"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"all"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"dollars"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one-half"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"same"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"only"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"one_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ten_dollar_bills"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">assets</span><span style="color: #0000FF;">,</span><span style="color: #000000;">assetv</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"ten_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"five_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">500</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"two_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"one_dollar_bills"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"dollars"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"half_dollars"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"quarters"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"dimes"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"nickels"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"pennies"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}})</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">source</span><span style="color: #0000FF;">,</span><span style="color: #000000;">texts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">replacements</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">expectations</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">vused</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vocab</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">cleanup_lines</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"your uncle"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- note: if you tweak texts/replacements then you may
-- need to tweak the uncle constant to match.</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">uncle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">li</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">uncle</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">noise</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remove_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">noise</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[$]=</span><span style="color: #008000;">"."</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],{</span><span style="color: #008000;">"dimes"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nickels"</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">words</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"have?"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">","</span> <span style="color: #008080;">then</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- re-join eg "$3" and ".99" (oops)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">])></span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">w</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">]}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match_replace</span><span style="color: #0000FF;">({</span><span style="color: #008000;">","</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"many"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"many"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match_replace</span><span style="color: #0000FF;">({</span><span style="color: #008000;">","</span><span style="color: #0000FF;">},</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,{})</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">count</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span>
<span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"--==&gt;expected:"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">li</span><span style="color: #0000FF;">[</span><span style="color: #000000;">15</span><span style="color: #0000FF;">..$]</span>
<span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" ,"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">","</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">expectations</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">expectations</span><span style="color: #0000FF;">,</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">count</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d puzzles:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Step 1: remove noise and otherwise simplify (if nothing else, down to a %d-word vocab):\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vocab</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">count</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">cleanup_lines</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"as"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"consisting"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"all"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"than"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"only"</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">unknowns</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">parse_sentence</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Converts eg {"$1.00","quarters","and","nickels"} to {100,25,5}.
-- An "equation" of {100,25,5} means "100==25*unknown[1]+5*unknown[2]".
-- Obviously this is suitably scruffy, but the 31-word vocab certainly helps!
-- It is worth noting that by this stage most sentences begin or end in a number.
-- Since we may not have the full set of unknowns, each equation ends with a code:
-- 0: pad with 0, 1: pad with 1, 'a': pad with the unknown asset values</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">rest</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">isnumber</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">set_asset_sum</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ww</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ww</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vocab</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ww</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%dc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"$%f"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">f</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">!={}</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iif</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">):</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">={}</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">ww</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">vused</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"times"</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"asmany"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rest</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- eg {"3","times","asmany","quarters","as","dimes"}</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">6</span> <span style="color: #008080;">or</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"as"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">..</span><span style="color: #000000;">6</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">6</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"coins"</span>
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"bills"</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)></span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">--/* eg:
{"18","coins","consisting","pennies","and","nickels"}
{"26","coins","all","dollars","and","quarters"}
{"25","coins","nickels","and","dimes"}
{"33","coins","consisting","nickels","dimes","and","quarters"}
{"27","coins"}
{"20","bills","consisting","one_dollar_bills","and","two_dollar_bills"}
--*/</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$])</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]}&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],{</span><span style="color: #008000;">"more"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"less"</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rest</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
<span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">--/* eg:
{"5","more","pennies","than","nickels"}
{"29","less","quarters","than","dimes"}
--*/</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">5</span> <span style="color: #008080;">or</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"than"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$])</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]}&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">less</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"less"</span><span style="color: #0000FF;">?-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">less</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">less</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">--/* eg:
{"$75","bills"}
{"$45.25","quarters","and","dimes"}
--*/</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)></span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- log assets:
-- eg {"$13.25","nickels","and","quarters"}</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"and"</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]}</span>
<span style="color: #000000;">set_asset_sum</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[$]</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"total"</span>
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">3</span> <span style="color: #008080;">and</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]={</span><span style="color: #008000;">"coins"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"total"</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">--/*
{"total","money","$5.95"}
{"total","coins","38c"}
{"total","coins","$3.00"}
{"total","$3.74"}
{"total","cost","$17.00"}
{"coins","total","$1.44"}
--*/</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[$]}</span>
<span style="color: #000000;">set_asset_sum</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">-- eg {"quarters","and","dimes","38"}</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[$]}&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"one-half"</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- eg {"one-half","asmany","dimes","as","nickels"}</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">5</span> <span style="color: #008080;">or</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"asmany"</span> <span style="color: #008080;">or</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"as"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$])</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"many"</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">--/* eg
{"many","quarters","and","dimes","have?"}
{"many","each","coin","have?"}
{"many","each","have?"}
{"many","each","bill","have?"}
{"many","dimes","have?"}
{"many","coins","each","have?"}
--*/</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[$]!=</span><span style="color: #008000;">"have?"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- no rule, as yet, just outputs everything instead.</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"same"</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- eg {"same","pennies","nickels","and","dimes"}</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$])</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- (p==n)</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- (n==d)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"total"</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- eg {"total","13","bills"}</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">3</span> <span style="color: #008080;">or</span> <span style="color: #008080;">not</span> <span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">or</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"bills"</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">isnumber</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]}&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">--/* eg:
{"one_dollar_bills","five_dollar_bills","and","ten_dollar_bills"}
{"only","nickels","dimes","and","quarters"}
{"only","quarters","and","dimes"}
--*/
-- just log assets:</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">add_unknowns</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">set_asset_sum</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- common code for eg {"total","$3.74"} and {"$75","bills"}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">u</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">uu</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unknowns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">u</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">uu</span><span style="color: #0000FF;">,</span><span style="color: #000000;">assets</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">assetv</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'a'</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rest</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- {sequence s2,unknowns} = parse_sentence(rest,unknowns)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s2</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parse_sentence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">s2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solveN</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Based on https://mathcs.clarku.edu/~djoyce/ma105/simultaneous.html
-- aka the ancient Chinese Jiuzhang suanshu ~100 B.C. (!!)
--
-- Example (ignoring n, which is solely for output):
-- rules = {{18,1,1},{38,1,5}}, ie 18==p+n, 38==p+5*n
-- unknowns = {"pennies","nickels"}
-- expected = "pennies = 13, nickels = 5"
--
-- In the elimination phase, both p have multipliers of 1, so we can
-- ignore those two sq_mul and just do (38=p+5n)-(18=p+n)==&gt;(20=4n).
-- Obviously therefore n is 5 and substituting backwards p is 13.
--</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rj</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">rii</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rji</span>
<span style="color: #000000;">rules</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- successively eliminate (grow lower left triangle of 0s)</span>
<span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">l</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">rii</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rii</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rji</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rji</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rj</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rj</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rii</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rji</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (job done)</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- then substitute each backwards</span>
<span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rii</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (all else should be 0)</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s = %d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">rii</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rji</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rji</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">rji</span><span style="color: #0000FF;">*</span><span style="color: #000000;">rii</span>
<span style="color: #000000;">rj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rj</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %v ==&gt; %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- printf(1,"%d: %s\n",{n,res}) -- (maybe pref.)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">expected</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nStep 2: convert sentences into structures/equations, and solve them:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">count</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],{</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">sentencii</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (one ...but some still contain "and")</span>
<span style="color: #000000;">unknowns</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">sentencii</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parse_sentence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">],</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">sentencii</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">)></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- messy: puzzle has too much info!
-- (14 aka "33 coins" and 17 "Terry" with 38 coins,
-- eliminate wrongly and get a divide by zero...)
-- sentences = sentences[1..length(unknowns)]</span>
<span style="color: #000000;">sentences</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sentences</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">sentences</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sentences</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">)!=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- pad any short equations, eg 3 more nickels than dimes
-- needs a 0 for quarters, if were not mentioned before.</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ss</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sentences</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">padcode</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ss</span><span style="color: #0000FF;">[$]</span>
<span style="color: #000000;">ss</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ss</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">shortlen</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ss</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">shortlen</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">switch</span> <span style="color: #000000;">padcode</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">ss</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shortlen</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">ss</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shortlen</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'a'</span><span style="color: #0000FF;">:</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">u</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">shortlen</span> <span style="color: #008080;">to</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">uu</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unknowns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">u</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">uu</span><span style="color: #0000FF;">,</span><span style="color: #000000;">assets</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ss</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">assetv</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">default</span><span style="color: #0000FF;">:</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">sentences</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ss</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">solveN</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentences</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unknowns</span><span style="color: #0000FF;">,</span><span style="color: #000000;">expectations</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vused</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?{</span><span style="color: #008000;">"unused vocab"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vocab</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
{{out}}
You just gotta love this Pidgin English! The problem numbering system used below is mine alone.<br>
I was slightly unsure whether to interpolate these q&a outputs, but I think the separation chosen has its own merits.<br>
The structures/equations of part 2 are completely unreadable at first, but quite simple really.
<pre>
24 puzzles:
Step 1: remove noise and otherwise simplify (if nothing else, down to a 31-word vocab):
 
1: 3 times asmany quarters as dimes . total money $5.95 . many quarters and dimes have?
2: 18 coins consisting pennies and nickels . total coins 38c . many pennies and nickels have?
3: 6 more quarters than nickels . total coins $3.00 . many nickels and quarters have?
4: 32 coins consisting nickels and quarters . total money $3.80 . many nickels and quarters have?
5: 2 times asmany dimes as pennies and 3 more nickels than pennies . total coins $1.97 . many each coin have?
6: 3 times asmany quarters as half_dollars and 6 more dimes than half_dollars . total money $4.65 . many each coin have?
7: $75 bills . 2 times asmany one_dollar_bills as five_dollar_bills and 1 less ten_dollar_bills than five_dollar_bills . many each bill have?
8: 8 coins consisting quarters and dimes . total $1.25 . many each coin have?
9: 3 times asmany dimes as nickels and 5 more pennies than nickels . total coins $1.13 . many each coin have?
10: 9 more dimes than nickels . total money $1.20 . many dimes have?
11: 20 bills consisting one_dollar_bills and two_dollar_bills . total money $35 . many two_dollar_bills have?
12: 8 more pennies than nickels and 3 more dimes than nickels . total money $3.10 . many dimes have?
13: 26 coins all dollars and quarters . total $17.00 . many each coin have?
14: 33 coins consisting nickels dimes and quarters . total $3.30 . 3 times asmany nickels as quarters and one-half asmany dimes as nickels . many coins each have?
15: same pennies nickels and dimes . coins total $1.44 . many each coin have?
16: 25 coins nickels and dimes . total $1.65 . many each coin have?
17: 2 more quarters than dimes . total $6.80 . quarters and dimes 38 . many quarters and dimes have?
18: one_dollar_bills five_dollar_bills and ten_dollar_bills . total $43 . 4 times asmany one_dollar_bills as ten_dollar_bills . total 13 bills . many each bill have?
19: 3 times asmany one_dollar_bills as five_dollar_bills . total $32 . many each bill have?
20: total $41.25 . 255 coins . only nickels dimes and quarters . 2 times asmany dimes as nickels . many each coin have?
21: 27 coins . total $4.50 . only quarters and dimes . many coins each have?
22: $13.25 nickels and quarters . 165 coins . many each coin have?
23: $45.25 quarters and dimes . 29 less quarters than dimes . many each coin have?
24: 12 coins consisting dimes and pennies . total money $0.30 . many each coin have?
 
Step 2: convert sentences into structures/equations, and solve them:
1: {{0,1,-3},{595,25,10}} ==> quarters = 21, dimes = 7
2: {{18,1,1},{38,1,5}} ==> pennies = 13, nickels = 5
3: {{6,1,-1},{300,25,5}} ==> quarters = 11, nickels = 5
4: {{32,1,1},{380,5,25}} ==> nickels = 21, quarters = 11
5: {{0,1,-2,0},{3,0,-1,1},{197,10,1,5}} ==> dimes = 14, pennies = 7, nickels = 10
6: {{0,1,-3,0},{6,0,-1,1},{465,25,50,10}} ==> quarters = 9, half_dollars = 3, dimes = 9
7: {{7500,100,500,1000},{0,1,-2,0},{1,0,1,-1}} ==> one_dollar_bills = 10, five_dollar_bills = 5, ten_dollar_bills = 4
8: {{8,1,1},{125,25,10}} ==> quarters = 3, dimes = 5
9: {{0,1,-3,0},{5,0,-1,1},{113,10,5,1}} ==> dimes = 9, nickels = 3, pennies = 8
10: {{9,1,-1},{120,10,5}} ==> dimes = 11, nickels = 2
11: {{20,1,1},{3500,100,200}} ==> one_dollar_bills = 5, two_dollar_bills = 15
12: {{8,1,-1,0},{3,0,-1,1},{310,1,5,10}} ==> pennies = 25, nickels = 17, dimes = 20
13: {{26,1,1},{1700,100,25}} ==> dollars = 14, quarters = 12
14: {{33,1,1,1},{330,5,10,25},{0,1,-2,0}} ==> nickels = 18, dimes = 9, quarters = 6
15: {{0,1,-1,0},{0,0,1,-1},{144,1,5,10}} ==> pennies = 9, nickels = 9, dimes = 9
16: {{25,1,1},{165,5,10}} ==> nickels = 17, dimes = 8
17: {{2,1,-1},{38,1,1}} ==> quarters = 20, dimes = 18
18: {{4300,100,500,1000},{0,1,0,-4},{13,1,1,1}} ==> one_dollar_bills = 8, five_dollar_bills = 3, ten_dollar_bills = 2
19: {{0,1,-3},{3200,100,500}} ==> one_dollar_bills = 12, five_dollar_bills = 4
20: {{4125,5,10,25},{255,1,1,1},{0,-2,1,0}} ==> nickels = 45, dimes = 90, quarters = 120
21: {{27,1,1},{450,25,10}} ==> quarters = 12, dimes = 15
22: {{1325,5,25},{165,1,1}} ==> nickels = 140, quarters = 25
23: {{4525,25,10},{29,-1,1}} ==> quarters = 121, dimes = 150
24: {{12,1,1},{30,10,1}} ==> dimes = 2, pennies = 10
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-pattern}}
{{libheader|Wren-str}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./dynamic" for Struct
import "./pattern" for Pattern
import "./str" for Str
import "./sort" for Sort
import "./fmt" for Fmt
 
var Kind = Struct.create("Kind", ["name", "value", "number"])
 
// variable1 = constant1 * variable2 + constant2
var Relation = Struct.create("Relation", ["variable1", "variable2", "constant1", "constant2"])
 
var nums = {
"one-half": "0 times", "one": "1", "two": "2", "three": "3", "four": "4", "five": "5",
"six": "6", "seven": "7", "eight": "8", "nine": "9", "ten": "10", "eleven": "11", "twelve": "12",
"thirteen": "13", "fourteen": "14", "fifteen": "15", "sixteen": "16", "seventeen": "17",
"eighteen": "18", "nineteen": "19", "twenty": "20", "thirty": "30", "forty": "40",
"fifty": "50", "sixty": "60", "seventy": "70", "eighty": "80", "ninety": "90", "hundred": "100"
}
 
var nums2 = {
"twenty-": "2", "thirty-": "3", "forty-": "4",
"fifty-": "5", "sixty-": "6", "seventy-": "7", "eighty-": "8", "ninety-": "9"
}
 
var coins = {
"pennies": 0.01, "nickels": 0.05, "dimes": 0.10, "quarters": 0.25, "half-dollars": 0.50,
"one-dollar": 1.00, "two-dollar": 2.00, "five-dollar": 5.00, "ten-dollar": 10.00
}
 
var bills = {
"$1": "one-dollar", "$2": "two-dollar", "$5": "five-dollar", "$10": "ten-dollar"
}
 
var rx1 = Pattern.new("[/$+1/f|+1/d¢]")
var rx2 = Pattern.new("[pennies|nickels|dimes|quarters|half-dollar|one-dollar|two-dollar|five-dollar|ten-dollar]")
var rx3 = Pattern.new("/s[+1/d]/s")
var rx4 = Pattern.new("[+1/d] times as many [+1/y] as [~she has |][+1/y]")
var rx5 = Pattern.new("[+1/d] more [+1/y] than [~she has |][+1/y]")
var rx6 = Pattern.new("[+1/d] less [+1/y] than [~she has |][+1/y]")
var rx7 = Pattern.new("[+1/d] dollars")
 
var spaced = Fn.new { |s| " %(s) " }
 
// Gets a sorted list of monetary values.
var getValues = Fn.new { |q|
var ss = rx1.findAll(q).map { |m| m.text.trimEnd(".") }.toList
if (ss.count == 0) return []
var res = []
for (s in ss) {
if (s == "") continue
if (s[0] == "$") {
s = s[1..-1]
} else {
s = "." + s[0..-3] // '¢' is 2 bytes
}
var f = Num.fromString(s)
res.add(f)
}
res.sort()
return res
}
 
// Gets a sorted slice of non-monetary integers.
var getNumbers = Fn.new { |q|
var ns = rx3.findAll(q).map { |m| m.text }.toList
if (ns.count == 0) return null
var res = []
for (n in ns) {
var i = Num.fromString(n)
res.add(i)
}
res.sort()
return res
}
 
// Gets the 'kinds' for the problem.
var getKinds = Fn.new { |a|
var num = Num.fromString(a[1])
var kinds = [Kind.new(a[2], 0, 0), Kind.new(a[4], 0, 0)]
var areCoins = false
for (i in 0...kinds.count) {
var v = coins[kinds[i].name]
if (v) {
kinds[i].value = v
areCoins = true
}
}
if (!areCoins) return [0, null]
return [num, kinds]
}
 
// Checks if the problem involves 3 coins and
// also returns their names and the names of the coins which occur most.
var hasThreeCoins = Fn.new { |q|
q = q.replace(".", "").replace(",", "")
var words = q.split(" ")
var coinMap = {}
for (word in words) {
if (coins.containsKey(word)) {
var v = coinMap[word]
if (v) {
coinMap[word] = v + 1
} else {
coinMap[word] = 1
}
}
}
if (coinMap.count != 3) return [null, "", false]
var maxNum = 0
var maxNames = []
var names = []
for (me in coinMap) {
names.add(me.key)
if (me.value > maxNum) {
maxNum = me.value
maxNames = [me.key]
} else if (me.value == maxNum) {
maxNames.add(me.key)
}
}
return [names, maxNames, true]
}
 
var errorMsg = Fn.new { |p|
System.print(p)
System.print("*** CAN'T SOLVE THIS ONE ***\n")
}
 
var printAnswers = Fn.new { |p, kinds|
System.print(p)
System.write("ANSWER:")
var i = 0
for (kind in kinds) {
if (i > 0) System.write(",")
System.write(" %(kind.number) %(kind.name)")
i = i + 1
}
System.print("\n")
}
 
// Processes a problem which involves 3 coins.
var threeCoins = Fn.new { |p, q, names, maxNames|
var relations = []
var am = rx4.findAll(q).map { |m| [m.text] + m.capsText }.toList
for (i in 0...am.count) {
var res = getKinds.call(am[i])
var mult = res[0]
var kinds = res[1]
relations.add(Relation.new(kinds[0].name, kinds[1].name, mult, 0))
}
var mt = rx5.findAll(q).map { |m| [m.text] + m.capsText }.toList
for (i in 0...mt.count) {
var res = getKinds.call(mt[i])
var plus = res[0]
var kinds = res[1]
relations.add(Relation.new(kinds[0].name, kinds[1].name, 1, plus))
}
var lt = rx6.findAll(q).map { |m| [m.text] + m.capsText }.toList
for (i in 0...lt.count) {
var res = getKinds.call(lt[i])
var minus = res[0]
var kinds = res[1]
relations.add(Relation.new(kinds[0].name, kinds[1].name, 1, -minus))
}
var le = relations.count
if (le > 2) {
errorMsg.call(p)
return
}
if (le == 0) { // numbers of each coin must be the same
var sum = 0
for (name in names) sum = sum + coins[name]
var tv = getValues.call(q)[-1]
var n = (tv/sum + 0.5).floor
var kinds = []
for (name in names) kinds.add(Kind.new(name, 0, n))
printAnswers.call(p, kinds)
} else {
var totalValue = getValues.call(q)[-1]
for (maxName in maxNames) {
for (i in 0...le) {
if (relations[i].constant1 == 0) {
relations[i].constant1 = 0.5 // deals with 'one-half' cases
}
if (le == 2 && maxName == relations[i].variable1) {
var v = relations[i].variable2
relations[i].variable1 = v
relations[i].variable2 = maxName
relations[i].constant1 = 1 / relations[i].constant1
relations[i].constant2 = -relations[i].constant2
}
}
var tv = totalValue
var v1 = ""
var v2 = ""
var v3 = ""
var n1 = 0
var n2 = 0
var n3 = 0
if (le == 2) {
var tmc = coins[relations[0].variable1] * relations[0].constant1 +
coins[relations[1].variable1] * relations[1].constant1 + coins[maxName]
tv = tv - coins[relations[0].variable1] * relations[0].constant2 -
coins[relations[1].variable1] * relations[1].constant2
v1 = maxName
v2 = relations[0].variable1
v3 = relations[1].variable1
n1 = (tv/tmc + 0.5).floor
n2 = (relations[0].constant1*n1 + relations[0].constant2 + 0.5).floor
n3 = (relations[1].constant1*n1 + relations[1].constant2 + 0.5).floor
} else {
var tn = getNumbers.call(q)[-1]
v1 = relations[0].variable1
v2 = relations[0].variable2
for (name in names) {
if (name != v1 && name != v2) {
v3 = name
break
}
}
var mult1 = coins[v1]
var mult2 = coins[v2]
var mult3 = coins[v3]
n2 = (((tn-relations[0].constant2)*mult3-tv+relations[0].constant2*mult1)/
((relations[0].constant1+1)*mult3-relations[0].constant1*mult1-mult2) + 0.5).floor
n1 = (n2*relations[0].constant1 + relations[0].constant2 + 0.5).floor
n3 = tn.floor - n1 - n2
}
var calcValue = n1 * coins[v1] + n2 * coins[v2] + n3 * coins[v3]
if ((totalValue - calcValue).abs <= 1e-14) {
var kinds = [Kind.new(v1, 0, n1), Kind.new(v2, 0, n2), Kind.new(v3, 0, n3)]
printAnswers.call(p, kinds)
return
}
}
errorMsg.call(p)
}
}
 
var ps = [
"If a person has three times as many quarters as dimes and the total amount of money is $5.95, find the number of quarters and dimes.",
"A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38¢, find the number of pennies and nickels.",
"A small child has 6 more quarters than nickels. If the total amount of coins is $3.00, find the number of nickels and quarters the child has.",
"A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.",
"A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.",
"In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.",
"A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps is $3.74, find the number of each type of the stamps purchased.",
"A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number of each sold.",
"An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and paid $15.71, how many reams of each type of paper did the student purchase?",
"A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?",
"A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?",
"A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?",
"A person bought ten greeting cards consisting of birthday cards costing $1.50 each and anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the number of each kind of card the person bought.",
"A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the number of dimes the person has.",
"A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.",
"A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.",
"Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?",
"A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?",
"A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?",
"Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?",
"Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?",
"In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?",
"Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?",
"A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine?",
"Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?",
"Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each type of coin did she have?",
"Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each type of coin does he have?",
"A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?"
]
for (p in ps) {
var q = Str.lower(p).replace("twice", "two times").replace(" does ", " has ")
for (d in ["half", "one", "two", "five", "ten"]) {
q = q.replace(d + " dollar", d + "-dollar")
}
for (me in nums) {
q = q.replace(spaced.call(me.key), spaced.call(me.value))
}
for (me in nums2) {
q = q.replace(me.key, me.value)
}
for (me in nums) {
q = q.replace(me.key + " ", me.value + " ")
}
for (me in bills) {
q = q.replace(me.key + " ", me.value + " ")
}
q = q.replace(" bills", "").replace(" bill", "")
// check if there are 3 coins involved
var res = hasThreeCoins.call(q)
if (res[2]){
threeCoins.call(p, q, res[0], res[1])
continue
}
var am = rx4.findAll(q).map { |m| [m.text] + m.capsText }.toList
if (am.count == 1) {
var res = getKinds.call(am[0])
var mult = res[0]
var kinds = res[1]
if (!kinds) {
errorMsg.call(p)
continue
}
var tv = getValues.call(q)[-1]
kinds[1].number = (tv/(mult*kinds[0].value + kinds[1].value) + 0.5).floor
kinds[0].number = kinds[1].number * mult
printAnswers.call(p, kinds)
continue
}
var mt = rx5.findAll(q).map { |m| [m.text] + m.capsText }.toList
if (mt.count == 1) {
var res = getKinds.call(mt[0])
var plus = res[0]
var kinds = res[1]
if (!kinds) {
errorMsg.call(p)
continue
}
var tv = getValues.call(q)[-1]
kinds[1].number = ((tv-plus*kinds[0].value)/(kinds[0].value + kinds[1].value) + 0.5).floor
kinds[0].number = kinds[1].number + plus
printAnswers.call(p, kinds)
continue
}
var lt = rx6.findAll(q).map { |m| [m.text] + m.capsText }.toList
if (lt.count == 1) {
var res = getKinds.call(lt[0])
var minus = res[0]
var kinds = res[1]
if (!kinds) {
errorMsg.call(p)
continue
}
var tv = getValues.call(q)[-1]
kinds[1].number = ((tv+minus*kinds[0].value)/(kinds[0].value + kinds[1].value) + 0.5).floor
kinds[0].number = kinds[1].number - minus
printAnswers.call(p, kinds)
continue
}
res = getValues.call(q)
var tv = 0
if (res.count > 0) {
tv = res[-1]
} else {
var res3 = rx7.findAll(q).map { |m| [m.text] + m.capsText }.toList
tv = Num.fromString(res3[0][1])
}
var tn = getNumbers.call(q)[-1]
var coinNames = rx2.findAll(q).map { |m| m.text }.toList
Sort.insertion(coinNames)
var kinds = []
if (coinNames.count > 0) {
kinds.add(Kind.new(coinNames[0], coins[coinNames[0]], 0))
for (i in 1...coinNames.count) {
if (coinNames[i] != coinNames[i-1]) {
kinds.add(Kind.new(coinNames[i], coins[coinNames[i]], 0))
}
}
if (kinds.count != 2) {
errorMsg.call(p)
continue
}
} else if (res.count >= 3) {
kinds.add(Kind.new(Fmt.swrite("$$$.2f item", res[0]), res[0], 0))
for (i in 1...res.count-1) {
if (res[i] != res[i-1]) {
kinds.add(Kind.new(Fmt.swrite("$$$.2f item", res[i]), res[i], 0))
}
}
if (kinds.count!= 2) {
errorMsg.call(p)
continue
}
} else {
errorMsg.call(p)
continue
}
kinds[0].number = ((tv-tn*kinds[1].value)/(kinds[0].value-kinds[1].value) + 0.5).floor
kinds[1].number = tn - kinds[0].number
printAnswers.call(p, kinds)
}</syntaxhighlight>
 
{{out}}
<pre>
If a person has three times as many quarters as dimes and the total amount of money is $5.95, find the number of quarters and dimes.
ANSWER: 21 quarters, 7 dimes
 
A pile of 18 coins consists of pennies and nickels. If the total amount of the coins is 38¢, find the number of pennies and nickels.
ANSWER: 5 nickels, 13 pennies
 
A small child has 6 more quarters than nickels. If the total amount of coins is $3.00, find the number of nickels and quarters the child has.
ANSWER: 11 quarters, 5 nickels
 
A child's bank contains 32 coins consisting of nickels and quarters. If the total amount of money is $3.80, find the number of nickels and quarters in the bank.
ANSWER: 21 nickels, 11 quarters
 
A person has twice as many dimes as she has pennies and three more nickels than pennies. If the total amount of the coins is $1.97, find the numbers of each type of coin the person has.
ANSWER: 7 pennies, 14 dimes, 10 nickels
 
In a bank, there are three times as many quarters as half dollars and 6 more dimes than half dollars. If the total amount of the money in the bank is $4.65, find the number of each type of coin in the bank.
ANSWER: 3 half-dollars, 9 quarters, 9 dimes
 
A person bought 12 stamps consisting of 37¢ stamps and 23¢ stamps. If the cost of the stamps is $3.74, find the number of each type of the stamps purchased.
ANSWER: 5 $0.23 item, 7 $0.37 item
 
A dairy store sold a total of 80 ice cream sandwiches and ice cream bars. If the sandwiches cost $0.69 each and the bars cost $0.75 each and the store made $58.08, find the number of each sold.
ANSWER: 32 $0.69 item, 48 $0.75 item
 
An office supply store sells college-ruled notebook paper for $1.59 a ream and wide-ruled notebook paper for $2.29 a ream. If a student purchased 9 reams of notebook paper and paid $15.71, how many reams of each type of paper did the student purchase?
ANSWER: 7 $1.59 item, 2 $2.29 item
 
A clerk is given $75 in bills to put in a cash drawer at the start of a workday. There are twice as many $1 bills as $5 bills and one less $10 bill than $5 bills. How many of each type of bill are there?
ANSWER: 5 five-dollar, 10 one-dollar, 4 ten-dollar
 
A person has 8 coins consisting of quarters and dimes. If the total amount of this change is $1.25, how many of each kind of coin are there?
ANSWER: 5 dimes, 3 quarters
 
A person has 3 times as many dimes as he has nickels and 5 more pennies than nickels. If the total amount of these coins is $1.13, how many of each kind of coin does he have?
ANSWER: 3 nickels, 9 dimes, 8 pennies
 
A person bought ten greeting cards consisting of birthday cards costing $1.50 each and anniversary cards costing $2.00 each. If the total cost of the cards was $17.00, find the number of each kind of card the person bought.
ANSWER: 6 $1.50 item, 4 $2.00 item
 
A person has 9 more dimes than nickels. If the total amount of money is $1.20, find the number of dimes the person has.
ANSWER: 11 dimes, 2 nickels
 
A person has 20 bills consisting of $1 bills and $2 bills. If the total amount of money the person has is $35, find the number of $2 bills the person has.
ANSWER: 5 one-dollar, 15 two-dollar
 
A bank contains 8 more pennies than nickels and 3 more dimes than nickels. If the total amount of money in the bank is $3.10, find the number of dimes in the bank.
ANSWER: 17 nickels, 25 pennies, 20 dimes
 
Your uncle walks in, jingling the coins in his pocket. He grins at you and tells you that you can have all the coins if you can figure out how many of each kind of coin he is carrying. You're not too interested until he tells you that he's been collecting those gold-tone one-dollar coins. The twenty-six coins in his pocket are all dollars and quarters, and they add up to seventeen dollars in value. How many of each coin does he have?
ANSWER: 14 one-dollar, 12 quarters
 
A collection of 33 coins, consisting of nickels, dimes, and quarters, has a value of $3.30. If there are three times as many nickels as quarters, and one-half as many dimes as nickels, how many coins of each kind are there?
ANSWER: 18 nickels, 6 quarters, 9 dimes
 
A wallet contains the same number of pennies, nickels, and dimes. The coins total $1.44. How many of each type of coin does the wallet contain?
ANSWER: 9 pennies, 9 dimes, 9 nickels
 
Suppose Ken has 25 coins in nickels and dimes only and has a total of $1.65. How many of each coin does he have?
ANSWER: 8 dimes, 17 nickels
 
Terry has 2 more quarters than dimes and has a total of $6.80. The number of quarters and dimes is 38. How many quarters and dimes does Terry have?
ANSWER: 20 quarters, 18 dimes
 
In my wallet, I have one-dollar bills, five-dollar bills, and ten-dollar bills. The total amount in my wallet is $43. I have four times as many one-dollar bills as ten-dollar bills. All together, there are 13 bills in my wallet. How many of each bill do I have?
ANSWER: 8 one-dollar, 2 ten-dollar, 3 five-dollar
 
Marsha has three times as many one-dollar bills as she does five dollar bills. She has a total of $32. How many of each bill does she have?
ANSWER: 12 one-dollar, 4 five-dollar
 
A vending machine has $41.25 in it. There are 255 coins total and the machine only accepts nickels, dimes and quarters. There are twice as many dimes as nickels. How many of each coin are in the machine?
ANSWER: 90 dimes, 45 nickels, 120 quarters
 
Michael had 27 coins in all, valuing $4.50. If he had only quarters and dimes, how many coins of each kind did he have?
ANSWER: 15 dimes, 12 quarters
 
Lucille had $13.25 in nickels and quarters. If she had 165 coins in all, how many of each type of coin did she have?
ANSWER: 140 nickels, 25 quarters
 
Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each type of coin does he have?
ANSWER: 121 quarters, 150 dimes
 
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?
ANSWER: 2 dimes, 10 pennies
</pre>
 
9,476

edits