Get a Jumpstart with our Sample Apps
Results 1 to 9 of 9

Thread: Programming Puzzle 10 - Sweets on a budget

  1. #1
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default 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;

    d) your script should be exported to a text file and attached to your answer here.

    Good luck.

    PS - Credit to Creative Computing Magazine

  2. #2
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default 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.
    Attached Files Attached Files

  3. #3
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default 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
    Corrected script uploaded
    Attached Files Attached Files

  4. #4
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 10 - Sweets on a budget

    Very nice!

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

  5. #5
    Member pmanandhar's Avatar
    Real Name
    Pratik
    Join Date
    Apr 2011
    Location
    Sterling Heights, Michigan, NEPAL
    Posts
    15

    Default 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. #6
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default 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. #7
    Member pmanandhar's Avatar
    Real Name
    Pratik
    Join Date
    Apr 2011
    Location
    Sterling Heights, Michigan, NEPAL
    Posts
    15

    Default 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. #8
    Member pmanandhar's Avatar
    Real Name
    Pratik
    Join Date
    Apr 2011
    Location
    Sterling Heights, Michigan, NEPAL
    Posts
    15

    Default 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. #9
    Member
    Real Name
    Jeb Richardson
    Join Date
    Aug 2011
    Location
    Bowling Green, KY
    Posts
    26

    Default Re: Programming Puzzle 10 - Sweets on a budget

    puz 10 attempt.

    puz 10.txt

Similar Threads

  1. Programming Puzzle 1 - Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 32
    Last Post: 12-26-2011, 02:38 PM
  2. Programming Puzzle 3 - Fractions, anyone?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 7
    Last Post: 11-08-2011, 06:40 AM
  3. Programming Puzzle 7 - Yet another nested loop!
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 4
    Last Post: 08-08-2011, 06:41 PM
  4. Programming Puzzle 6 - Another nested loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 6
    Last Post: 08-08-2011, 05:30 PM
  5. Programming Puzzle 5 - Nested loops
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 10
    Last Post: 08-08-2011, 03:06 PM

Posting Permissions

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