# Thread: Programming Puzzle 10 - Sweets on a budget

1. ## Programming Puzzle 10 - Sweets on a budget

Your sales manager is headed to the candy store and needs your help figuring out how much money her purchase will require. When she first visited the store she bought one pound of jellybeans and two pounds of chocolate for \$2.00. A week later, she bought four pounds of caramels and one pound of jellybeans paying \$3.00. The next week, she bought three pounds of licorice, one pound of jellybeans and one pound of caramels for \$1.50. Now she wants to buy one pound of each of the four candies. How much money will this cost?

Instructions:

a) you may assume these ridiculously low prices have remained fixed;

b) write an xbasic script that solves the puzzle;

c) your script should display your solution in a message box on screen;

Good luck.

PS - Credit to Creative Computing Magazine

2. ## Re: Programming Puzzle 10 - Sweets on a budget

I first tried to solve this algebraically with pencil and paper. I ended up proving that 1.5 = 1.5.

Then I tried XY line charts and plotting intercepts, which resulted in x = .5 and y =1 but that didn't help me with how many jellybeans are in a pound of chocolate .

I next tried using my graphing calculator and creating matrix's but alas my matrix was not properly dimmed.

So I gave up and wrote a brute force approach using for...next loops.

3. ## Re: Programming Puzzle 10 - Sweets on a budget

So I had a typo in my script

Line 61 should have been
Code:
`IF 1 * vJellybeansN + 2 * vChocolateN = 200 THEN`

4. ## Re: Programming Puzzle 10 - Sweets on a budget

Very nice!

Turns out there are multiple pricing schemes that fit the puzzle criteria.

5. ## Re: Programming Puzzle 10 - Sweets on a budget

To Tom and everyone,
I think lot of people would be interested to solve this manually in less than 5 mins first even before start to program. The best way I could think to solve this puzzle is to use Matrix and my favorite "rref" function.
Step 1:
Set up matrix:
[ 1 2 0 0 2 ] ' first digit refers to jelly, second - chocolate, third - caramel, forth - licorice
[ 1 0 4 0 3 ] ' fifth - total amount paid
[ 1 0 1 3 1.5 ]
[ 3 2 5 3 7.5 ] ' last row is total number of each item and total amount paid (not necessary, just used to make four row because we have 4 items)

1st row means 1 jelly + 2 chocolate = \$2
2nd row means 1 jelly + 4 caramel = \$3
3rd row means 1 jelly + 1 caramel = 3 licorice = \$1.5

after using calculator or can use internet to solve above matrix with rref we get
[1 0 0 4 0]
[0 1 0 -2 0]
[0 0 1 -1 0]
[0 0 0 0 1]

check the last row - all 0 means nothing costs \$1 --- which proves there is no valid answer to this question..

If there were some answer then the solution would look like this:
[1 0 0 0 5]
[0 1 0 0 6]
[0 0 1 0 7]
[0 0 0 1 8] ' 5,6,7,8 are just arbitrary numbers....

6. ## Re: Programming Puzzle 10 - Sweets on a budget

Attachment 28503Thanks, Pratik.

Must be something wrong somewhere, cause here's a list of valid solutions.

7. ## Re: Programming Puzzle 10 - Sweets on a budget

I know there wasn't a unique answer to this problem but was assuming that there wasn't any.. I will research and come back soon...

8. ## Re: Programming Puzzle 10 - Sweets on a budget

hi, back again,,
I found my mistake. Since I was trying to solve really fast I mis-calculated the total amout paid that is \$6.5 instead of \$7.5
now according to my method above my new rref answer is
[1 0 0 4 1]
[0 1 0 -2 0.5]
[0 0 1 -1 0.5]
[0 0 0 0 0]

Now that makes sense that there are lot of solutions to this problem.
Now the solutions are:
jelly = 1 - 4x
chocolate = 0.5 + 2x
caramel = 0.5 + x
licorice = x

So, as you can see, you can pick any value of x and there is your solution (infinity.......).
Now x will work with negative numbers too but you guys are smart to pick it as positive and less than 1.5 as you paid max \$1.5 when buying licorice...

Pratik

9. ## Re: Programming Puzzle 10 - Sweets on a budget

puz 10 attempt.

puz 10.txt

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•