I am interesting in the topic of finite automata, so I started over by implementing a parking machine from this video: Computers Without Memory - Computerphile to understand conceptions. Didn't do much of an error checking - have made a minimal working version.
Short description of the task, but for better understanding watch the video:
- You have the parking machine which is implemented by the finite automaton.
- The parking lot costs 25 pence. The machine takes only 5, 10, 20 pence coins. Thus, any combination of these coins can be used to pay for parking.
- After a sum of coins has reached 25 pence, the ticket is issued.
- The machine gobble up all extra money above the 25 pence, like 20 + 20 pence = the ticket only, no change are given.
Questions:
- What do you think about my approach?
- Is it optimal, extendable, suitable for real world applications or this technique has limiting factors and an another should be used?
- How would you implement this machine? It will be good to see another solutions.
It is possible to do this by such way: Explicit state transition table, may be I will do this also. My target is to implement something more serious like a regex engine in the future, but I should read some books/articles before.
#!/usr/bin/python3
def get_coin(current_sum, *functions):
print(f"Current sum = {current_sum} pence")
coin = int(input("Insert coin\n"))
if coin == 5:
functions[0]()
elif coin == 10:
functions[1]()
elif coin == 20:
functions[2]()
else:
error(current_sum, functions)
def error(current_sum, functions):
print("Error: only 5, 10, 20 pence coins are allowed\n")
get_coin(current_sum, *functions)
def zero_pence():
get_coin("0", five_pence, ten_pence, twenty_pence)
def five_pence():
get_coin("5", ten_pence, fifteen_pence, twenty_five_pence)
def ten_pence():
get_coin("10", fifteen_pence, twenty_pence, give_ticket)
def fifteen_pence():
get_coin("15", twenty_pence, twenty_five_pence, give_ticket)
def twenty_pence():
get_coin("20", twenty_five_pence, give_ticket, give_ticket)
def twenty_five_pence():
input("Current sum = 25 pence, press the 'return' button to pay")
give_ticket()
def give_ticket():
print("""\nTake your ticket:
Date: 1 November 2019
Start time: \t20:21
End time: \t22:21\n
""")
def parking_machine():
prompt = """\n\tThe parking machine.
Information:
1. The machine takes 5, 10, 20 coins.
2. The machine doesn't give a change.
3. The parking costs 25 pence.
Press 's' button to start inserting of coins.
"""
prompt = '#' * 80 + prompt + '#' * 80 + '\n'
while True:
button = input(prompt)
if button == 's':
zero_pence()
parking_machine()