Skip to content

Instantly share code, notes, and snippets.

View kottenator's full-sized avatar

Rostyslav Bryzgunov kottenator

  • Facebook
  • Seattle, WA
View GitHub Profile
@kottenator
kottenator / diff.js
Created April 15, 2018 19:54
Own custom diff
/*
Specific conditions:
- input: `oldString` and `newString` (e.g. `'old same'` and `'same new'`)
- output format: `'(old )same[ new]'`
- return diff of min length, not counting `()[]`
- return diff which is min among all diff variants by lexicographical order, with one remark: consider `\w` < `()` < `[]`
Taken from CodeFights: Dropbox bot challange.
*/
@kottenator
kottenator / breadth-first traversal.js
Created March 4, 2018 17:53
Breadth-first graph traversal (BFS)
function breadthFirstTraverse(connections, start = 0) {
let stack = [[start, 0]]; // keep node index & current depth
let visited = new Set;
while (stack.length) {
let newStack = [];
log('Current stack:', stack);
for (let [i, depth] of stack) {
log(`Visit ${i} (depth: ${depth})`);
@kottenator
kottenator / depth-first graph traversal.js
Last active March 4, 2018 17:53
Depth-first graph traversal (DFS)
/**
* General implementation of depth-first graph traversal (OK to have loops),
* no recursion.
*/
function depthFirstTraverse(connections, i=0) {
let visited = {};
let traverseStack = [[-1, i]]; // edge from A to B vertex
let revisit = false;
while (traverseStack.length) {
@kottenator
kottenator / in-order traversal no recursion.js
Created March 1, 2018 13:54
In-order traverse without recursion
/**
* Binary tree in-order traversal.
*
* How would look this simple, straightforward algorithm without recursion?
* I don't like recursion, that's one of my attempts to avoid it.
*/
function traverseInOrderNoRecursion(t) {
if (!t) {
return [];
}
@kottenator
kottenator / decodeString solution.js
Last active March 4, 2018 16:53
CodeFights: decodeString - O(n) solution
/**
* Solution for this - https://codefights.com/interview-practice/task/dYCH8sdnxGf5aGkez
*/
function decodeString(s) {
return parse(s)[0];
}
function parse(string, startIdx=0) {
let i = startIdx;
let res = '';
@kottenator
kottenator / countClouds solution.js
Last active March 4, 2018 16:53
CodeFights: countClouds - crazy hard way to solve it
/**
* Solution for this: https://codefights.com/interview-practice/task/HdgqPhHqs3NciAHqH
*/
function countClouds(skyMap) {
let clouds = [];
let completeClouds = 0;
for (let skyLine of skyMap) {
let newCloudsMapping = {
cloudToRange: {}, // {[cloud number]: [list of ranges]} - to continue the cloud
javascript:void(function(){var el=document.getElementById('my-grid');if(el){el.style.display=el.style.display=='block'?'none':'block';}else{el=document.createElement('div');document.body.appendChild(el);el.id='my-grid';el.style.cssText='background-image:url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' width=\'1\' height=\'25.5\'><rect style=\'fill: rgb(255,0,0);\' width=\'1\' height=\'0.25px\' x=\'0\' y=\'0\'/></svg>");position:absolute;left:0;right:0;top:0;z-index:9999;pointer-events:none;background-position:0 3px;display:block;';function resize(){el.style.height=document.body.scrollHeight+'px';}resize();document.body.addEventListener('resize', resize);}}())
@kottenator
kottenator / .inputrc
Created January 15, 2017 01:58
Bash completion and navigation
set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on
# Tab completion
TAB: menu-complete
# Shift + Tab - backward completion
"\e[Z": "\e-1\C-i"
@kottenator
kottenator / kott.theme.bash
Last active January 15, 2017 01:58
Custom theme for bash-it
# Custom theme for https://github.com/bash-it/bash-it
# Place it into ~/.bash_it/custom/themes/kott/kott.theme.bash
# Enable it in your ~/.bashrc: export BASH_IT_THEME='kott'
SCM_THEME_PROMPT_DIRTY="✗ "
SCM_THEME_PROMPT_CLEAN="✓ "
SCM_THEME_PROMPT_PREFIX=" ("
SCM_THEME_PROMPT_SUFFIX=")"
function scm_prompt_info {
@kottenator
kottenator / jQuery plugin with browserify-shim.md
Last active May 21, 2019 18:17
jQuery plugin with browserify-shim

Goal

Demonstrate how to configure browserify with browserify-shim to exclude jQuery from the bundle and to make jQuery plugin (that supports CommonJS modules) work correctly, using global window.jQuery object.

Step 1: create files

index.html:

<script src="node_modules/jquery/dist/jquery.js"></script>