Actually yes. First, Python does not have a signed operator switchthat provides a binary search when the operator becomes long enough, which is much faster than a linear search.
Python , :
def other_case(x):
'''We can store non-lambdas too'''
return 8
functions = {
'd': lambda x: 4*x,
'!': lambda x: 5,
'k': lambda x: (1-2*x+x**2),
'Z': lambda x: (1/x),
'*': other_case,
}
, :
def call(a, x):
return functions[a](x)
O (1) , , , .
Edit
, , . , - 2000 5000 ( ), 100 . 500 , 30 .
from __future__ import division
def mass2000(x):
'''Do something for mass of 2000'''
return 1/x
def mass2100(x):
'''Do something for mass of 2100'''
return x
def mass2200(x):
'''Do something for mass of 2200'''
return x**2
lookup = [
mass2000,
mass2100,
mass2200,
]
def call(mass, x):
if (mass < 2000 or mass > 5000):
raise ValueError("Mass out of range")
return lookup[(mass - 2000) // 100](x)