Skip to content

Instantly share code, notes, and snippets.

View gribnoysup's full-sized avatar

Sergey Petushkov gribnoysup

View GitHub Profile
@gribnoysup
gribnoysup / balancedParentheses.js
Last active August 27, 2015 17:22
Balanced Parenthesis check
function balancedParentheses (string) {
var stack = new Stack();
var i, len;
string = string
// remove any strings or regexes used in target string (if it is a function for example)
.replace(/(\`.{0,}\`|\/.{0,}\/|\'.{0,}\'|\".{0,}\")/g, '');
for (i = 0, len = string.length; i < len; i++) {
switch (string[i]) {
@gribnoysup
gribnoysup / evalPostfix.js
Created August 27, 2015 17:23
Evaluate postfix expression
function evalPostfix (expr) {
var i = 0, len = 0, op1, op2;
var stack = new Stack();
for (i = 0, len = expr.length; i < len; i++) {
if (expr[i].match(/[^\+\-\*\/\%]/)) {
stack.push(expr[i]);
} else {
op2 = stack.pop();
op1 = stack.pop();
@gribnoysup
gribnoysup / infixToPostfixNoParentheses.js
Created August 27, 2015 17:25
Infix to postfix convertion (without parentheses)
function infixToPostfixNoParentheses (expr) {
var postfix = '', i = 0, len = 0;
var stack = new Stack();
for (i, len = expr.length; i < len; i++) {
if (expr[i].match(/[^\+\-\*\/\%]/)) {
postfix += expr[i];
} else if (stack.isEmpty()) {
stack.push(expr[i]);
} else {
@gribnoysup
gribnoysup / infixToPostfixWithParentheses.js
Created August 27, 2015 17:25
Infix to postfix convertion (with parenthesis)
function infixToPostfixWithParentheses (expr) {
if (!balancedParentheses(expr)) return false;
var postfix = '', i = 0, len = 0;
var stack = new Stack();
for (i, len = expr.length; i < len; i++) {
if (expr[i].match(/[^\(\)\+\-\*\/\%]/)) {
postfix += expr[i];
} else if (stack.isEmpty()) {
@gribnoysup
gribnoysup / Stack.js
Last active August 28, 2015 08:04
Super simple stack implementation for data structure tests
function Stack () {
this.__arr = [];
}
Stack.prototype = {
constructor : Stack,
push : function push (elem) {
if (typeof elem !== 'undefined') return this.__arr.push(elem);
},
pop : function pop () {
@gribnoysup
gribnoysup / StaticQueue.js
Last active August 28, 2015 08:55
Static Queue implementation
function StaticQueue (size) {
this.__arr = {};
this.__size = size;
this.__front = -1;
this.__rear = -1;
for (var i = 0; i < size; i++) {
this.__arr[i] = undefined;
}
}
@gribnoysup
gribnoysup / LinkedList.js
Last active August 28, 2015 13:59
Linked List implementation
function Node (val) {
this.val = val;
this.next = null;
}
function LinkedList (firstVal) {
this.__head = null;
this.__length = 0;
if (typeof firstVal !== 'undefined') {
this.append(firstVal);
@gribnoysup
gribnoysup / binarySearch.js
Last active August 29, 2015 08:09
Binary search, binary search with recursion, rotation count, circular array search
// first = true - find first occurance
// first = false - find last occurance
// first = undefined - first found
function binarySearch(array, x, first) {
var start = 0, end = array.length - 1, mid = 0, result = -1;
while (start <= end) {
mid = Math.floor((start + end) / 2);
if (array[mid] == x) {
result = mid;
@gribnoysup
gribnoysup / binarySearchTree.js
Created August 30, 2015 10:07
Binary Search Tree (BST)
function BinarySearchTree (rootData) {
this.root = null;
this.nodes = 0;
this.add(rootData);
}
BinarySearchTree.prototype = {
constructor : BinarySearchTree,
add : function add (data) {
if (this.root === null) {
@gribnoysup
gribnoysup / trigger.md
Last active April 9, 2021 02:10
Trigger transition on dynamically appended DOM element

HTML

<p>
  <button class="show">show</button>&nbsp;<button class="hide">hide</button>
</p>
<p class="animation-container"></p>

CSS