Skip to content

Instantly share code, notes, and snippets.

View vadimkorr's full-sized avatar
🦉

Vadim vadimkorr

🦉
View GitHub Profile
@vadimkorr
vadimkorr / mul-of-vals-except-curr.js
Created March 16, 2018 12:26
Given an array, assign the multiplication of all values except current for each position (in-place)
// Given an array, assign the multiplication of all values except current for each position (in-place)
function getMul(arr) {
let res = arr.reduce((prev, curr, index, array) => prev * curr, 1);
arr.map(v => console.log(res/v));
}
let arr = [1, 2, 3, 4, 5, 6];
getMul(arr);
@vadimkorr
vadimkorr / merge-sort.js
Created March 17, 2018 17:40
Merge sort
// Merge sort
function mergeSort(vals, scrtach, start, end) {
if (start == end) return;
let mid = Math.floor((start + end) / 2);
mergeSort(vals, scratch, start, mid);
mergeSort(vals, scratch, mid + 1, end);
@vadimkorr
vadimkorr / all-possinle-sums.js
Created March 17, 2018 18:06
Find all possible sums made from subsets of items from array A
// Find all possible sums made from subsets of items from array A
function subsetSums(arr, l, r, sum = 0) {
// Print current subset
if (l > r) {
console.log(sum);
return;
}
// Subset including arr[l]
@vadimkorr
vadimkorr / count-inversions-in-array.js
Created March 17, 2018 18:20
Count amount of inversions in array
// Count inversions of array
function getInvCount(arr) {
let n = arr.length;
let invCount = 0;
for (let i = 0; i < n - 1; i++)
for (let j = i + 1; j < n; j++)
if (arr[i] > arr[j])
invCount++;
@vadimkorr
vadimkorr / min-diff.js
Created March 17, 2018 18:50
Given an array of n integers, find and print the minimum absolute difference between any two elements in the array (where i!=j, 2<=n<=10^9)
// Given an array of n integers, find and print the minimum absolute difference
// between any two elements in the array (where i!=j, 2<=n<=10^9)
function findMinDiff(arr) {
let n = arr.length;
// Sort array in non-decreasing order
arr.sort(function(a, b) {
return a - b;
});
@vadimkorr
vadimkorr / queue-based-on-stack.js
Last active March 17, 2018 20:13
Implement Queue interface using Stack interface as a storage
// Implement Queue interface using Stack interface as a storage
function queue() {
this.stack = [];
}
queue.prototype.enq = function(item) {
this.stack.push(item);
}
@vadimkorr
vadimkorr / restore-bracket-sequence.js
Created March 19, 2018 19:01
Restore bracket sequence
// Restore bracket sequence
function restore(str) {
let arr = [];
let counter = 0;
for(let i=0; i<str.length; i++) {
if (str[i]=='(') {
arr.push('(');
counter++;
} else { // str[i]==')'
@vadimkorr
vadimkorr / parse-tree-and-solve.js
Last active March 20, 2018 21:28
Parse tree and find result of expression'
const tree = {
and: {
left: {
and: {
left: {
le: {
left: {
fieldRef: "a.count"
},
right: {
<input
#inputElement
[value]="value"
[type]="type"
[id]="id"
[placeholder]="placeholder"
(change)="onChange($event)"
(keyup)="onKeyup($event)"
(blur)="onBlur($event)">
@vadimkorr
vadimkorr / input.component.ts
Last active March 25, 2018 12:23
input.component.ts
import { Component, Input, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'custom-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.css']
})
export class InputComponent {
value: string = '';
@Input() type: string = 'text';