Skip to content

Instantly share code, notes, and snippets.

View lovasoa's full-sized avatar
🎯
Focusing

Ophir LOJKINE lovasoa

🎯
Focusing
View GitHub Profile
@lovasoa
lovasoa / find_pair_with_sum.md
Last active February 9, 2021 06:22
Working at google: answers to google job interview programming problem mentioned in https://www.youtube.com/watch?v=XKu_SEDAykw

find_pair_with_sum

Given a sorted array of integers and a number n, find a pair of numbers in the array whose sum is n.

Solution

Algorithm

Start with a pair constituted of the first and last numbers of the sorted array. At each step, compute the sum of the current pair. If the sum is larger than our objective, take following element as the first of our pair.

@lovasoa
lovasoa / dichotomic-bash.sh
Last active December 6, 2023 23:57
Generic dichotomic search as a shell script
#!/usr/bin/env bash
# Dichotomic search in bash
# This is a bash-specific function that uses bash features.
# This is not guaranteed to work in other shells.
#
# Usage:
# source dichotomic-bash.sh
# result=$(dichotomic_search min max command)
#
# Returns the largest i for which `command i` succeeds (exits with a null exit code)
@lovasoa
lovasoa / binary_search_replace.py
Last active December 13, 2016 15:31
Replace text in a binary file, padding the replacement text with spaces
#!/usr/bin/env python3
# Replace text in a binary file, padding the replacement text with spaces
import sys, locale
if len(sys.argv) != 3:
sys.stderr.write("Usage: %s string_from string_to < input.bin > output.bin.\n" % (sys.argv[0],))
sys.exit(1)
codec = locale.getpreferredencoding()
orig= bytes(sys.argv[1], codec)
@lovasoa
lovasoa / binary-tree.elm
Created February 10, 2017 18:47
Binary trees in elm
{- OVERVIEW ------------------------------------------------------
A "Tree" represents a binary tree. A "Node" in a binary tree
always has two children. A tree can also be "Empty". Below I have
defined "Tree" and a number of useful functions.
This example also includes some challenge problems!
-----------------------------------------------------------------}
@lovasoa
lovasoa / BowyerWatson.js
Last active March 2, 2017 13:33
BowyerWatson algorithm in js
// Copyright (C) 2015 Dominic Charlesworth <[email protected]>
// Author: Dominic Charlesworth <[email protected]>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
@lovasoa
lovasoa / flatten.py
Last active March 5, 2017 14:42
flatten an array of arbitrarily nested arrays into a flat array (e.g. [[1,2,[3]],4] -> [1,2,3,4])
#!/usr/bin/env python3
"""
Small module to flatten iterables
"""
def flatten(obj):
"""Flatten any nested iterables.
>>> flatten([[1,2,[3]],4])
[1, 2, 3, 4]
@lovasoa
lovasoa / xpath.js
Created March 17, 2017 18:10
Simple xpath function for querying the DOM. Takes a string and returns an array of dom nodes.
function xpath(query) {
var arr = [];
var results = document.evaluate(query, document, null, XPathResult.ANY_TYPE, null );
while(r = results.iterateNext()) arr.push(r);
return arr;
}
@lovasoa
lovasoa / NotInPair.java
Last active March 31, 2017 16:05
Дан целочисленный массив из 1001 элемента , все числа в массиве имеют пару и одно уникальное. найдите это число за O(n).
import java.util.*;
/*
Дан целочисленный массив из 1001 элемента ,
все числа в массиве имеют пару и одно уникальное.
найдите это число за O(n).
*/
class NotInPair {
@lovasoa
lovasoa / new-rules.xml
Last active May 4, 2017 15:32
Quick-and-dirty command-line editor for WiMi model files
<model description="Model 1" formatXmlVersion="2.0" id="{ed7a8cc7-38a3-4ca4-8373-751d7374bc9e}" shortName="Model 1">
<class id="{1f381b22-438a-4243-987c-51fdd36758a8}" shortName="Model 1">
<parameters />
<rules>
<rule description="tk=0,5(tk1+tk2)" id="005516d4-2cbf-4e6d-9836-70c2e424e4a6" initId="b:cb01c927-966b-433a-a53a-30e6b9f5ba51;c:3e35e46f-c897-4376-93c9-d83d9910942e" relation="dbae2f6c-b823-4e27-9067-080feece5355" resultId="a:478c281f-8d74-48ac-87ed-cb57ed515049" shortName="tk(&#1089;&#1088;&#1077;&#1076;&#1085;&#1077;&#1077; &#1079;&#1085;&#1072;&#1095;&#1077;&#1085;&#1080;&#1077; &#1074;&#1088;&#1077;&#1084;&#1077;&#1085;&#1080; &#1086;&#1073;&#1088;&#1072;&#1073;&#1086;&#1090;&#1082;&#1080; &#1079;&#1072;&#1087;&#1088;&#1086;&#1089;&#1072; &#1074; &#1082;&#1072;&#1085;&#1072;&#1083;&#1077; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080; &#1076;&#1072;&#1085;&#1085;&#1099;&#1093;)" />
<rule id="2cedfec1-9f66-4684-b84b-ab842827d4ce" initId="T0:7e954f2b-1ed2-4273-beeb-993a9f0e7d4c;Td:a4044004-
@lovasoa
lovasoa / find_tesseract_param.py
Created May 6, 2017 16:48
Find optimal parameters for tesseract OCR, given a set of input files and expected output.
#!/usr/bin/env python3
import Levenshtein
import scipy.optimize
import subprocess
import glob
import sys
p = subprocess.Popen(["tesseract", "--print-parameters"], stdout=subprocess.PIPE)
args, _ = p.communicate()