Skip to content

Instantly share code, notes, and snippets.

View izmailoff's full-sized avatar
🎯
Focusing

Aleksey Izmailov izmailoff

🎯
Focusing
View GitHub Profile
@izmailoff
izmailoff / virtual python environment.sh
Last active May 16, 2022 08:14
Setup virtual python environment
# OSX specific install commands:
brew install pyenv
pyenv install 3.6.8
# or if fails:
CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 3.6.8
# the rest of the commands work both on OSX and Linux:
virtualenv -p /Users/alex/.pyenv/versions/3.6.8/bin/python venv
source venv/bin/activate
import org.scalatest.{Assertion, AsyncFlatSpec}
import scala.concurrent.{Future, Await}
import org.scalatest.Matchers._
import scala.concurrent.duration._
import scala.language.postfixOps
class FutureSpec extends AsyncFlatSpec {
// Overriding execution context is not required, unless you want to use Await.
// To avoid deadlocking the main thread you need to have multithreaded context.
@izmailoff
izmailoff / change_basis.py
Created March 8, 2018 07:05
Change of basis for vectors
import numpy as np
def change_basis(v, dims):
"""
:param v: vector of any dimension
:param dims: basis of new dimension matching dimensions of v
:return: the same vector v in new coordinate space
"""
return [np.dot(v, x) / np.dot(x, x) for x in dims]
@izmailoff
izmailoff / find_interval.py
Last active February 12, 2018 06:54
Find the interval in a list of non-overlapping intervals that contains a value. Example of geoip table lookup: given an IP find a location. Motivated by: https://dev.maxmind.com/geoip/geoip2/geoip2-city-country-csv-databases/
# Problem: given this data (xs) and value (x) find it's mapped str value ('SG', ...)
# Tuple format: (interval_start_inclusive, interval_end_inclusive, searched_value)
db = [(8123, 9888, 'SG'), (1, 1000, 'US'), (1001, 5045, 'CA')]
# Solution: sort once
db_sorted = sorted(db, key=lambda x: x[0])
# Implement binary search (for practice purposes):
def binary_search(x, xs):
if not xs:
@izmailoff
izmailoff / balanced_binary_tree.py
Created February 12, 2018 06:06
A function that checks if binary tree is balanced
def height_min_max(tree):
if not tree:
return 0, 0
else:
_, l, r = tree
lmin, lmax = height_min_max(l)
rmin, rmax = height_min_max(r)
return 1 + min(lmin, rmin), 1 + max(lmax, rmax)
@izmailoff
izmailoff / max_subarray.py
Created February 9, 2018 05:49
Implements maximum subarray algorithm as described in: https://en.wikipedia.org/wiki/Maximum_subarray_problem
def max_subarray(xs):
if not xs:
return [], 0
start = 0
end = 1
cur_sum = 0
max_sum = xs[0]
for i in range(len(xs)):
tmp_sum = cur_sum + xs[i]
if xs[i] > tmp_sum:
@izmailoff
izmailoff / expression_problem.hs
Created January 2, 2018 08:47 — forked from chrisdone/expression_problem.hs
Solving the Expression Problem with Haskell
{-# LANGUAGE NamedFieldPuns #-}
-- The Expression Problem and my sources:
-- http://stackoverflow.com/questions/3596366/what-is-the-expression-problem
-- http://blog.ontoillogical.com/blog/2014/10/18/solving-the-expression-problem-in-clojure/
-- http://eli.thegreenplace.net/2016/the-expression-problem-and-its-solutions/
-- http://www.ibm.com/developerworks/library/j-clojure-protocols/
-- To begin demonstrating the problem, we first need some
@izmailoff
izmailoff / ExpressionProblemWithImplicits.scala
Last active February 13, 2023 13:00 — forked from elnygren/expression_problem.clj
Solving the Expression Problem with Scala's Implicits
object Main extends App {
import math._
// The Expression Problem and my sources:
// http://stackoverflow.com/questions/3596366/what-is-the-expression-problem
// http://blog.ontoillogical.com/blog/2014/10/18/solving-the-expression-problem-in-clojure/
// http://eli.thegreenplace.net/2016/the-expression-problem-and-its-solutions/
// http://www.ibm.com/developerworks/library/j-clojure-protocols/
@izmailoff
izmailoff / functional_sets_in_python.py
Last active December 15, 2017 03:37
Example of functional sets in Python
set_of_1 = lambda i: i == 1
set_of_1(1)
set_of_1(2)
empty = lambda i: False
empty(1)
@izmailoff
izmailoff / setup_docker_fedora.sh
Created October 22, 2017 08:13
Setup docker on Fedora without sudo
sudo dnf install docker
# Start/enable service
sudo systemctl start docker
sudo systemctl enable docker
# Allow docker to run without sudo/root privs
sudo groupadd docker && sudo gpasswd -a ${USER} docker && sudo systemctl restart docker
newgrp docker