diff --git a/lab4/1.js b/lab4/1.js index c7c43cf..b2db97c 100644 --- a/lab4/1.js +++ b/lab4/1.js @@ -6,7 +6,13 @@ * ['мир', 'Рим', 'сирота', 'Ариост', 'мри', 'пва', 'лор', 'авп']; -> [["мир", "Рим", "мри"], ["сирота", "Ариост"], ["пва", "авп"]] */ function getAnagramms(arr) { - //code here + let groups = {}; + arr.map( function(word) { + let key = word.toLowerCase().split('').sort(); + groups[key] ? groups[key].push(word) : groups[key] = [word]; + } ); + return Object.values(groups); } module.exports = getAnagramms; +console.log(getAnagramms(['мир', 'Рим', 'сирота', 'Ариост', 'мри', 'пва', 'лор', 'авп'])); diff --git a/lab4/2.js b/lab4/2.js index 9c1abc1..0b9937d 100644 --- a/lab4/2.js +++ b/lab4/2.js @@ -7,7 +7,7 @@ * "тот" -> true */ function isPalindrome(str) { - //code here + return str.split('').join() === str.split('').reverse().join(); } module.exports = isPalindrome; diff --git a/lab4/3.js b/lab4/3.js index a65bef0..c9bcacf 100644 --- a/lab4/3.js +++ b/lab4/3.js @@ -8,7 +8,17 @@ */ function rle(str) { - //code here + return str.split('').reduce( function(result, value, index, arr) { + if (result[result.length - 2] === value) { + result[result.length - 1] = result[result.length - 1] + 1; + return result; + } + if (result[result.length - 1] < 2) result.pop(); + result.push(value); + if (index != arr.length - 1) result.push(1); + return result; + }, []).join(''); } module.exports = rle; +console.log(rle('BCCADDEEEBB')); \ No newline at end of file diff --git a/lab4/4.js b/lab4/4.js index 7d6162e..06361bd 100644 --- a/lab4/4.js +++ b/lab4/4.js @@ -17,7 +17,18 @@ */ function get1DArray(arr) { - //code here + return arr.flat(Infinity); } module.exports = get1DArray; +console.log(get1DArray([1, 2, 'aa', [1, 2, 3], +[ + [1, 2], + [1, 2] +], +[ + [ + [1, 2, [1, 2, [2]]], 3 + ], 4 +] +])); \ No newline at end of file diff --git a/lab4/5.js b/lab4/5.js index 4760146..8dd4419 100644 --- a/lab4/5.js +++ b/lab4/5.js @@ -11,7 +11,18 @@ */ function checkBrackets(str) { - //code here + if (str.length % 2) return false; + let queue = []; + for (elem of str) { + if ('[(<'.includes(elem)) { + queue.push(elem === '[' ? ']' : elem === '(' ? ')' : '>'); + continue; + } + elem === queue[queue.length - 1] ? queue.pop() : elem = ''; + if (!elem) return false; + } + return true; } module.exports = checkBrackets; +console.log(checkBrackets('[(()[])<(<>)>]')); diff --git a/tests/lab4.test.js b/tests/lab4.test.js index 439a487..236f1a1 100644 --- a/tests/lab4.test.js +++ b/tests/lab4.test.js @@ -71,18 +71,18 @@ test("get1DArray 2", () => { expect(get1DArray([])).toEqual([]); }); -// test('checkBrackets 1', () => { -// expect(checkBrackets('[(<>)]')).toBe(true); -// }); -// test('checkBrackets 2', () => { -// expect(checkBrackets('([)]()<>')).toBe(false); -// }); -// test('checkBrackets 3', () => { -// expect(checkBrackets('[[()]]([])<>')).toBe(true); -// }); -// test('checkBrackets 4', () => { -// expect(checkBrackets('[]')).toBe(true); -// }); -// test('checkBrackets 5', () => { -// expect(checkBrackets('')).toBe(true); -// }); +test('checkBrackets 1', () => { + expect(checkBrackets('[(<>)]')).toBe(true); +}); +test('checkBrackets 2', () => { + expect(checkBrackets('([)]()<>')).toBe(false); +}); +test('checkBrackets 3', () => { + expect(checkBrackets('[[()]]([])<>')).toBe(true); +}); +test('checkBrackets 4', () => { + expect(checkBrackets('[]')).toBe(true); +}); +test('checkBrackets 5', () => { + expect(checkBrackets('')).toBe(true); +});