Skip to content

Instantly share code, notes, and snippets.

Avatar

Rostyslav Bryzgunov kottenator

  • Facebook
  • Seattle, WA
View GitHub Profile
@kottenator
kottenator / diff.js
Created Apr 15, 2018
Own custom diff
View diff.js
/*
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 Mar 4, 2018
Breadth-first graph traversal (BFS)
View breadth-first traversal.js
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 Mar 4, 2018
Depth-first graph traversal (DFS)
View depth-first graph traversal.js
/**
* 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 Mar 1, 2018
In-order traverse without recursion
View in-order traversal no recursion.js
/**
* 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 Mar 4, 2018
CodeFights: decodeString - O(n) solution
View decodeString solution.js
/**
* 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 Mar 4, 2018
CodeFights: countClouds - crazy hard way to solve it
View countClouds solution.js
/**
* 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
View Vertical rhythm bookmarklet.js
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 Jan 15, 2017
Bash completion and navigation
View .inputrc
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 Jan 15, 2017
Custom theme for bash-it
View kott.theme.bash
# 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
jQuery plugin with browserify-shim
View jQuery plugin with browserify-shim.md

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>