The task is to implement an IDE-style function signature lookup engine. It has two core APIs:
- Register a function (register): store the function name, its list of parameter types, and whether it takes variadic arguments.
- Query for matches (find_matches): given a list of argument types for a call, return the names of all functions that can be legally called with them.
Core interface definition:
class FunctionLibrary:
def register(self, name: str, params: list[str], is_variadic: bool = False) -> None:
"""Register a function signature"""
pass
def find_matches(self, query: list[str]) -> list[str]:
"""Given the list of actual argument types, return the names of all matching functions (any order)"""
pass
The problem evolves in steps, and each step tests something different.
Part 1: Exact match. When is_variadic=False, the number of parameters, the parameter types, and the parameter order must be exactly the same as in the query.
- Register: register("foo", ["String", "Integer"], False)
- Query ["String", "Integer"] -> matches foo
- Query ["String"] or ["Integer", "String"] -> no match
Part 2: Variadic arguments. When is_variadic=True, the last parameter is the variadic one (like Python's *args or Java's Type...). The behavior:
- The fixed parameters before it must match strictly and exactly.
- The trailing variadic parameter can take 0 or any number of arguments of that type.
- Register: register("bar", ["String", "Integer"], is_variadic=True) (so the fixed prefix is String, followed by 0 to many Integer)
- Query ["String"] -> matches (0 Integers at the tail)
- Query ["String", "Integer"] -> matches (1 Integer at the tail)
- Query ["String", "Integer", "Integer"] -> matches (2 Integers at the tail)
- Query ["String", "Boolean"] -> no match (wrong type at the tail)
Classic test cases:
- Register:
- register("funcA", ["String", "Integer"], False)
- register("funcB", ["String", "Integer", "Integer"], True)
- Query find_matches(["String", "Integer"]):
-> Output: ["funcA", "funcB"]
(Explanation: funcA matches exactly with 2 parameters; funcB satisfies the fixed prefix ["String", "Integer"] plus 0 variadic "Integer".) - Query find_matches(["String", "Integer", "Integer", "Integer"]):
-> Output: ["funcB"]
(Explanation: funcA has the wrong number of parameters; funcB satisfies the fixed prefix plus 2 variadic "Integer".) - Query find_matches(["String"]):
-> Output: []
(Explanation: not enough arguments, so nothing matches.)
Discussion
Loading comments…