Skip to content

Instantly share code, notes, and snippets.

View Abhiroop's full-sized avatar
:electron:
"Syntactic sugar causes cancer of the semicolon."

Abhiroop Sarkar Abhiroop

:electron:
"Syntactic sugar causes cancer of the semicolon."
View GitHub Profile
@Abhiroop
Abhiroop / gist:6366385
Last active December 21, 2015 21:09
Knight's Tour
board = []
board_size = -1
def initiateBoard (board_dimensions):
global board
global board_size
global knights_moves
board_size = board_dimensions
for i in range(0, board_size):
board.append(board_size*[0])
settings_table = {
{
-- Edit this table to customise your rings.
-- You can create more rings simply by adding more elements to settings_table.
-- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
name='time',
-- "arg" is the argument to the stat type, e.g. if in Conky you would write ${cpu cpu0}, 'cpu0' would be the argument. If you would not use an argument in the Conky variable, use ''.
arg='%I.%M',
-- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
max=12,
@Abhiroop
Abhiroop / Performance
Last active August 1, 2016 20:07
Top k elements from a vector
Timing the performance of various ways of extracting top k elements from a vector:
Here sequence size is about 10000
and k=300
Here is a randomly generated vector containing 10K maps. Each of the maps look like this
{
:id 123
:data (0 1 2 3 ....)
:more-data (0 1 2 3 ....)
@Abhiroop
Abhiroop / BST.hs
Last active October 16, 2017 14:37
Creating a balanced binary search tree type in Haskell
{-# LANGUAGE GADTs, DataKinds #-}
module BST where
data Nat = Zero | Succ Nat
data Tree n a where
Branch :: T n a -> Tree (Succ n) a
Leaf :: Tree Zero a
data T n a = NodeR (Tree n a) a (Tree (Succ n) a) -- right subtree has height + 1
data Color = R | B deriving Show
data Tree a = E | T Color (Tree a) a (Tree a) deriving Show
@Abhiroop
Abhiroop / block2.1
Last active October 30, 2017 12:00
No red node has a red parent or a red node has only black children.
Every path from the root node to an empty node contains the same number of black nodes.
The root and leaves of the tree are black.
member :: (Ord a) => a -> Tree a -> Bool
member x E = False
member x (T _ a y b)
| x < y = member x a
| x == y = True
| otherwise = member x b
insert :: (Ord a) => a -> Tree a -> Tree a
insert x s = makeBlack $ ins s
where ins E = T R E x E
ins (T color a y b)
| x < y = balance color (ins a) y b
| x == y = T color a y b
| x > y = balance color a y (ins b)
makeBlack (T _ a y b) = T B a y b
T B (T R (T R a x b) y c) z d
T B (T R a x (T R b y c)) z d
T B a x (T R (T R b y c) z d)
T B a x (T R b y (T R c z d))
balance :: Color -> Tree a -> a -> Tree a -> Tree a
balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
balance color a x b = T color a x b