Skip to content

Instantly share code, notes, and snippets.

If constructing an object is entagled with different cases and conditions it's better to use factory. That's not the case here.

Instead of this.

def make_board(mines, size):
    '''function that uses my created Board methods to create the playing board'''
    board = Board(tuple([tuple([Cell(True,True,True) for i in range(size+1)]) for j in range(size+1)]))
    open_pos = list(range(size-1)*(size-1))
    for i in range(mines):
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 #-}