Prelab 2: Everyday Algorithms

We can use algorithms to describe ordinary activities in our everyday life. For example, we can consider a recipe as an algorithm for cooking a particular food.

We input the ingredients, follow Steps 1-3, and output an Easy Pizza Brownie! Furthermore, Steps 1-3 can be viewed as three subroutines which may in turn be further divided broken into simpler subroutines. In Step 1, the first thing we are told to do is Heat oven to 350° F. This can be rewritten as a subroutine declaration by heatOven(temp), where temp is the input temperature value for the subroutine heatOven for heating up the oven. We can rewrite all of Step 1 as follows:

StepOne() { heatOven(350); grease(pizza_pan OR baking_pan); if(isDisposable(pizza_pan) = TRUE) place(pizza_pan,cookie_sheet); }

Problem 1: Design subroutines for Steps 2 and 3.


Algorithms for Getting Dressed

How complicated is it for people to get dressed? We will try to figure this out using what we learned in class about problem solving and algorithm design.

Let's call our algorithm GetDressed(). Since getting dressed involves putting on various items of clothing, we'll need a subroutine putOn(clothing), where putOn is the name of the subroutine, clothing is its input value, and the subroutine contains instructions for putting on the input item of clothing. The variable clothing may be any particular item of clothing available to you -- in other words, what's in your closet.

Suppose you have the following items of clothing in your closet:

My Closet
blue_jeans black_jeans brown_pants black_pants
blue_shirt red_shirt sweatshirt sweater
sandals shoes sneakers boots
white_socks black_socks orange_socks wool_socks
shorts tshirt belt jacket

Scenario 1: Suppose you know exactly what you want to wear: your blue jeans, belt, red shirt, white socks and sneakers. Then getting dressed is a simple sequential process. The only thing you need to worry about when designing your algorithm is the order in which to put on the items of clothing.

GetDressed() { putOn(red_shirt); putOn(blue_jeans); putOn(belt); putOn(white_socks); putOn(sneakers); }

Scenario 2: Suppose you want to wear jeans, a tshirt and sandals but one pair of jeans is dirty. Then getting dressed will involve a selection of clothing, depending on what items are dirty. To determine if an item of clothing is dirty, we declare a subroutine isDirty(clothing) that is true if the item of clothing is dirty and false otherwise.

GetDressed() { putOn(tshirt); if(isDirty(blue_jeans) = TRUE) putOn(black_jeans); else putOn(blue_jeans); putOn(sandals); }

Scenario 3: Suppose we have a subroutine int temperature() that gives us the temperature outside. We want to design an algorithm that selects between three different outfits depending on whether it is cold, warm, or hot outdoors.

GetDressed() { if(temperature > 80) putOn(tshirt); putOn(shorts); putOn(sandals); else if(temperature > 60) putOn(sweatshirt); putOn(blue_jeans); putOn(black_socks); putOn(shoes); else putOn(sweatshirt); putOn(blue_jeans); putOn(wool_socks); putOn(shoes); putOn(jacket); }

Problem 2: Design an algorithm GetDressed() that describes how you decide what you want to wear. You can define new items of clothing if you want. You may need to declare new subroutines depending on the criteria that affect the outfit you put on. Here are some examples.

Do you put on the first thing you find?

If you just put on the first shirt you find in the closet, you might want to declare a subroutine clothing firstThingFound() that will return the first item of clothing found in your closet. However, you also need some way to distinguish if the item of clothing is in fact a shirt. You can declare another subroutine isShirt(clothing) that takes an item of clothing as input, returns true if it is a shirt, and false otherwise. Then put these two subroutines put together in a while-loop: found_item = firstThingFound(); while(isShirt(found_item) = FALSE) { found_item = firstThingFound(); }

Do you try on several items before deciding what to wear?

You need a subroutine takeOff(clothing) to take off the clothes you just put on. You also need a subroutine to test whether you will try on something else. Some examples could be tooSmall(clothing), tooBig(clothing), isUnflattering(clothing), etc, all of which should return either true or false.

Reading (optional):