def n_sum(n, nums, target, **kv):
def sum_closure_default(a, b):<EOL><INDENT>return a + b<EOL><DEDENT>def compare_closure_default(num, target):<EOL><INDENT>"""<STR_LIT>"""<EOL>if num < target:<EOL><INDENT>return -<NUM_LIT:1><EOL><DEDENT>elif num > target:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT><DEDENT>def same_closure_default(a, b):<EOL><INDENT>return a == b<EOL><DEDENT>def n_sum(n, nums, target):<EOL><INDENT>if n == <NUM_LIT:2>:      <EOL><INDENT>results = two_sum(nums, target)<EOL><DEDENT>else:<EOL><INDENT>results = []<EOL>prev_num = None<EOL>for index, num in enumerate(nums):<EOL><INDENT>if prev_num is not None andsame_closure(prev_num, num):<EOL><INDENT>continue<EOL><DEDENT>prev_num = num<EOL>n_minus1_results = (<EOL>n_sum(                      <EOL>n - <NUM_LIT:1>,                  <EOL>nums[index + <NUM_LIT:1>:],       <EOL>target - num            <EOL>)   <EOL>)   <EOL>n_minus1_results = (<EOL>append_elem_to_each_list(num, n_minus1_results)<EOL>)<EOL>results += n_minus1_results<EOL><DEDENT><DEDENT>return union(results)<EOL><DEDENT>def two_sum(nums, target):<EOL><INDENT>nums.sort()<EOL>lt = <NUM_LIT:0><EOL>rt = len(nums) - <NUM_LIT:1><EOL>results = []<EOL>while lt < rt:<EOL><INDENT>sum_ = sum_closure(nums[lt], nums[rt])<EOL>flag = compare_closure(sum_, target)<EOL>if flag == -<NUM_LIT:1>:<EOL><INDENT>lt += <NUM_LIT:1><EOL><DEDENT>elif flag == <NUM_LIT:1>:<EOL><INDENT>rt -= <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>results.append(sorted([nums[lt], nums[rt]]))<EOL>lt += <NUM_LIT:1><EOL>rt -= <NUM_LIT:1><EOL>while (lt < len(nums) and<EOL>same_closure(nums[lt - <NUM_LIT:1>], nums[lt])):<EOL><INDENT>lt += <NUM_LIT:1><EOL><DEDENT>while (<NUM_LIT:0> <= rt and<EOL>same_closure(nums[rt], nums[rt + <NUM_LIT:1>])):<EOL><INDENT>rt -= <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>return results<EOL><DEDENT>def append_elem_to_each_list(elem, container):<EOL><INDENT>results = []<EOL>for elems in container:<EOL><INDENT>elems.append(elem)<EOL>results.append(sorted(elems))<EOL><DEDENT>return results<EOL><DEDENT>def union(duplicate_results):<EOL><INDENT>results = []<EOL>if len(duplicate_results) != <NUM_LIT:0>:<EOL><INDENT>duplicate_results.sort()<EOL>results.append(duplicate_results[<NUM_LIT:0>])<EOL>for result in duplicate_results[<NUM_LIT:1>:]:<EOL><INDENT>if results[-<NUM_LIT:1>] != result:<EOL><INDENT>results.append(result)<EOL><DEDENT><DEDENT><DEDENT>return results<EOL><DEDENT>sum_closure = kv.get('<STR_LIT>', sum_closure_default)<EOL>same_closure = kv.get('<STR_LIT>', same_closure_default)<EOL>compare_closure = kv.get('<STR_LIT>', compare_closure_default)<EOL>nums.sort()<EOL>return n_sum(n, nums, target)<EOL>
n: int
nums: list[object]
target: object
sum_closure: function, optional
    Given two elements of nums, return sum of both.
compare_closure: function, optional
    Given one object of nums and target, return -1, 1, or 0.
same_closure: function, optional
    Given two object of nums, return bool.
return: list[list[object]]

Note:
1. type of sum_closure's return should be same 
   as type of compare_closure's first param