We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Partial Function with Placeholders

Number: 2900

Difficulty: Easy

Paid? Yes

Companies: N/A


Problem Description

Given a function fn and an array args (which may contain placeholders represented as the string "_"), create a function partialFn that, when invoked with additional arguments (restArgs), returns the result of calling fn with a modified argument list. In the modified list, each placeholder in args is replaced with the next value from restArgs, and if there are extra values in restArgs, they are appended at the end.


Key Insights

  • The problem is about function currying and partial application.
  • Iterate over the provided args and replace each placeholder "_" with the next element from restArgs.
  • After processing args, append any leftover elements in restArgs to the argument list.
  • Finally, call fn with the new list of arguments spread out.

Space and Time Complexity

Time Complexity: O(n + m), where n is the length of args and m is the length of restArgs. Space Complexity: O(n + m) for the new list of arguments.


Solution

The solution involves creating a closure function (partialFn) that takes additional arguments (restArgs). Inside this function, we loop over the original args. Whenever a placeholder ("_") is encountered, we replace it with the next available element from restArgs. If there are leftover restArgs after processing all placeholders, they are appended at the end of the list. Finally, we invoke fn with the assembled argument list using a spread mechanism (or its equivalent in the language at hand).

Data Structures & Techniques:

  • Use of an index pointer to iterate over restArgs.
  • Looping through the args array to construct the final argument list.
  • Handling remaining elements in restArgs if any.

This approach is straightforward and efficient given the constraints.


Code Solutions

def partial(fn, args):
    # Define the closure function which will accept extra arguments (restArgs)
    def partialFn(*restArgs):
        result = []  # This list will store the final arguments for fn
        rest_index = 0  # Pointer to track the current index in restArgs
        
        # Iterate over each element in the original args array
        for arg in args:
            # If current element is a placeholder and there are still restArgs available
            if arg == "_" and rest_index < len(restArgs):
                result.append(restArgs[rest_index])
                rest_index += 1
            else:
                result.append(arg)
        # If any restArgs remain, append them at the end
        if rest_index < len(restArgs):
            result.extend(restArgs[rest_index:])
        
        # Call the provided function fn with unpacked result list as arguments
        return fn(*result)
     
    return partialFn

# Example usage
if __name__ == '__main__':
    fn = lambda *args: args
    args = [1, 2, "_", 4, "_", 6]
    partialFn = partial(fn, args)
    print(partialFn(3, 5))  # Expected output: (1,2,3,4,5,6)
← Back to All Questions