Skip to content

Instantly share code, notes, and snippets.

@Sinha-Ujjawal
Sinha-Ujjawal / subset_sum.py
Last active May 24, 2023 16:04
Subset Sum using mknapsack library
import numpy as np
from mknapsack import solve_subset_sum
def subset_sum(*, weights: np.array, capacity: float) -> np.array:
assert capacity > 0, f"Capacity must be positive!, given: {capacity}"
n_weights = len(weights)
if n_weights < 2:
@Sinha-Ujjawal
Sinha-Ujjawal / either_type.py
Last active October 23, 2022 14:59
Either type in Python
from typing import Generic, Tuple, TypeVar, Iterable, List, Optional, Any, Union
L = TypeVar("L")
R = TypeVar("R")
# SKI+ Combinators
S = lambda f: lambda g: lambda x: f(x)(g(x))
K = lambda x: lambda y: x
I = S(K)(K(K))
KI = K(I)
@Sinha-Ujjawal
Sinha-Ujjawal / linked_list_skipy.py
Last active September 25, 2022 08:15
Linked List Definition In Python Using SKI Combinators
from typing import Generic, Tuple, TypeVar, Iterable, List, Optional, Any
L = TypeVar("L")
R = TypeVar("R")
# SKI+ Combinators
S = lambda f: lambda g: lambda x: f(x)(g(x))
K = lambda x: lambda y: x
I = S(K)(K(K))
KI = K(I)
@Sinha-Ujjawal
Sinha-Ujjawal / sk.py
Created September 25, 2022 07:47
SK Combinators (Building blocks for all pure functions)
S = lambda f: lambda g: lambda x: f(x)(g(x))
K = lambda x: lambda y: x
@Sinha-Ujjawal
Sinha-Ujjawal / bifunctor.hs
Created September 10, 2022 13:20
BiFunctor in Haskell
class Bifunctor f where
bimap :: (a -> a') -> (b -> b') -> f a b -> f a' b'
@Sinha-Ujjawal
Sinha-Ujjawal / either_type.hs
Created September 10, 2022 13:19
Either Type in Haskell
data Either a b = Left a | Right b
-- Implicit functions
-- Left :: a -> Either a b
-- Right :: b -> Either a b
@Sinha-Ujjawal
Sinha-Ujjawal / pair_type.hs
Created September 10, 2022 13:18
Pair Type in Haskell
data Pair a b = Pair a b
fst :: Pair a b -> a
fst (Pair a _) = a
snd :: Pair a b -> b
snd (Pair _ b) = b
@Sinha-Ujjawal
Sinha-Ujjawal / universal_constructions.hs
Created September 10, 2022 13:11
Universal Constructions in Haskell
{-# LANGUAGE ScopedTypeVariables #-}
import Prelude (IO, pure, print, String, Bool(True, False), Int, Show)
(.) :: (b -> c) -> (a -> b) -> (a -> c)
g . f = \x -> g (f x)
($) :: (x -> y) -> x -> y
f $ x = f x
id :: a -> a
@Sinha-Ujjawal
Sinha-Ujjawal / Playground.hs
Last active August 24, 2021 11:24
Plutus Playground Smart Contract
-- This is a starter contract, based on the Game contract,
-- containing the bare minimum required scaffolding.
--
-- What you should change to something more suitable for
-- your use case:
-- * The MyDatum type
-- * The MyMyRedeemerValue type
--
-- And add function implementations (and rename them to
-- something suitable) for the endpoints: