I was writing to a friend and somehow got into the topic of chewing gum, something that I’m very fond of. Matter of fact, I have a very methodical way of consuming my favorite three flavors, Orbit Cinnamon, Trident Strawberry, and Extra Watermelon, while I’m working busily away. So instead of writing it out in words, I decided to write a script in faux code. It was fun! And mostly for it’s preservation so that I can find it in X years down the road, here it is!

% We start out with zero pieces of gum, and nothing being chewed.

% The order keeps track of my special order of chewing, to be cycled through, 1 through 5
pieceCount = 0;
piecesLeft = 7; % we start with 7 pieces
chewing={}; % we start chewing nothing
order=1; % start with order 1
while (piecesLeft > 0)
% Always spit out gum when we have 2 pieces that have lost flavor:
if ((pieceCount == 2) && (lostflavor(chewing)==’true’)
spit(chewing);
% Or spit if we have one extra watermelon piece that has lost flavor
elseif((pieceCount == 1) && (lostflavor(chewing)==’true) && (chewing==’extra(1)’||’extra(2)’))
spit(chewing);
else
order = consume(order);
end
% Here is the function to chew the gum based on our current order
function consume(order_number)
switch order:
% Start with one strawberry piece, and chew until it’s no good
case 1: eat(strawberry(1))
while(lostflavor(strawberry(1))~=’true’)
chew()
end
% When it loses flavor, add an extra watermelon piece
add(extra(1),strawberry(1))
while(lostflavor(strawberry(1),extra(1))~=’true’)
chew()
end
return 2;
% Then eat the last extra piece, and chew until it’s no good
case 2: eat(extra(2))
while(lostflavor(extra(2))~=’true’)
chew()
end
return 3;
% Eat both strawberry pieces together, because they are small!
case 3: eat({strawberry(2),strawberry(3)})
while(lostflavor({strawberry(2),strawberry(3)})~=’true’)
chew()
end
return 4;
% Grand finale is two cinnamon pieces!
case 4: eat({orbit(1),orbit(2))
while(lostflavor({orbit(1),orbit(2)})~=’true’)
chew()
end
return 5;
case 5;
fprintf(‘%s\n’,’We’ve finished all of our gum! Replenish supply and start over’)
exit;
end
end
% Function to eat gum
function eat({gum})
nomnom(gum);
piecesLeft=piecesLeft-size(gum);
chewing=gum;
end
% Function to spit out gum being chewed
function spit(tospit)
trash(tospit)
pieceCount = 0;
piecesLeft = piecesLeft -size(tospit);
chewing = {};
end
end
It kind of makes me want to try writing scripts for little things in life that are methodical like brushing your teeth, cooking, or running. It also makes me realize that people who design the machines and gadgets that we use in our everyday life actually DO think about these sorts of algorithms. That’s so cool! :O)




Suggested Citation:
Sochat, Vanessa. "My Gum Chewing Algorithm :P." @vsoch (blog), 27 Nov 2010, https://vsoch.github.io/2010/my-gum-chewing-algorithm-p/ (accessed 16 Apr 24).