# 5 Ejemplos de memoización en Python:[br][br]#1. Secuencia de Fibonacci usando memoización:[br]def fibonacci(n, memo={}):[br] if n in memo:[br] return memo[n][br] if n <= 2:[br] return 1[br] memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)[br] return memo[n][br][br]#2. Factorial usando memoización:[br]def factorial(n, memo={}):[br] if n in memo:[br] return memo[n][br] if n == 0:[br] return 1[br] memo[n] = n * factorial(n-1, memo)[br] return memo[n][br][br]#3. Calcula la n-ésima potencia de un número usando memoización:[br]def power(x, n, memo={}):[br] if n == 0:[br] return 1[br] if n % 2 == 0:[br] if n not in memo:[br] memo[n] = power(x, n/2, memo)[br] return memo[n] * memo[n][br] else:[br] if (n-1) not in memo:[br] memo[n-1] = power(x, (n-1)/2, memo)[br] return x * memo[n-1] * memo[n-1][br][br]#4. Subsecuencia común más larga usando memoización:[br]def lcs(s1, s2, memo={}):[br] if (s1, s2) in memo:[br] return memo[(s1, s2)][br] if not s1 or not s2:[br] return ""[br] if s1[0] == s2[0]:[br] memo[(s1, s2)] = s1[0] + lcs(s1[1:], s2[1:], memo)[br] else:[br] memo[(s1, s2)] = max(lcs(s1[1:], s2, memo), lcs(s1, s2[1:], memo), key=len)[br] return memo[(s1, s2)][br][br]#5. Problema de mochila usando memoización:[br]def knapsack(capacity, weights, values, n, memo={}):[br] if (capacity, n) in memo:[br] return memo[(capacity, n)][br] if n == 0 or capacity == 0:[br] return 0[br] if weights[n-1] > capacity:[br] memo[(capacity, n)] = knapsack(capacity, weights, values, n-1, memo)[br] return memo[(capacity, n)][br] memo[(capacity, n)] = max(values[n-1] + knapsack(capacity-weights[n-1], weights, values, n-1, memo), knapsack(capacity, weights, values, n-1, memo))[br] return memo[(capacity, n)]