β‘
Function Composition
Function Compositionμ΄λ €μ ν¨μ +40pts
Problem
Write a function that composes multiple functions from right to left.
Examples
Input:
compose(x => x * 2, x => x + 1)(5)Output:
12π‘ (5 + 1) * 2 = 12
Explanation
μ΄ λ¬Έμ λ **ν¨μν νλ‘κ·Έλλ°**μ ν΅μ¬ κ°λ μΈ **ν¨μ ν©μ±(composition)**μ νμ΅ν©λλ€. **ν¨μ ν©μ±μ΄λ?** μ¬λ¬ ν¨μλ₯Ό μ°κ²°νμ¬ νλμ μλ‘μ΄ ν¨μλ₯Ό λ§λλ κ²μ λλ€. μνμ f(g(x))μ κ°μ΅λλ€. **composeμ λμ λ°©μ** μ€λ₯Έμͺ½μμ μΌμͺ½μΌλ‘ ν¨μλ₯Ό μ μ©ν©λλ€: compose(f, g, h)(x) = f(g(h(x))) **compose(x => x * 2, x => x + 1)(5) μ€ν** 1. μ€λ₯Έμͺ½ ν¨μ λ¨Όμ : 5 + 1 = 6 2. μΌμͺ½ ν¨μ μ μ©: 6 * 2 = 12 **reduc...
View detailed explanation βKey Concepts
ν¨μ ν©μ± reduceRight λ©μλ κ³ μ°¨ ν¨μ ν΄λ‘μ
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.