diff --git a/codewars/Adding Big Numbers/Adding Big Numbers.js b/codewars/Adding Big Numbers/Adding Big Numbers.js new file mode 100644 index 000000000..5cac82529 --- /dev/null +++ b/codewars/Adding Big Numbers/Adding Big Numbers.js @@ -0,0 +1,19 @@ +function add(num1, num2) { + let result = ''; + let carry = 0; + let i = num1.length - 1; + let j = num2.length - 1; + while (i >= 0 || j >= 0 || carry > 0) { + const digit1 = i >= 0 ? parseInt(num1[i]) : 0; + const digit2 = j >= 0 ? parseInt(num2[j]) : 0; + + const sum = digit1 + digit2 + carry; + result = (sum % 10) + result; + carry = Math.floor(sum / 10); + + i--; + j--; + } + + return result; +} \ No newline at end of file diff --git a/codewars/Anagram difference/Anagram defferende.js b/codewars/Anagram difference/Anagram defferende.js new file mode 100644 index 000000000..9acb59fb8 --- /dev/null +++ b/codewars/Anagram difference/Anagram defferende.js @@ -0,0 +1,28 @@ +function anagramDifference(w1, w2) { + const letters1 = {}; + const letters2 = {}; + + for (let i = 0; i < w1.length; i++) { + const letter = w1[i]; + letters1[letter] = (letters1[letter] || 0) + 1; + } + + for (let i = 0; i < w2.length; i++) { + const letter = w2[i]; + letters2[letter] = (letters2[letter] || 0) + 1; + } + + let difference = 0; + for (const letter in letters1) { + difference += Math.abs((letters1[letter] || 0) - (letters2[letter] || 0)); + } + for (const letter in letters2) { + if (!(letter in letters1)) { + difference += letters2[letter]; + } + } + + return difference; + } + + \ No newline at end of file diff --git a/codewars/Array Deep Count/Array Deep Count.js b/codewars/Array Deep Count/Array Deep Count.js new file mode 100644 index 000000000..5f70ed309 --- /dev/null +++ b/codewars/Array Deep Count/Array Deep Count.js @@ -0,0 +1,16 @@ +const chai = require("chai"); +const assert = chai.assert; +chai.config.truncateThreshold = 0; + +function deepCount(arr) { + let count = 0; + + for (const element of arr) { + count++; + if (Array.isArray(element)) { + count += deepCount(element); + } + } + + return count; +} diff --git a/codewars/Build Tower/Build Tower.js b/codewars/Build Tower/Build Tower.js new file mode 100644 index 000000000..9508cbfc0 --- /dev/null +++ b/codewars/Build Tower/Build Tower.js @@ -0,0 +1,19 @@ +function towerBuilder(nFloors) { + const tower = []; + for (let i = 0; i < nFloors; i++) { + const stars = (i * 2) + 1; // Количество звездочек на каждой строке + const spaces = nFloors - i - 1; // Количество пробелов по краям + let row = ""; + for (let j = 0; j < spaces; j++) { + row += " "; + } + for (let j = 0; j < stars; j++) { + row += "*"; + } + for (let j = 0; j < spaces; j++) { + row += " "; + } + tower.push(row); + } + return tower; + } \ No newline at end of file diff --git a/codewars/Convert string to camel case/Convert string to camel case.js b/codewars/Convert string to camel case/Convert string to camel case.js new file mode 100644 index 000000000..8e82f42a3 --- /dev/null +++ b/codewars/Convert string to camel case/Convert string to camel case.js @@ -0,0 +1,16 @@ +function toCamelCase(str) { + if (!str) { + return ""; + } + + const words = str.split(/[-_]/); + const camelCaseWords = words.map((word, index) => { + if (index === 0) { + return word; + } else { + return word[0].toUpperCase() + word.slice(1).toLowerCase(); + } + }); + + return camelCaseWords.join(""); + } \ No newline at end of file diff --git a/codewars/Duplicate Encoder/Duplicate Encoder.js b/codewars/Duplicate Encoder/Duplicate Encoder.js new file mode 100644 index 000000000..e41718174 --- /dev/null +++ b/codewars/Duplicate Encoder/Duplicate Encoder.js @@ -0,0 +1,16 @@ +function duplicateEncode(word) { + const lowerWord = word.toLowerCase(); + const charCount = {}; + let result = ''; + + for (const char of lowerWord) { + charCount[char] = (charCount[char] || 0) + 1; + } + + for (const char of lowerWord) { + result += charCount[char] === 1 ? '(' : ')'; + + + } + +} \ No newline at end of file diff --git a/codewars/Find the missing letter/Find the missing letter.js b/codewars/Find the missing letter/Find the missing letter.js new file mode 100644 index 000000000..308e8f784 --- /dev/null +++ b/codewars/Find the missing letter/Find the missing letter.js @@ -0,0 +1,27 @@ +function findMissingLetter(array) { + 2 + let startLetter = array[0].charCodeAt(0); + 3 + let missingLetter = ""; + 4 + ​ + 5 + for (let i = 1; i < array.length; i++) { + 6 + let currentLetter = array[i].charCodeAt(0); + 7 + if (currentLetter - startLetter !== i) { + 8 + missingLetter = String.fromCharCode(startLetter + i); + 9 + break; + 10 + } + 11 + } + 12 + ​ + 13 + return missingLetter; + 14 + } \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/Flatten a Nested Map.js b/codewars/Flatten a Nested Map/Flatten a Nested Map.js new file mode 100644 index 000000000..e5dd05814 --- /dev/null +++ b/codewars/Flatten a Nested Map/Flatten a Nested Map.js @@ -0,0 +1,14 @@ +function flattenMap(obj, parentKey = '', result = {}) { + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + const newKey = parentKey ? `${parentKey}/${key}` : key; + const value = obj[key]; + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + flattenMap(value, newKey, result); + } else { + result[newKey] = value; + } + } + } + return result; +} diff --git a/codewars/Fun with tree - max sum/Fun with tree - max sum.js b/codewars/Fun with tree - max sum/Fun with tree - max sum.js new file mode 100644 index 000000000..e69de29bb diff --git a/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js b/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js new file mode 100644 index 000000000..effb19442 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js @@ -0,0 +1,26 @@ +function Node(data) { + this.data = data; + this.next = null; + } + + function sortedInsert(head, data) { + const newNode = new Node(data); + + if (head === null || data < head.data) { + newNode.next = head; + return newNode; + } + + let current = head; + let previous = null; + + while (current !== null && data >= current.data) { + previous = current; + current = current.next; + } + + newNode.next = previous.next; + previous.next = newNode; + + return head; + } \ No newline at end of file diff --git a/codewars/Merge two arrays/Merge two arrays.js b/codewars/Merge two arrays/Merge two arrays.js new file mode 100644 index 000000000..91c709ced --- /dev/null +++ b/codewars/Merge two arrays/Merge two arrays.js @@ -0,0 +1,18 @@ +function mergeArrays(chars, nums) { + let merged = []; + let i = 0; + let j = 0; + + while (i < chars.length || j < nums.length) { + if (i < chars.length) { + merged.push(chars[i]); + i++; + } + if (j < nums.length) { + merged.push(nums[j]); + j++; + } + } + + return merged; + } \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/Moving Zeros To The End.js b/codewars/Moving Zeros To The End/Moving Zeros To The End.js new file mode 100644 index 000000000..e27a0e528 --- /dev/null +++ b/codewars/Moving Zeros To The End/Moving Zeros To The End.js @@ -0,0 +1,18 @@ +function moveZeros(arr) { + const result = []; + let zeroCount = 0; + + for (const element of arr) { + if (element === 0) { + zeroCount++; + } else { + result.push(element); + } + } + + for (let i = 0; i < zeroCount; i++) { + result.push(0); + } + + return result; +} \ No newline at end of file diff --git a/codewars/Permutations/Permutations.js b/codewars/Permutations/Permutations.js new file mode 100644 index 000000000..38472770b --- /dev/null +++ b/codewars/Permutations/Permutations.js @@ -0,0 +1,19 @@ +function permutations(string) { + const result = new Set(); + + function backtrack(path, remaining) { + if (remaining.length === 0) { + result.add(path); + return; + } + for (let i = 0; i < remaining.length; i++) { + const nextPath = path + remaining[i]; + const nextRemaining = remaining.slice(0, i) + remaining.slice(i + 1); + backtrack(nextPath, nextRemaining); + } + } + + backtrack('', string); + return Array.from(result); + +} diff --git a/codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js b/codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js new file mode 100644 index 000000000..b5ddca63e --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js @@ -0,0 +1,10 @@ +function productFib(prod) { + let a = 0; + let b = 1; + + while (a * b < prod) { + [a, b] = [b, a + b]; + } + + return [a, b, a * b === prod]; + } \ No newline at end of file diff --git a/codewars/Simple Pig Latin/Simple Pig Latin.js b/codewars/Simple Pig Latin/Simple Pig Latin.js new file mode 100644 index 000000000..3a6afa1aa --- /dev/null +++ b/codewars/Simple Pig Latin/Simple Pig Latin.js @@ -0,0 +1,12 @@ +function pigIt(str) { + return str + .split(' ') + .map((word) => { + if (word.match(/^[a-zA-Z]+$/)) { + return word.slice(1) + word[0] + 'ay'; + } else { + return word; + } + }) + .join(' '); + } \ No newline at end of file diff --git a/codewars/Snail/Snail.js b/codewars/Snail/Snail.js new file mode 100644 index 000000000..bdc50fd66 --- /dev/null +++ b/codewars/Snail/Snail.js @@ -0,0 +1,43 @@ +snail = function(array) { + if (array.length === 0 || array[0].length === 0) { + return []; + } + + const result = []; + let top = 0; + let bottom = array.length - 1; + let left = 0; + let right = array[0].length - 1; + + while (top <= bottom && left <= right) { + + for (let i = left; i <= right; i++) { + result.push(array[top][i]); + } + top++; + + + for (let i = top; i <= bottom; i++) { + result.push(array[i][right]); + } + right--; + + if (top <= bottom) { + + for (let i = right; i >= left; i--) { + result.push(array[bottom][i]); + } + bottom--; + } + + if (left <= right) { + + for (let i = bottom; i >= top; i--) { + result.push(array[i][left]); + } + left++; + } + } + + return result; +}; \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/Sum of Digits - Digital Root.js b/codewars/Sum of Digits - Digital Root/Sum of Digits - Digital Root.js new file mode 100644 index 000000000..5419f64d5 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/Sum of Digits - Digital Root.js @@ -0,0 +1,13 @@ +function digitalRoot(n) { + if (n < 10) { + return n; + } + + let sum = 0; + while (n > 0) { + sum += n % 10; + n = Math.floor(n / 10); + } + + return digitalRoot(sum); + } \ No newline at end of file diff --git a/codewars/Sum of Intervals/Sum of Intervals.js b/codewars/Sum of Intervals/Sum of Intervals.js new file mode 100644 index 000000000..e69de29bb diff --git a/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Cheacker.js b/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Cheacker.js new file mode 100644 index 000000000..37994ce12 --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Cheacker.js @@ -0,0 +1,34 @@ +function isSolved(board) { + function checkWinner(player) { + for (let i = 0; i < 3; i++) { + if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { + return true; + } + } + for (let i = 0; i < 3; i++) { + if (board[0][i] === player && board[1][i] === player && board[2][i] === player) { + return true; + } + } + if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { + return true; + } + if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { + return true; + } + return false; + } + + if (checkWinner(1)) return 1; // Игрок 1 победил + if (checkWinner(2)) return 2; // Игрок 2 победил + + for (let row of board) { + for (let cell of row) { + if (cell === 0) { + return -1; // Игра продолжается + } + } + } + + return 0; // Ничья + } \ No newline at end of file diff --git a/codewars/Valid Parentheses/Valid Parentheses.js b/codewars/Valid Parentheses/Valid Parentheses.js new file mode 100644 index 000000000..551dbf599 --- /dev/null +++ b/codewars/Valid Parentheses/Valid Parentheses.js @@ -0,0 +1,17 @@ +function validParentheses(s) { + let balance = 0; + + for (let char of s) { + if (char === '(') { + balance++; + } else if (char === ')') { + balance--; + } + + if (balance < 0) { + return false; + } + } + + return balance === 0; +} diff --git a/codewars/Where my anagrams at/Where my anagrams at.js b/codewars/Where my anagrams at/Where my anagrams at.js new file mode 100644 index 000000000..c8f65b8b1 --- /dev/null +++ b/codewars/Where my anagrams at/Where my anagrams at.js @@ -0,0 +1,4 @@ +function anagrams(word, words) { + const sortedWord = word.split('').sort().join(''); + return words.filter(w => w.split('').sort().join('') === sortedWord); + } \ No newline at end of file diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7bc4a71de..2fcf9bea3 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1 +1,37 @@ -console.log('Hello world'); +import {task19A, task19B} from './math'; +import { Mouse } from './src/labclass2'; + + +const a = 2.0; +const x1 = 1.16; +const x2 = 1.32; +const x3 = 1.47; +const x4 = 1.65; +const x5 = 1.93; +let xn = 1.2; +let xk = 4.2; +let dx = 0.6; +const xs: number[] = [x1, x2, x3, x4, x5]; + +const cat1 = new Mouse('Roly', 5, 'Satin mice','Grey'); +console.log(cat1.getInfo); +const cat2 = new Kitten('Mickey', 2, 'Yellow-throated mice','Yellow'); +console.log(Cat's name: ${cat3.name}); +console.log(cat3.changeCatName('Tyutya')); + + + + + + + + + + + + + + + + + diff --git a/rpgsaga/saga/src/labclass2.ts b/rpgsaga/saga/src/labclass2.ts new file mode 100644 index 000000000..9a3742de2 --- /dev/null +++ b/rpgsaga/saga/src/labclass2.ts @@ -0,0 +1,68 @@ +export class Mouse { + name: string; + age: number; + breed: string; + color: string; + + constructor(mousename: string, mouseage: number, mousebreed: string, mousecolor: string) { + this.name = mousename; + this.age = mouseage; + this.breed = mousebreed; + this.color = mousecolor; + } + + get getName(): string { + return this.name; + } + + set setName(valuename: string) { + this.name = valuename; + } + + get getAge(): number { + return this.age; + } + + set setAge(valueage: number) { + if (valueage < 0 || valueage > 3) { + console.error("Mice can't live that long"); + } else { + this.age = valueage; + } + } + + get getBreed(): string { + return this.breed; + } + + set setBreed(valuebreed: string) { + this.breed = valuebreed; + } + + get getColor(): string { + return this.color; + } + + set setColor(valuecolor: string) { + this.color = valuecolor; + } + + + get getInfo(): string { + return `Mouse's name: ${this.name}, mouse's age: ${this.age}, mouse's breed: ${this.breed}, mouse's color: ${this.color}`; + } + + changeMouseName(newname: string): void { + this.name = newname; + } + + whereFrom(): string { + if (this.name === 'Mickey') { + return "Disneyland"; + } else if (this.name === "Jerry") { + return "Cartoon"; + } else { + return "sorryyy("; + } + } +} diff --git a/rpgsaga/saga/src/math.ts b/rpgsaga/saga/src/math.ts new file mode 100644 index 000000000..9c589dae8 --- /dev/null +++ b/rpgsaga/saga/src/math.ts @@ -0,0 +1,19 @@ +declare interface Math { + log10(x: number): number; +} + +function calculate19(a: number, x: number): number { +return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); +} + +function task15A(a: number, xn: number, xk: number, dx: number): number[] { +const results: number[] = []; +for (let x = xn; x <= xk; x += dx) { + results.push(calculate19(a, x)); +} +return results; +} + +function task15B(a: number, xs: number[]): number[] { +return xs.map(x => calculate19(a, x)); +} diff --git a/rpgsaga/saga/src/tests.ts b/rpgsaga/saga/src/tests.ts new file mode 100644 index 000000000..8f5a6e0bd --- /dev/null +++ b/rpgsaga/saga/src/tests.ts @@ -0,0 +1,36 @@ +import {task19A, task19B} from '../src/math'; + + +describe('calculate19', () => { + it('...', () => { + expect(calculate19(2, 1)).toBeCloseTo(0.0144, 4); + }); + }); + + describe('task19A', () => { + it('...', () => { + const a = 2.0; + const xn = 1.2; + const xk = 4.2; + const dx = 0.6; + const expectedResults = [0.0249, 0.0233, 0.0214, 0.0195, 0.0179, 0.0163]; + const results = task19A(a, xn, xk, dx); + expect(results).toEqual(expectedResults); + }); + }); + + describe('task19B', () => { + it('...', () => { + const a = 2.0; + const x1 = 1.16; + const x2 = 1.32; + const x3 = 1.47; + const x4 = 1.65; + const x5 = 1.93; + const xs = [x1, x2, x3, x4, x5]; + const expectedResults = [0.0250, 0.0246, 0.0242, 0.0237, 0.0229]; + const results = task19B(a, xs); + expect(results).toEqual(expectedResults); + }); + }); + diff --git a/rpgsaga/saga/tests/lab2tests.ts b/rpgsaga/saga/tests/lab2tests.ts new file mode 100644 index 000000000..6c604ed1c --- /dev/null +++ b/rpgsaga/saga/tests/lab2tests.ts @@ -0,0 +1,69 @@ +import { Mouse } from '../src'; + +describe('Mouse', () => { + it('should correctly initialize properties in the constructor', () => { + const Mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + expect(mouse.getName).toBe('Jerry'); + expect(mouse.getAge).toBe(1); + expect(mouse.getBreed).toBe('White Mouse'); + expect(mouse.getColor).toBe('White'); + }); + + it('Должен корректно устанавливать и получать имя мыши', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + mouse.setName = 'Tom'; + expect(mouse.getName).toBe('Tom'); + }); + + it('Должен вывести ошибку, если указан неверный возраст', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + console.error = jest.fn(); + mouse.setAge = 10; + expect(console.error).toHaveBeenCalledWith("Mice can't live that long"); + expect(mouse.getAge).toBe(1); + + mouse.setAge = -1; + expect(console.error).toHaveBeenCalledWith("Mice can't live that long"); + expect(mouse.getAge).toBe(1); + }); + + it('Должен корректно устанавливать и получать возраст мыши', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + mouse.setAge = 2; + expect(mouse.getAge).toBe(2); + }); + + + it('Должен корректно устанавливать и получать породу мыши', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + mouse.setBreed = 'Black Mouse'; + expect(mouse.getBreed).toBe('Black Mouse'); + }); + + it('Должен корректно устанавливать и получать цвет мыши', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + mouse.setColor = 'Gray'; + expect(mouse.getColor).toBe('Gray'); + }); + + it('Должен получать полную информацию о мыши', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + expect(mouse.getInfo).toBe("Мышонка зовут Джерри, возраст мышонка: 1 год, порода мышонка: Белый мышонок, окрас мышонка: Белый"); + }); + + it('Должен корректно изменять имя мыши', () => { + const mouse = new Mouse('Jerry', 1, 'White Mouse', 'White'); + mouse.changeMouseName('Tom'); + expect(mouse.getName).toBe('Tom'); + }); + + it('Должен корректно определять происхождение мыши', () => { + const mouse1 = new Mouse('Mickey', 2, 'White Mouse', 'White'); + const mouse2 = new Mouse('Jerry', 1, 'White Mouse', 'White'); + const mouse3 = new Mouse('Minnie', 6, 'White Mouse', 'White'); + + expect(mouse1.whereFrom()).toBe("Disneyland"); + expect(mouse2.whereFrom()).toBe("Cartoon"); + expect(mouse3.whereFrom()).toBe("sorryyy("); + }); +}); diff --git a/rpgsaga/saga/tests/testslab1.ts b/rpgsaga/saga/tests/testslab1.ts new file mode 100644 index 000000000..8f5a6e0bd --- /dev/null +++ b/rpgsaga/saga/tests/testslab1.ts @@ -0,0 +1,36 @@ +import {task19A, task19B} from '../src/math'; + + +describe('calculate19', () => { + it('...', () => { + expect(calculate19(2, 1)).toBeCloseTo(0.0144, 4); + }); + }); + + describe('task19A', () => { + it('...', () => { + const a = 2.0; + const xn = 1.2; + const xk = 4.2; + const dx = 0.6; + const expectedResults = [0.0249, 0.0233, 0.0214, 0.0195, 0.0179, 0.0163]; + const results = task19A(a, xn, xk, dx); + expect(results).toEqual(expectedResults); + }); + }); + + describe('task19B', () => { + it('...', () => { + const a = 2.0; + const x1 = 1.16; + const x2 = 1.32; + const x3 = 1.47; + const x4 = 1.65; + const x5 = 1.93; + const xs = [x1, x2, x3, x4, x5]; + const expectedResults = [0.0250, 0.0246, 0.0242, 0.0237, 0.0229]; + const results = task19B(a, xs); + expect(results).toEqual(expectedResults); + }); + }); +