Skip to content

Instantly share code, notes, and snippets.

import Data.Function.Memoize (memoize)
import Data.List (maximumBy, group)
import qualified Data.Map as Map
import Data.Map.Lazy (empty, insertWith, size, Map)
import Data.Ord (comparing, compare, Down)
import Data.Monoid
import System.Environment (getArgs)
type Solution = (Map Integer Integer, Integer)
@purpleP
purpleP / minmax.hs
Created September 5, 2017 13:33
Efficient algorithm for finding minimum and maximum value from sequence
import Data.Tuple (swap)
import System.Environment (getArgs)
minmax (a:b:xs) = foldl (minmax') (sort' (a, b)) (pairs xs)
where minmax' (curmin, curmax) candidates = (newmin, newmax)
where (cmin, cmax) = sort' candidates
newmin = if cmin < curmin then cmin else curmin
newmax = if cmax > curmax then cmax else curmin
sort' tup@(a, b) = if a <= b then tup else swap tup
@purpleP
purpleP / min_max_open_close
Created October 12, 2017 19:06
Minumum, maximum and first and last from group without `first` and `last` in mysql (it doesn't have them)
create table `values` (
id integer unsigned primary key auto_increment,
value double,
`time` time
);
insert into `values` (time, value) values
('00:00:01', 3),
('00:00:02', 2),
('00:00:03', 1),
class split:
def __init__(self, delimiter, sequence):
"""
Break a sequence on particular elements.
Return an iterator over chunks.
Arguments:
delimiter if a function, it returns True on chunk separators;
otherwise, it is the value of chunk separator.
from bisect import bisect_left, bisect_right
def pairs_sums_to(k, xs):
xs = [*sorted(xs)]
lo, hi = 0, len(xs) - 1
while True:
left_pos = bisect_left(xs, k, lo, hi)
if left_pos > hi or left_pos == lo:
return
@purpleP
purpleP / merge.py
Last active February 19, 2019 13:43
merge
from collections import Counter
import hypothesis.strategies as st
import hypothesis as hp
import pytest
def merge(xs, ys):
xs, ys = iter(xs), iter(ys)
none = object()
y = next(ys, none)
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
@purpleP
purpleP / vim-tmux.md
Created January 29, 2017 00:37
How to run tests with tmux and vim

Running tests with vim and tmux

After using vim and neovim for almost a year now I've come to the point when frustration I had with running test with vim made me think about ideal workflow for running tests (at least in command line, but some rules apply to GUI).

Below you can read my thoughts about ideal workflow, a summary of problems with current workflows (at least that I know of, but I think I've tried them all) and my own new (well, at least I think it's new) solution to them.