Pseudocoding: Introducing the Pizzalgorithm
This was a fun exercise I had in an assignment as intro to programming, and I thought I’d share.
Phase 2: Problem Solving
Tasks:
1) Write an algorithm to tell a computer how to deal with traffic lights. Use only if, else
if, and/or else.
IF (the light is green) { PROCEED }
ELSE IF (the light is yellow) { SLOW DOWN }
ELSE IF (the light is red) { STOP }
2) Write an algorithm describing your morning routine from waking up to work/school.
Use if, else if, else, and loops.
WHILE (alarm goes off AND clock reads earlier than 7:00) {
Hit ‘Snooze’ ;
}
ELSE put on glasses AND fall out of bed
IF (on floor AND have no shirt AND have no pants) {
Feel around;
IF (feel shirt) { pick it up } ;
IF (feel pants) { pick it up } ;
Crawl to dresser {
Grab clean underwear AND socks ;
IF (have no shirt) { grab one clean shirt } ;
IF (have no pants) { grab clean pair of pants } ;
}
Stand ;
}
Smell self
IF (stinky) {
go shower;
dry off;
put on underwear;
put on socks;
put on pants;
put on shirt;
}
ELSE IF (slight smell OR fresh as a daisy) {
put on underwear;
put on socks;
put on pants;
put on shirt;
}
IF (not in bathroom) { go to bathroom }
IF (in bathroom) {
have bowel movement;
brush teeth;
WHILE (hair is unruly AND clock reads earlier than 7:45) {
run fingers through hair;
};
WHILE (clock reads later than 7:45 AND earlier than 8:15) { check email }
IF (clock reads 8:15) {
put on jacket;
go catch bus;
}
3) Write 2 algorithms of your choice.
Bonus: Write an algorithm for writing algorithms.
Algorithm for writing algorithms
IF (homework = write an algorithm) {
Think of scenario;
IF (scenario begins with IF statement) {
write it down;
WHILE (new question connects to IF statement) {
add ELSE IF statement;
}
IF (FALSE result requires a command) {
add ELSE statement;
}
};
ELSE IF (scenario begins with WHILE loop) { write it down };
WHILE (variable is unassigned)
{ assign values to variable };
WHILE (algorithm does not achieve desired result) {
Trace;
Correct;
}
Algorithm for eating pizza (or How Many Slices of Pizza Can You Eat?)
pizza = 8 slices;
sliceCount = 0;
WHILE (Hungry) {
Eat 1 slice;
pizza = pizza – 1 slice;
sliceCount = sliceCount + 1;
IF (pizza = 0 slices) { get another pizza };
}
Algorithm for KFC (or How Many Pieces of Chicken Do You Need to Finish the KFC Gravy?)
bucket_o_chicken = 6 pieces;
piece = 8 bites;
gravy = 100 dips;
chickenCount = 0;
WHILE (gravy > 0 dips) {
take piece of chicken out of bucket;
bucket_o_chicken = bucket_o_chicken – 1 piece;
WHILE (piece > 0) {
dip in gravy;
gravy = gravy – 1 dip;
bite piece;
piece = piece – 1 bite;
IF (piece = 0 bites) { chickenCount = chickenCount + 1 }
}
IF bucket_o_chicken = 0 pieces { get another bucket }
}
I wrote these before lunch. Does it show?
I also tried to write my algorithm for eating pizza in Perl. Not really sure if it worked or not:
#!/usr/local/bin/perl
$pizza = 8; # Assign value to the variable
$sliceCount = 0; # Assign value to the variable
do
{
$pizza = $pizza – 1; # Eat 1 slice of pizza
$sliceCount = $sliceCount + 1; # Add to count of slices eaten
until ($pizza = 0)
{
open (PIZZABOX, “<$pizza”); # Get another pizza
}
}
while (hungry) # Redo so long as you are hungry
No trackbacks yet.