Introduction to Computer Science
CompSci 101 : Fall 2013

Problem Solving

After you have worked out an algorithm for solving a problem, you can begin to write a program that implements your algorithm. Try the following programs.

  1. Flipping Pancakes

    Imagine you are a short-order cook in a pancake restaurant, so you need to cook pancakes as fast as possible. You have one pan that can fit some number of pancakes at a time. Using this pan you must cook all the pancakes in the order. Each pancake must be cooked for five minutes on each side, and once a pancake starts cooking on a side it has to cook for five minutes on that side. However, you can take a pancake out of the pan when you are ready to flip it after five minutes and put it back in the pan later to cook it on the other side. For example, if you need to cook 3 pancakes and your pan only holds 2 pancakes at a time, it would take 15 miuntes to cook all three pancakes because you could cook pancakes 1 and 2 on one side for five minutes, then take pancake 2 out of the pan, flip pancake 1 and start cooking pancake 3. After 10 minutes, pancake 1 is done so you take it out, pancake 3 can be flipped, and you can put pancake 2 back in the pan (since you removed the first pancake).

    1. Write a Python program to determine the shortest time needed to do any number of pancakes.

  2. Adding Time

    Consider the problem of adding units of measure together (like minutes and seconds). When you add the smaller units, e.g., seconds, they may "overrun" into the larger unit, e.g., minutes, you must update the larger unit and remove its value from the seconds total. For example, adding 20 seconds to 30 seconds results in 50 total seconds. But adding 20 seconds to 50 seconds, results in 1 minute and 10 seconds.

    1. Write a program to add time, i.e., adds together minutes and seconds and outputs the result in hours, minutes, and seconds
    2. What cases might you try to determine if your program works?
    3. Change your program to add distances, i.e., adds together feet and inches and outputs the result in miles, feet, and inches
    4. Change your program to add money, i.e., adds together English shillings and pence and outputs the result in pounds, shillings, and pence
    5. Write a general Python method to add any two units together

       

  3. Pricing the Movies

    Consider the problem facing the owner of a movie theater who has complete freedom in setting ticket prices. The more he charges, the fewer the people who can afford tickets. In a recent experiment the owner determined a precise relationship between the price of a ticket and average attendance. At a price of $5.00 per ticket, 120 people attend a performance. Decreasing the price by a dime ($.10) increases attendance by 15. Unfortunately, the increased attendance also comes at an increased cost. Every performance costs the owner $180. Each attendee costs another four cents ($0.04). The owner would like to know the exact relationship between profit and ticket price so that he can determine the price at which he can make the highest profit.

    1. Describe an algorithm that computes the theater's profit based on a given ticket price.
    2. Choose several reasonable values to test which one might yeild the most profit.
    3. Choose several reasonable values to test which one might yeild no profit.
    4. Describe an algorithm to find which ticket price would yeild the most profit.