1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Define a function to add two numbers
def add(a, b):
return a + b
# Define a function to subtract two numbers
def subtract(a, b):
return a - b
# Define a function to multiply two numbers
def multiply(a, b):
return a * b
# Define a function to divide two numbers
def divide(a, b):
return a / b
# Define a dispatch table that maps operation names to functions
operation_table = {
"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide,
}
# Define a function that takes an operation name and two numbers, and performs the operation
def perform_operation(operation, a, b):
if operation in operation_table:
return operation_table[operation](a, b)
else:
raise ValueError(f"Unknown operation '{operation}'")
# Test the dispatch table by performing some operations
print(perform_operation("add", 2, 3)) # Output: 5
print(perform_operation("subtract", 5, 1)) # Output: 4
print(perform_operation("multiply", 4, 6)) # Output: 24
print(perform_operation("divide", 10, 2)) # Output: 5
|