Skip to content

Instantly share code, notes, and snippets.

View unilecs's full-sized avatar

UniLecs unilecs

View GitHub Profile
@unilecs
unilecs / revertWordsInString.js
Created October 3, 2017 20:48
Revert words in the string
function revertWordsInString(str) {
let inputStr = str.trim();
let words = [], curWord = "";
for(let i=0; i<str.length; i++) {
// если встретили не пробел, формируем слово
if (str[i] !== " ") {
curWord += str[i];
}
@unilecs
unilecs / compressString.js
Created October 3, 2017 20:51
Compress string: ABBCCCD -> AB2C3D
function compressString(str) {
// regexp
const zip = str.replace(/(.)\1{0,}/g,
group => (group[0] + (group.length > 1 ? group.length : '')));
return zip;
}
const str = 'AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD';
console.info("Compression of string: ", compressString(str));
@unilecs
unilecs / compressString.js
Created October 3, 2017 20:54
Compress string: ABBCCCD -> AB2C3D
function CompressString(str) {
let acc = 0;
let char;
let result = str.split('').reduce((r, currentValue, index, arr) => {
if (currentValue != arr[index-1]) {
if (char) {
r += ((acc > 1) ? `${char}${acc}` : char);
}
@unilecs
unilecs / checkUniqueSymbols.js
Created October 3, 2017 20:59
Check that all symbols are unique in the string
function checkUniqueSymbols(inputStr) {
if (inputStr) {
const hasDuplicateSymbols = inputStr.match(/(\w)(.+)?\1/) !== null;
return !hasDuplicateSymbols;
}
return false;
}
console.info("abcadez", checkUniqueSymbols("abcadez")); // false
console.info("abcdefg", checkUniqueSymbols("abcdefg")); // true
console.info(" try ", checkUniqueSymbols(" try ")); // true
@unilecs
unilecs / waterVolume.js
Last active May 2, 2018 16:24
Get Sum Volume of water
function waterVolume(arr) {
let leftMax = 0, rightMax = 0, volume = 0;
let left = 0, right = 0;
let leftLocalMaxArr = [], rightLocalMaxArr = [];
for(let i=0; i<arr.length; i++) {
left = i;
right = arr.length - 1 - i;
// локальные максимумы слева
@unilecs
unilecs / giveMyMoney.js
Created October 5, 2017 15:49
Task about giving cash from ATM
function giveMyMoney(money, coins) {
let needCoins = [], minCoins = []; minCoins[0] = 0;
// перебираем все суммы до заданной суммы (можно начать с мин.купюры)
for (let sum = 1; sum <= money; sum++) {
// по умолчанию эта сумма не для выдачи
minCoins[sum] = Number.MAX_VALUE;
@unilecs
unilecs / getUniqueNumberInArray.js
Created October 7, 2017 19:59
Get number w/o pair in the array of numbers
function getUniqueNumberInArray(arr) {
if (!arr || !arr.length) {
return null;
}
let result = arr[0];
for (let i=1; i<arr.length; i++) {
result ^= arr[i]; // XOR each element in array
}
return result;
@unilecs
unilecs / compressString.js
Created October 8, 2017 17:53
Compression of string
function compressString(inputStr) {
if (inputStr) {
// частный случай: если строка состоит из 1го символа
// или сжатие приведет к увеличению строки
let length = inputStr.length, inputStrArr = [...inputStr];
let compressStrLength = getCompressionStringLength(inputStrArr);
if (length === 1 || compressStrLength >= length) {
return inputStr;
}
@unilecs
unilecs / containsDividers.js
Last active April 10, 2019 06:12
Contains dividers in the array of number
function containsDividers(intArray, X) {
// HashMap: число -> кол-во вхождений в массив
let hashMapElements = {};
for(let item of intArray) {
if (hashMapElements[item]) {
++hashMapElements[item];
} else {
hashMapElements[item] = 1;
}
@unilecs
unilecs / getFirstUniqueSymbol.js
Created October 8, 2017 19:32
Первый уникальный символ в строке (1 способ)
function getFirstUniqueSymbol(inputStr) {
if (inputStr) {
const length = inputStr.length;
// частный случай: строка из 1го символа
if (length === 1) {
return inputStr;
}
// записываем в массив кол-во каждого символа: индекс элемента = код символа в строке