Skip to content

Instantly share code, notes, and snippets.

View vadimkorr's full-sized avatar
🦉

Vadim vadimkorr

🦉
View GitHub Profile
@vadimkorr
vadimkorr / input.component.ts
Created March 23, 2018 07:47
NG_VALUE_ACCESSOR provider
...
@Component({
selector: 'custom-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.css'],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => InputComponent),
multi: true,
}]
@vadimkorr
vadimkorr / input.component.ts
Last active March 25, 2018 12:36
ControlValueAccessor implementation
import { Component, Input, ViewChild, ElementRef, Renderer2, forwardRef } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
@Component({
selector: 'custom-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.css']
})
export class InputComponent implements ControlValueAccessor {
@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';
<input
#inputElement
[value]="value"
[type]="type"
[id]="id"
[placeholder]="placeholder"
(change)="onChange($event)"
(keyup)="onKeyup($event)"
(blur)="onBlur($event)">
@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: {
@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 / 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 / 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 / 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 / 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]