Given a list of function signatures and a list of input arguments, return all function names that can be called with those arguments. The system supports variadic arguments and type inheritance.
Each function signature is represented as a list where the first element is the function name (a string) and the remaining elements are parameter type descriptors (strings). A parameter type descriptor ending with '...' indicates a variadic parameter that accepts zero or more arguments of that base type. A variadic parameter can only appear as the last parameter.
Each input argument is represented as a string indicating its type. The type hierarchy is: int is a subtype of float, float is a subtype of number, and number is a subtype of object. Every type is also a subtype of itself. A function can accept an argument if the argument's type is a subtype of (or equal to) the parameter's declared type.
Return the list of matching function names sorted in lexicographical order.
Function Signature
def match_functions(signatures: list[list[str]], arguments: list[str]) -> list[str]:
Rules
-
A signature is
[name, param_1, ..., param_k]
with
k >= 0
parameters.
-
Subtyping is transitive, so
int
is also a subtype of
number
and
object
. A wider type never fits a narrower parameter: a
float
argument does not match an
int
parameter.
-
A signature whose last parameter does not end in
'...'
needs exactly
k
arguments, and argument
i
must be a subtype of parameter
i
.
-
A signature whose last parameter is
T...
needs at least
k - 1
arguments. The first
k - 1
arguments match the fixed parameters by position, and every remaining argument (possibly none) must be a subtype of
T
.
-
A signature with no parameters matches only an empty argument list.
-
Argument types never end in
'...'
.
-
Function names are distinct. Sort the result in ascending order by plain string comparison, and return an empty list when nothing matches.
Constraints
-
1 <= len(signatures) <= 100
-
1 <= len(signatures[i]) <= 10
-
Each signature's first element is a non-empty string (function name).
-
Parameter types are one of
'int'
,
'float'
,
'number'
,
'object'
, or one of these followed by
'...'
for variadic. Argument types are one of the four plain types.
-
0 <= len(arguments) <= 20
Examples
Input: signatures = [["add","int","int"],["sum","number..."],["print","object"],["noop"]], arguments = ["int","int"]
Output: ["add","sum"]
add takes two int parameters, an exact match. sum has the variadic number..., and int is a subtype of number, so both arguments fit. print needs exactly one argument and noop needs none, so neither accepts two.
Input: signatures = [["foo","float"],["bar","int"],["baz","object"]], arguments = ["int"]
Output: ["bar","baz","foo"]
int is a subtype of float and of object, and it equals int, so all three functions accept the call. The names are returned in sorted order.
Input: signatures = [["f","float","int..."],["g","int","object..."],["h","number"],["k","int..."]], arguments = ["float"]
Output: ["f","h"]
f matches float to its fixed float parameter and gives its variadic int... zero arguments. h accepts the float as a number. g and k would both need a float to fit an int parameter, which the hierarchy does not allow.