Decision tables: Difference between revisions

→‎{{header|Tcl}}: Add Python solution
m (→‎{{header|Tcl}}: formatting)
(→‎{{header|Tcl}}: Add Python solution)
Line 73:
Ensure printer software is installed
Check/replace ink</pre>
 
=={{header|Python}}==
<lang python>
'''
Create a Decision table then use it
'''
 
def dt_creator():
print("\n\nCREATING THE DECISION TABLE\n")
conditions = input("Input conditions, in order, separated by commas: ")
conditions = [c.strip() for c in conditions.split(',')]
print( ("\nThat was %s conditions:\n " % len(conditions))
+ '\n '.join("%i: %s" % x for x in enumerate(conditions, 1)) )
print("\nInput an action, a semicolon, then a list of tuples of rules that trigger it. End with a blank line")
action2rules, action = [], ' '
while action:
action = input("%i: " % (len(action2rules) + 1)).strip()
if action:
name, _, rules = [x.strip() for x in action.partition(';')]
rules = eval(rules)
assert all(len(rule) == len(conditions) for rule in rules), \
"The number of conditions in a rule to trigger this action is wrong"
action2rules.append((name, rules))
actions = [x[0] for x in action2rules]
# Map condition to actions
rule2actions = dict((y,[]) for y in set(sum((x[1] for x in action2rules), [])))
for action, rules in action2rules:
for r in rules:
rule2actions[r].append( action )
return conditions, rule2actions
 
def dt_user(dt, default=['Pray!']):
conditions, rule2actions = dt
print("\n\nUSING THE DECISION TABLE\n")
rule = tuple((int('y' == input("%s? (Answer y if statement is true or n): " % c)) for c in conditions))
print("Try this:\n " + '\n '.join(rule2actions.get(rule, default)))
 
if __name__ == '__main__':
dt = dt_creator()
dt_user(dt)
dt_user(dt)
dt_user(dt)</lang>
 
'''Sample Run'''
<pre>
 
CREATING THE DECISION TABLE
 
Input conditions, in order, separated by commas: Printer does not print, A red light is flashing, Printer is unrecognised
 
That was 3 conditions:
1: Printer does not print
2: A red light is flashing
3: Printer is unrecognised
 
Input an action, a semicolon, then a list of tuples of rules that trigger it. End with a blank line
1: Check the power cable; [(1,0,1)]
2: Check the printer-computer cable; [(1,1,1), (1,0,1)]
3: Ensure printer software is installed; [(1,1,1), (1,0,1), (0,1,1), (0,0,1)]
4: Check/replace ink; [(1,1,1), (1,1,0), (0,1,1), (0,1,0)]
5: Check for paper jam; [(1,1,0), (1,0,0)]
6:
 
 
USING THE DECISION TABLE
 
Printer does not print? (Answer y if statement is true or n): n
A red light is flashing? (Answer y if statement is true or n): y
Printer is unrecognised? (Answer y if statement is true or n): y
Try this:
Ensure printer software is installed
Check/replace ink
 
 
USING THE DECISION TABLE
 
Printer does not print? (Answer y if statement is true or n): y
A red light is flashing? (Answer y if statement is true or n): n
Printer is unrecognised? (Answer y if statement is true or n): y
Try this:
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
 
 
USING THE DECISION TABLE
 
Printer does not print? (Answer y if statement is true or n): n
A red light is flashing? (Answer y if statement is true or n): n
Printer is unrecognised? (Answer y if statement is true or n): n
Try this:
Pray!</pre>
 
=={{header|Tcl}}==
Anonymous user