Problem Statement Create a function named compose...
Criado em: 13 de junho de 2026
Respondido usando GPT-5.5 Thinking por Chat01
Criado em: 13 de junho de 2026
Respondido usando GPT-5.5 Thinking por Chat01
Problem Statement
Create a function named compose that:
Accepts any number of functions as arguments.
Returns a new function representing their composition.
Passes arguments to the first function.
Passes each function's output to the next function.
Returns the final output.
The provided functions are:
add(*args) # Returns the sum of all arguments
square(a) # Returns the square of a number
splitter(a) # Returns [floor(a/2), a % 2]
my_max(a) # Returns the maximum value in a number or list
my_min(a) # Returns the minimum value in a number or list
Note: Only the first function handles multiple arguments; all subsequent functions take a single argument.
Example
functionsList = [add, splitter]
argumentsList = [2, 3]
Processing:
add(2, 3) -> 5
splitter(5) -> [2, 1]
Output:
[2, 1]
Function Description
Complete the function:
compose(*functionsList)
Parameters
functionsList: function arguments
Returns
function: the composition of functionsList
Constraints
1 ≤ n ≤ 15
functionsList[0] = add
functionsList[i] ∈ {square, splitter, my_max, my_min} for i ≠ 0
1 ≤ q ≤ 10
1 ≤ argumentsList[i] ≤ 10
It is guaranteed that the list of functions generates a valid composition.
Input Format For Custom Testing
The first line contains an integer n, denoting the number of elements in functionsList.
Each of the next n lines contains a string describing functionsList[i].
The next line contains an integer q, denoting the number of elements in argumentsList.
Each of the next q lines contains an integer describing argumentsList[i].
Sample Case 0
Sample Input
3
add
splitter
my_max
3
2
1
3
Sample Output
3
Explanation
compose(2, 1, 3)
add(2, 1, 3) -> 6
splitter(6) -> [3, 0]
my_max([3, 0]) -> 3
Output:
3
Sample Case 1
Sample Input
3
add
square
splitter
2
2
4
Sample Output
[18, 0]
Explanation
compose(2, 4)
add(2, 4) -> 6
square(6) -> 36
splitter(36) -> [18, 0]
Output:
[18, 0]
Function to Implement
def compose(*functionsList):
pass
Expected behavior:
def compose(*functionsList):
def composed(*args):
result = functionsList0
textfor fn in functionsList[1:]: result = fn(result) return result return composed
pythondef compose(*functionsList): def composed(*args): result = functionsList[0](*args) for fn in functionsList[1:]: result = fn(result) return result return composed
Example:
pythoncomposed_fn = compose(add, splitter, my_max) print(composed_fn(2, 1, 3))
Output:
python3
Why it works:
pythonadd(2, 1, 3) # 6 splitter(6) # [3, 0] my_max([3, 0]) # 3
Only the first function receives *args; every later function receives the previous result as a single argument.
Solve this with all test cases passed and hidden case passed and edge case pass too
Paste this into pipeline.py for the TODO methods:
pythonfrom pyspark.sql import SparkSession, DataFrame import pyspark.sql.functions as F from main.base import PySparkJobInterface class PySparkJob(PySparkJobInterface): def init_spark_session(self) -> SparkSession: return ( SparkSession.builder .master("local") .appName("Banking Data Mining") .getOrCreate() ) def extract_valid_transactions( self, accounts: DataFrame, transactions: DataFrame ) -> DataFrame: accounts_from = ( accounts .select( F.col("accountNumber").alias("validFromAccountNumber"), F.col("balance").cast("double").alias("balance") ) ) accounts_to = ( accounts .select(F.col("accountNumber").alias("validToAccountNumber")) .distinct() ) valid_transactions = ( transactions.alias("t") .join( accounts_from.alias("af"), F.col("t.fromAccountNumber") == F.col("af.validFromAccountNumber"), "inner" ) .join( accounts_to.alias("at"), F.col("t.toAccountNumber") == F.col("at.validToAccountNumber"), "inner" ) .filter( F.col("t.transferAmount").cast("double") <= F.col("af.balance") ) .select("t.*") ) return valid_transactions def distinct_transactions(self, transactions: DataFrame) -> int: return ( transactions .select("fromAccountNumber") .distinct() .count() ) def transactions_per_account(self, transactions: DataFrame) -> dict: rows = ( transactions .groupBy("fromAccountNumber") .count() .orderBy(F.desc("count"), F.asc("fromAccountNumber")) .limit(10) .collect() ) return { row["fromAccountNumber"]: row["count"] for row in rows }
This handles the hidden cases by:
pythonvalid transaction = fromAccountNumber exists in accounts AND toAccountNumber exists in accounts AND transferAmount <= balance of fromAccountNumber
transactions_per_account returns the top 10 accounts by transaction count, sorted highest count first.