Skip to content

Instantly share code, notes, and snippets.

View singhAmandeep007's full-sized avatar
🎯
Focusing

Amandeep Singh singhAmandeep007

🎯
Focusing
View GitHub Profile
@singhAmandeep007
singhAmandeep007 / higherOrderFunctions.py
Last active July 26, 2024 17:48
Higher order functions in python with typing
def apply_operation(operation: Callable[[int], int], numbers: List[int]) -> List[int]:
result = []
for num in numbers:
result.append(operation(num))
return result
def square(num: int) -> int:
return num * num
@singhAmandeep007
singhAmandeep007 / fibonacci.py
Last active April 25, 2024 12:38
Caching fibonacci function using decorators in python
def count_calls(func):
"""
This function is a decorator that keeps track of the number of times a function is called.
It takes a function as input and returns a new function that behaves exactly like the input function,
but also increments a counter and prints the number of calls each time the function is called.
:param func: The function to be decorated.
:return: The decorated function that counts and prints the number of calls.
"""
@singhAmandeep007
singhAmandeep007 / tictactoe.py
Created April 25, 2024 03:32
Tic Tac Toe game in python
def display_board(board, size=3):
"""
Prints the Tic Tac Toe board to the terminal in a formatted way.
"""
for idx in range(0, size):
print("".join(list(map(lambda i: f" {board[i + idx * size]}{' |' if i < size - 1 else ''}", range(0, size)))))
if idx < size - 1:
print(f"{'---|---|---'}")
def is_valid_move(board, position):
@singhAmandeep007
singhAmandeep007 / msw.middleware.ts
Created April 10, 2024 08:09
MSW request middleware error helper
import { HttpResponse, JsonBodyType } from "msw";
export const errors = {
resourceNotFound: (
body: JsonBodyType = { error: { message: "Resource not found" } }
) => HttpResponse.json(body, { status: 404 }),
unauthorized: (
body: JsonBodyType = {
error: {
message: "Not authorized to access resource"
@singhAmandeep007
singhAmandeep007 / setupTests.ts
Created April 10, 2024 04:08
Mocking localStorage jest
const localStorageMock: Storage = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
length: 0,
key: jest.fn()
};
global.localStorage = localStorageMock;
@singhAmandeep007
singhAmandeep007 / concurrent.sample.test.ts
Last active April 10, 2024 07:58
Concurrent test in jest v29.7
import { expect, test } from "@jest/globals";
function sum(...args: number[]): number {
return args.reduce((acc, val) => acc + val, 0);
}
function isSumOddOrEven(...args: number[]): string {
const total = sum(...args);
if (total % 2 === 0) {