Problem Wk.3.2: Nano Quiz
This problem has been submitted previously; no changes will be saved.
Due date: 2/17, 9:50am
Do all parts of this problem and then click Submit. There is only one Submit button, you should
only do that when you are finished with all the parts of the problem.
Do not try to start another log in, you will lose what you typed.
There is a limited checking budget (20 checks) on this quiz.
You have 15 minutes. You must click submit before:
2/17, 9:50am
Part 1: Constructing a State Machine
Define a state machine for a vending machine.
The vending machine has an unlimited number of sodas that it sells for 75 cents
apiece.
The user can deposit quarters in the machine.
If the user presses the cancel button, all the coins she's put in so far are returned.
If the user presses the dispense button,
If she had not deposited at least 75 cents, she gets no soda and no change.
If she had deposited 75 cents or more, she gets a soda and any amount over 75
cents that she has deposited.
More concretely,
The input for the machine is one of:
'quarter' (25 cents) indicates that a coin has been deposited
'cancel' indicates that the user wants her money back
'dispense' indicates that the user wants a soda
The output of the machine is:
(change, soda)
where
change is an integer that indicates (in cents) the amount of change (possibly 0)
that is returned
soda is a boolean that indicates whether or not the machine will dispense a soda
For example:
Vending().transduce(['dispense', 'quarter', 'quarter', 'quarter', 'quarter',
'dispense', 'quarter', 'cancel', 'dispense'])
would return:
[(0, False), (0, False), (0, False), (0, False), (0, False),
(25, True), (0, False), (25, False), (0, False)] class Vending(sm.SM):
startState = 0
def getNextValues(self, state, inp):
if inp=='quarter':
return (state+1, (0, False))
if inp=='cancel':
return (0, (25*state, False))
if inp=='dispense':
if state<3:
return (state, (0, False))
return (0, (25*(state-3), True))
This is the answer we wrote:
class Vending(sm.SM):
startState = 0
def getNextValues(self, state, inp):
if inp == 'cancel':
return (0, (state, False))
elif inp == 'dispense':
if state >= 75:
return (0, (state - 75, True))
else:
return (state, (0, False))
else: # 'quarter'
return (state + 25, (0, False))
19 checks left
Check
Part 2: Enable Submit
Current time is: 3/1/2011, 9:10pm
Click Submit before: 2/17, 9:50am
The Check button will update the current time.
Enter Done below
[]
and click Submit.
If this problem is submitted past the due time, this subproblem will be marked
incorrect.
19 checks left
Check This is a multi-part problem, each part has its own Save and Check buttons but there is ONLY
ONE Submit button for the WHOLE problem. Finish working on all the parts before you click on
Submit.
Get Answers
6.01SC Introduction to EECS I, Quiz 3 Solutions
of 3
Report
Tell us what’s wrong with it:
Thanks, got it!
We will moderate it soon!
Free up your schedule!
Our EduBirdie Experts Are Here for You 24/7! Just fill out a form and let us know how we can assist you.
Take 5 seconds to unlock
Enter your email below and get instant access to your document