Skip to content

Instantly share code, notes, and snippets.

View theSamyak's full-sized avatar
💭
making

Samyak Jain theSamyak

💭
making
View GitHub Profile

Let's compare the fibonacci function with and without memoization to understand the performance difference.

Fibonacci without Memoization

Here's the fibonacci function without memoization:

def fibonacci(n):
    if n < 2:
        return n
 return fibonacci(n-1) + fibonacci(n-2)
@theSamyak
theSamyak / lambda_2.py
Created June 17, 2024 12:40
Example: Creating Multiplier Functions with Lambdas
def create_multiplier(factor):
return lambda x: x * factor
# Create specific multiplier functions
double = create_multiplier(2)
triple = create_multiplier(3)
# Use the created functions
print(double(5)) # Output: 10
print(triple(5)) # Output: 15
@theSamyak
theSamyak / 1.1_title.md
Last active June 14, 2024 20:53
Understanding First-Class Functions, High-Order Functions and Closures in Python: A Detailed Guide

First-Class Functions

Assigning Functions to Variables

@theSamyak
theSamyak / app.py
Last active May 25, 2024 19:04
[FCC First class functions and Closures Blog] A higher-order function that returns another function
def create_multiplier(n):
def multiplier(x):
return x * n
return multiplier
# Creating specific multiplier functions
multiply_by_2 = create_multiplier(2)
multiply_by_3 = create_multiplier(3)
# Using the returned functions
@theSamyak
theSamyak / app.py
Created May 25, 2024 13:40
[FCC First class functions and Closures Blog] A higher-order function that takes a function as an argument
def apply_operation(operation, x, y):
return operation(x, y)
# Functions to pass as arguments
def add(x, y):
return x + y
def multiply(x, y):
return x * y
@theSamyak
theSamyak / app.py
Last active May 25, 2024 12:14
[FCC First class and Closures Blog] Returning Functions from Other Functions
def create_multiplier(factor):
"""Returns a function that multiplies its input by the given factor."""
def multiplier(x):
return x * factor
return multiplier
# Create specific multiplier functions
double = create_multiplier(2)
triple = create_multiplier(3)
@theSamyak
theSamyak / app.py
Last active May 25, 2024 13:35
[FCC First class functions and Closures Blog] Passing Functions as Arguments
def double(n):
return n * 2
def map_function(func, values):
result = []
for value in values:
result.append(func(value))
return result
# Use the custom map function
@theSamyak
theSamyak / app.py
Last active May 25, 2024 13:34
[FCC First class functions and Closures Blog] Assigning Functions to Variables
def add(a, b):
return a + b
result = add(3, 4)
print(add) # Prints the function object
print(result) # Prints 7