-2

A cash drawer contains 160 bills, all 10s and 50s. The total value of the 10s and 50s is $1,760.

How many of each type of bill are in the drawer? You can figure this out by trial and error (or by doing algebra with pencil and paper), but try to use loops and conditionals to check a plausible possibilities and stop when you find the correct one.

Algebraically, on a piece of paper I figured out that it was four $50 bills and one hundred and fifty six $10 bills. This is because

  1. x+y=160

  2. 10x+50y=1760

    x=160-y

    10x=1600-10y

    1600-10y+50y=1760

    1600-40y=1760

    40y=1760

    y=4

    x=156

how would I make a model that i could code to solve this or any other version of this problem? I only know the very basic syntax of python as i've never ever programmed before this.

tobias_k
  • 81,265
  • 12
  • 120
  • 179

4 Answers4

1

Using numpy for the system:

x+y=160

10x+50y=1760
import numpy as np
a = np.array([[1, 1], [10, 50]])
b = np.array([160, 1760])
x = np.linalg.solve(a, b)
print(x)

Outputs:

[156.   4.]
Jmonsky
  • 1,519
  • 1
  • 9
  • 16
1

There are a total of two variables, the number of tens and the number of fifties. So you could do nested for-loops. A really blunt way of doing this would be:

 for fifties in range(161):
      for tens in range(161-fifties):
            if (fifties+tens == 160) and (50*fifties + 10*tens == 1760):
                 break

We can improve that a bit by noting that each time we increase the number of fifties, we decrease the possible number of tens:

 for fifties in range(161):
      for tens in range(161-fifties):
            if (fifties+tens == 160) and (50*fifties + 10*tens == 1760:
                 break

It can be improved even further by noting that although there are a total of two variables, we have the constraint that they add up to 160. Thus, we can use this constraint to get one given the other:

 for fifties in range(161):
      tens = 160 - fifties
      if 50*fifties + 10*tens == 1760:
           break
Acccumulation
  • 3,491
  • 1
  • 8
  • 12
0

You can take a dynamic programming approach to this for a general solution:

Set up some edge conditions where there is no answer:

  1. If the length of notes it less than 1
  2. You have one note but the total is not divisible by the denomination there's no answer.

The take a note off and see if it works by recursively calling the function with the remaining notes and adjusted bill-count/total.

def countNotes(notes, total, bills):
    if len(notes) < 1:
        return  
    if len(notes) == 1:
        return [bills] if bills * notes[0] == total else None

    for count in range(bills+1):
        amount = notes[0] * count
        rest = countNotes(notes[1:], total - amount, bills - count)
        if rest:
            return [count, *rest]

countNotes([50, 10], 1760, 160)
#[4, 156]

countNotes([100, 20, 5], 173, 2)
# None -- not possible

countNotes([100, 20, 5, 2], 1255, 61)
#[1, 57, 3, 0]

This will return the counts as a list in the same order as the notes passed in. If you're dealing with large lists and totals if could be improved by memoizing it.

Mark
  • 90,562
  • 7
  • 108
  • 148
0
d = {'x': 10, 'y': 50} #create dictionary to hold bill values
total_value = 1760
num_bills = 160

y = (total_value - num_bills *d['x']) / (d['y']-d['x']) #isolating y 

x = num_bills - y # isolating x with y value known

print("the number of ten dollar bills is: " + str(x))
print("the number of fifty dollar bills is: " + str(y))
Tonyag
  • 1