Skip to content

Commit

Permalink
laba 4 yeeeeah lmao
Browse files Browse the repository at this point in the history
  • Loading branch information
pahan committed Mar 20, 2022
1 parent 0b9036e commit 64792a8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
8 changes: 7 additions & 1 deletion lab4/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(['мир', 'Рим', 'сирота', 'Ариост', 'мри', 'пва', 'лор', 'авп']));
2 changes: 1 addition & 1 deletion lab4/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* "тот" -> true
*/
function isPalindrome(str) {
//code here
return str.split('').join() === str.split('').reverse().join();
}

module.exports = isPalindrome;
12 changes: 11 additions & 1 deletion lab4/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
13 changes: 12 additions & 1 deletion lab4/4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]));
13 changes: 12 additions & 1 deletion lab4/5.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('[(()[])<(<>)>]'));
30 changes: 15 additions & 15 deletions tests/lab4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 64792a8

Please sign in to comment.