Solving coin problems: Difference between revisions

Added Go
m (A category goes in the task template, not the title)
(Added Go)
Line 27:
 
The program should take coin problems written in plain english and output the solutions. The solutions needn't be output in English.
 
=={{header|Go}}==
This relatively simple program can only solve problems with 2 types of coins (or other objects) and consequently doesn't need an equation solver. However, it is able to solve the 19 problems of this type listed in the Perl entry.
<lang go>package main
 
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
)
 
type kind struct {
name string
value float64
number int
}
 
var nums = map[string]string{
"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", "thirty": "30", "forty": "40",
"fifty": "50", "sixty": "60", "seventy": "70", "eighty": "80", "ninety": "90",
"hundred": "100"}
 
var nums2 = map[string]string{
"twenty-": "2", "thirty-": "3", "forty-": "4",
"fifty-": "5", "sixty-": "6", "seventy-": "7", "eighty-": "8", "ninety-": "9"}
 
var coins = map[string]float64{
"pennies": 0.01, "nickels": 0.05, "dimes": 0.10, "quarters": 0.25, "half-dollar": 0.50,
"one-dollar": 1.00, "two-dollar": 2.00, "five-dollar": 5.00, "ten-dollar": 10.00}
 
var (
rx1 = regexp.MustCompile(`\$\d+(\.\d+)?|\d+¢`)
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 )?([-\w]+)`)
rx5 = regexp.MustCompile(`(\d+) more ([-\w]+) than (s?he does )?([-\w]+)`)
rx6 = regexp.MustCompile(`(\d+) less ([-\w]+) than (s?he does )?([-\w]+)`)
rx7 = regexp.MustCompile(`(\d+) dollars`)
)
 
func spaced(s string) string {
return fmt.Sprintf(" %s ", s)
}
 
// Gets a sorted slice of monetary values.
func getValues(q string) []float64 {
ss := rx1.FindAllString(q, -1)
if ss == nil {
return nil
}
var res []float64
for _, s := range ss {
if len(s) == 0 {
continue
}
if s[0] == '$' {
s = s[1:]
} else {
s = "." + s[:len(s)-2] // '¢' is 2 bytes
}
f, _ := strconv.ParseFloat(s, 64)
res = append(res, f)
}
sort.Float64s(res)
return res
}
 
// Gets a sorted slice of non-monetary integers.
func getNumbers(q string) []int {
ns := rx3.FindAllString(q, -1)
if ns == nil {
return nil
}
var res []int
for _, n := range ns {
i, _ := strconv.Atoi(strings.TrimSpace(n))
res = append(res, i)
}
sort.Ints(res)
return res
}
 
// Gets the 'kinds' for the problem.
func getKinds(amas [][]string) (int, []kind) {
a := amas[0]
num, _ := strconv.Atoi(a[1])
kinds := []kind{{a[2], 0, 0}, {a[4], 0, 0}}
areCoins := false
for i := range kinds {
if v, ok := coins[kinds[i].name]; ok {
kinds[i].value = v
areCoins = true
}
}
if !areCoins {
return 0, nil
}
return num, kinds
}
 
func printAnswers(p string, kinds []kind) {
fmt.Println(p)
fmt.Printf("ANSWER: %d %s, %d %s\n\n", kinds[0].number, kinds[0].name, kinds[1].number, kinds[1].name)
}
 
func errorMsg(p string) {
fmt.Println(p)
fmt.Println("*** CAN'T SOLVE THIS ONE ***\n")
}
 
func main() {
ps := []string{
"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 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 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 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.",
"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?",
"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?",
"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?",
"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 := range ps {
q := strings.ToLower(p)
q = strings.ReplaceAll(q, "twice", "two times")
for _, d := range []string{"half", "one", "two", "five", "ten"} {
q = strings.ReplaceAll(q, d+" dollar", d+"-dollar")
}
for k, v := range nums {
q = strings.ReplaceAll(q, spaced(k), spaced(v))
}
for k, v := range nums2 {
q = strings.ReplaceAll(q, k, v)
}
for k, v := range nums {
q = strings.ReplaceAll(q, k+" ", v+" ")
}
q = strings.ReplaceAll(q, " bills", "")
am := rx4.FindAllStringSubmatch(q, -1)
if len(am) == 1 {
mult, kinds := getKinds(am)
if kinds == nil {
errorMsg(p)
continue
}
res := getValues(q)
tv := res[len(res)-1]
fmult := float64(mult)
kinds[1].number = int(tv/(fmult*kinds[0].value+kinds[1].value) + 0.5)
kinds[0].number = kinds[1].number * mult
printAnswers(p, kinds)
continue
}
mt := rx5.FindAllStringSubmatch(q, -1)
if len(mt) == 1 {
plus, kinds := getKinds(mt)
if kinds == nil {
errorMsg(p)
continue
}
res := getValues(q)
tv := res[len(res)-1]
fplus := float64(plus)
kinds[1].number = int((tv-fplus*kinds[0].value)/(kinds[0].value+kinds[1].value) + 0.5)
kinds[0].number = kinds[1].number + plus
printAnswers(p, kinds)
continue
}
lt := rx6.FindAllStringSubmatch(q, -1)
if len(lt) == 1 {
minus, kinds := getKinds(lt)
if kinds == nil {
errorMsg(p)
continue
}
res := getValues(q)
tv := res[len(res)-1]
fminus := float64(minus)
kinds[1].number = int((tv+fminus*kinds[0].value)/(kinds[0].value+kinds[1].value) + 0.5)
kinds[0].number = kinds[1].number - minus
printAnswers(p, kinds)
continue
}
res := getValues(q)
var tv float64
if len(res) > 0 {
tv = res[len(res)-1]
} else {
res3 := rx7.FindAllStringSubmatch(q, -1)
i, _ := strconv.Atoi(res3[0][1])
tv = float64(i)
}
res2 := getNumbers(q)
tn := res2[len(res2)-1]
coinNames := rx2.FindAllString(q, -1)
sort.Strings(coinNames)
var kinds []kind
if len(coinNames) > 0 {
kinds = append(kinds, kind{coinNames[0], coins[coinNames[0]], 0})
for i := 1; i < len(coinNames); i++ {
if coinNames[i] != coinNames[i-1] {
kinds = append(kinds, kind{coinNames[i], coins[coinNames[i]], 0})
}
}
if len(kinds) != 2 {
errorMsg(p)
continue
}
} else if len(res) >= 3 {
kinds = append(kinds, kind{fmt.Sprintf("$%.2f item", res[0]), res[0], 0})
for i := 1; i < len(res)-1; i++ {
if res[i] != res[i-1] {
kinds = append(kinds, kind{fmt.Sprintf("$%.2f item", res[i]), res[i], 0})
}
}
if len(kinds) != 2 {
errorMsg(p)
continue
}
} else {
errorMsg(p)
continue
}
ftn := float64(tn)
kinds[0].number = int((tv-ftn*kinds[1].value)/(kinds[0].value-kinds[1].value) + 0.5)
kinds[1].number = tn - kinds[0].number
printAnswers(p, kinds)
}
}</lang>
 
{{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 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 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 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 $1.00 item, 15 $2.00 item
 
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
 
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
 
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
 
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>
 
=={{header|Perl}}==
9,482

edits