diff --git a/codewars/Adding Big Numbers/add.js b/codewars/Adding Big Numbers/add.js new file mode 100644 index 000000000..76db4d9d2 --- /dev/null +++ b/codewars/Adding Big Numbers/add.js @@ -0,0 +1,20 @@ +function add(a, b) { + let result = ''; + let carry = 0; + let maxLength = Math.max(a.length, b.length); + + a = a.padStart(maxLength, '0'); + b = b.padStart(maxLength, '0'); + + for (let i = maxLength - 1; i >= 0; i--) { + let sum = parseInt(a[i]) + parseInt(b[i]) + carry; + carry = Math.floor(sum / 10); + result = (sum % 10) + result; + } + + if (carry) { + result = carry + result; + } + + return result; + } \ No newline at end of file diff --git a/codewars/Anagram difference/anagramDifference.js b/codewars/Anagram difference/anagramDifference.js new file mode 100644 index 000000000..a6e355a82 --- /dev/null +++ b/codewars/Anagram difference/anagramDifference.js @@ -0,0 +1,25 @@ +function anagramDifference(w1, w2) { + const letterCounts1 = {}; + const letterCounts2 = {}; + + for (const letter of w1) { + letterCounts1[letter] = (letterCounts1[letter] || 0) + 1; + } + + for (const letter of w2) { + letterCounts2[letter] = (letterCounts2[letter] || 0) + 1; + } + + let difference = 0; + for (const letter in letterCounts1) { + difference += Math.abs(letterCounts1[letter] - (letterCounts2[letter] || 0)); + } + + for (const letter in letterCounts2) { + if (!letterCounts1[letter]) { + difference += letterCounts2[letter]; + } + } + + return difference; + } \ No newline at end of file diff --git a/codewars/Array Deep Count/deepCount.js b/codewars/Array Deep Count/deepCount.js new file mode 100644 index 000000000..7d3bef60a --- /dev/null +++ b/codewars/Array Deep Count/deepCount.js @@ -0,0 +1,13 @@ +function deepCount(array) { + let k = 0; + const countElements = arr => { + k += arr.length; + for (let i of arr) { + if (Array.isArray(i) ) { + countElements(i); + } + } + } + countElements(array); + return k; + } \ No newline at end of file diff --git a/codewars/Build Tower/towerBuilder.js b/codewars/Build Tower/towerBuilder.js new file mode 100644 index 000000000..496fe6b55 --- /dev/null +++ b/codewars/Build Tower/towerBuilder.js @@ -0,0 +1,9 @@ +function towerBuilder(nFloors) { + const tower = []; + for (let i = 1; i <= nFloors; i++) { + const spaces = " ".repeat(nFloors - i); + const stars = "*".repeat(i * 2 - 1); + tower.push(spaces + stars + spaces); + } + return tower; + } \ No newline at end of file diff --git a/codewars/Convert string to camel case/toCamelCase.js b/codewars/Convert string to camel case/toCamelCase.js new file mode 100644 index 000000000..e7e9bf5c1 --- /dev/null +++ b/codewars/Convert string to camel case/toCamelCase.js @@ -0,0 +1,10 @@ +function toCamelCase(str) { + a = str.replaceAll('_', '-').split('-'); + res = [] + res.push(a[0]); + for(i = 1;i < a.length;i++){ + res.push(a[i][0].toUpperCase()) + res.push(a[i].slice(1)) + } + return res.join("") + } \ No newline at end of file diff --git a/codewars/Duplicate Encoder/duplicateEncode.js b/codewars/Duplicate Encoder/duplicateEncode.js new file mode 100644 index 000000000..90cf416ee --- /dev/null +++ b/codewars/Duplicate Encoder/duplicateEncode.js @@ -0,0 +1,12 @@ +function duplicateEncode(word) { + word = word.toLowerCase(); + let result = ''; + for (let i = 0; i < word.length; i++) { + if (word.lastIndexOf(word[i]) === word.indexOf(word[i])) { + result += '('; + } else { + result += ')'; + } + } + return result; + } \ No newline at end of file diff --git a/codewars/Find the missing letter/findMissingLetter.js b/codewars/Find the missing letter/findMissingLetter.js new file mode 100644 index 000000000..bf30e57b1 --- /dev/null +++ b/codewars/Find the missing letter/findMissingLetter.js @@ -0,0 +1,8 @@ +function findMissingLetter(array) { + var string = array.join(""); + for (var i = 0; i < string.length; i++) { + if (string.charCodeAt(i + 1) - string.charCodeAt(i) !== 1) { + return String.fromCharCode(string.charCodeAt(i) + 1); + } + } + } \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/flattenMap.js b/codewars/Flatten a Nested Map/flattenMap.js new file mode 100644 index 000000000..b2867f873 --- /dev/null +++ b/codewars/Flatten a Nested Map/flattenMap.js @@ -0,0 +1,23 @@ +function flattenMap(input) { + const result = {}; + + function flatten(current, prefix) { + for (const [key, value] of Object.entries(current)) { + const newKey = prefix ? `${prefix}/${key}` : key; + + if ( + value !== null && + typeof value === "object" && + !Array.isArray(value) && + typeof value !== "function" + ) { + flatten(value, newKey); + } else { + result[newKey] = value; + } + } + } + flatten(input, ""); + + return result; + } \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/maxSum.js b/codewars/Fun with tree - max sum/maxSum.js new file mode 100644 index 000000000..c5d3937e2 --- /dev/null +++ b/codewars/Fun with tree - max sum/maxSum.js @@ -0,0 +1,13 @@ +function maxSum(root) { + if (!root) return 0; + + if(!root.left && !root.right) return root.value; + + let leftMax = -Infinity; + let rightMax = -Infinity; + + if (root.left) leftMax = maxSum(root.left); + if (root.right) rightMax = maxSum(root.right); + + return root.value + Math.max(leftMax, rightMax); +} \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/sortedInsert.js b/codewars/Linked Lists - Sorted Insert/sortedInsert.js new file mode 100644 index 000000000..b0b9b2eb5 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/sortedInsert.js @@ -0,0 +1,23 @@ +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; + while (current.next !== null && data > current.next.data) { + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + + return head; + } \ No newline at end of file diff --git a/codewars/Merge two arrays/mergeArrays.js b/codewars/Merge two arrays/mergeArrays.js new file mode 100644 index 000000000..065f3cb6f --- /dev/null +++ b/codewars/Merge two arrays/mergeArrays.js @@ -0,0 +1,17 @@ +function mergeArrays(a, b) { + let mergedArray = []; + let i = 0; + let j = 0; + + while (i < a.length || j < b.length) { + if (i < a.length) { + mergedArray.push(a[i]); + i += 1; + } + if (j < b.length) { + mergedArray.push(b[j]); + j += 1; + } + } + return mergedArray; + } \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/moveZeros.js b/codewars/Moving Zeros To The End/moveZeros.js new file mode 100644 index 000000000..64069ba1b --- /dev/null +++ b/codewars/Moving Zeros To The End/moveZeros.js @@ -0,0 +1,13 @@ +function moveZeros(arr) { + let nonZeroIndex = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== 0) { + arr[nonZeroIndex] = arr[i]; + nonZeroIndex++; + } + } + for (let i = nonZeroIndex; i < arr.length; i++) { + arr[i] = 0; + } + return arr; + } \ No newline at end of file diff --git a/codewars/Permutations/permutations.js b/codewars/Permutations/permutations.js new file mode 100644 index 000000000..0fdb0b2f9 --- /dev/null +++ b/codewars/Permutations/permutations.js @@ -0,0 +1,18 @@ +function permutations(string) { + if (string.length === 1) { + return [string]; + } + + const result = []; + for (let i = 0; i < string.length; i++) { + const char = string[i]; + const remainingChars = string.slice(0, i) + string.slice(i + 1); + const subPermutations = permutations(remainingChars); + + for (const subPermutation of subPermutations) { + result.push(char + subPermutation); + } + } + + return [...new Set(result)]; + } \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/productFib.js b/codewars/Product of consecutive Fib numbers/productFib.js new file mode 100644 index 000000000..64fdfadfe --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/productFib.js @@ -0,0 +1,17 @@ +function productFib(prod) { + let f0 = 0; + let f1 = 1; + let found = false; + + while (f0 * f1 <= prod) { + if (f0 * f1 === prod) { + found = true; + break; + } + let temp = f1; + f1 = f0 + f1; + f0 = temp; + } + + return [f0, f1, found]; + } \ No newline at end of file diff --git a/codewars/Simple Pig Latin/pigIt.js b/codewars/Simple Pig Latin/pigIt.js new file mode 100644 index 000000000..b39bdd0a7 --- /dev/null +++ b/codewars/Simple Pig Latin/pigIt.js @@ -0,0 +1,12 @@ +function pigIt(str) { + return str + .split(" ") + .map((word) => { + if (word.match(/[a-zA-Z]/)) { + return word.slice(1) + word.charAt(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..118cf5515 --- /dev/null +++ b/codewars/Snail/snail.js @@ -0,0 +1,37 @@ +function snail(array) { + if (array.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 && left <= right) { + for (let i = right; i >= left; i--) { + result.push(array[bottom][i]); + } + bottom--; + } + + if (top <= bottom && 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/digitalRoot.js b/codewars/Sum of Digits - Digital Root/digitalRoot.js new file mode 100644 index 000000000..53c101cb8 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/digitalRoot.js @@ -0,0 +1,19 @@ +function digitalRoot(n) { + let str = n.toString(); + + let sum = 0; + for (let i = 0; i < str.length; i++) { + sum += parseInt(str.charAt(i)); + } + + while (sum > 9) { + let temp = 0; + while (sum > 0) { + temp += sum % 10; + sum = Math.floor(sum / 10); + } + sum = temp; + } + + return sum; + } \ No newline at end of file diff --git a/codewars/Sum of Intervals/sumIntervals.js b/codewars/Sum of Intervals/sumIntervals.js new file mode 100644 index 000000000..172d73187 --- /dev/null +++ b/codewars/Sum of Intervals/sumIntervals.js @@ -0,0 +1,21 @@ +function sumIntervals(intervals) { + intervals.sort((a, b) => a[0] - b[0]); + let mergedIntervals = [intervals[0]]; + + for (let i = 1; i < intervals.length; i++) { + let lastInterval = mergedIntervals[mergedIntervals.length - 1]; + + if (intervals[i][0] <= lastInterval[1]) { + lastInterval[1] = Math.max(lastInterval[1], intervals[i][1]); + } else { + mergedIntervals.push(intervals[i]); + } + } + + let totalLength = 0; + for (let interval of mergedIntervals) { + totalLength += interval[1] - interval[0]; + } + + return totalLength; + } \ No newline at end of file diff --git a/codewars/Sum of pairs/sumPairs.js b/codewars/Sum of pairs/sumPairs.js new file mode 100644 index 000000000..9d878bd28 --- /dev/null +++ b/codewars/Sum of pairs/sumPairs.js @@ -0,0 +1,12 @@ +function sumPairs(ints, s) { + let seen = new Map(); + for (let i = 0; i < ints.length; i++) { + let num = ints[i]; + let complement = s - num; + if (seen.has(complement)) { + return [complement, num]; + } + seen.set(num, i); + } + return undefined; +} \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/isSolved.js b/codewars/Tic-Tac-Toe Checker/isSolved.js new file mode 100644 index 000000000..3ec1f238f --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/isSolved.js @@ -0,0 +1,35 @@ +function isSolved(board) { + for (let i = 0; i < 3; i++) { + if (board[i][0] !== 0 && board[i][0] === board[i][1] && board[i][0] === board[i][2]) { + return board[i][0]; + } + } + + for (let i = 0; i < 3; i++) { + if (board[0][i] !== 0 && board[0][i] === board[1][i] && board[0][i] === board[2][i]) { + return board[0][i]; + } + } + + if (board[0][0] !== 0 && board[0][0] === board[1][1] && board[0][0] === board[2][2]) { + return board[0][0]; + } + if (board[0][2] !== 0 && board[0][2] === board[1][1] && board[0][2] === board[2][0]) { + return board[0][2]; + } + + let draw = true; + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (board[i][j] === 0) { + draw = false; + break; + } + } + } + if (draw) { + return 0; + } + + return -1; + } \ No newline at end of file diff --git a/codewars/Valid Parentheses/validParentheses.js b/codewars/Valid Parentheses/validParentheses.js new file mode 100644 index 000000000..68318a698 --- /dev/null +++ b/codewars/Valid Parentheses/validParentheses.js @@ -0,0 +1,15 @@ +function validParentheses(parens) { + let stack = []; + + for (let i = 0; i < parens.length; i++) { + if (parens[i] === '(') { + stack.push(parens[i]); + } else if (parens[i] === ')') { + if (stack.length === 0 || stack.pop() !== '(') { + return false; + } + } + } + + return stack.length === 0; + } \ No newline at end of file diff --git a/codewars/Where my anagrams at/anagrams.js b/codewars/Where my anagrams at/anagrams.js new file mode 100644 index 000000000..68318a698 --- /dev/null +++ b/codewars/Where my anagrams at/anagrams.js @@ -0,0 +1,15 @@ +function validParentheses(parens) { + let stack = []; + + for (let i = 0; i < parens.length; i++) { + if (parens[i] === '(') { + stack.push(parens[i]); + } else if (parens[i] === ')') { + if (stack.length === 0 || stack.pop() !== '(') { + return false; + } + } + } + + return stack.length === 0; + } \ No newline at end of file diff --git a/rpgsaga/saga/package-lock.json b/rpgsaga/saga/package-lock.json index 5d3f4f98c..7ffb1ae17 100644 --- a/rpgsaga/saga/package-lock.json +++ b/rpgsaga/saga/package-lock.json @@ -10970,4 +10970,4 @@ "dev": true } } -} +} \ No newline at end of file diff --git a/rpgsaga/saga/package.json b/rpgsaga/saga/package.json index 48455ab0e..bd6d81b51 100644 --- a/rpgsaga/saga/package.json +++ b/rpgsaga/saga/package.json @@ -6,14 +6,13 @@ "scripts": { "dev": "ts-node --script-mode src/index.ts", "test": "jest", - "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"", - "lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix" + "lint": "eslint \"{src,apps,libs,test}//*.ts\"", + "lint:fix": "eslint \"{src,apps,libs,test}//*.ts\" --fix" }, "author": "", "license": "ISC", - "dependencies": {}, "devDependencies": { - "@eslint/compat": "^1.2.0", + "@eslint/compat": "^1.2.1", "@types/jest": "^29.5.13", "@types/node": "^22.7.4", "@typescript-eslint/eslint-plugin": "^8.8.0", @@ -31,4 +30,4 @@ "jest": { "preset": "ts-jest" } -} +} \ No newline at end of file diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7bc4a71de..35f7dba07 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1 +1,18 @@ -console.log('Hello world'); +import { task14A, task14B } from './lab1'; +import { Dish } from './lab2'; + +const a = 7.2; +const b = 4.2; +const xn = 1.81; +const xk = 5.31; +const dx = 0.7; +const xs: number[] = [2.4, 2.8, 3.9, 4.7, 3.16]; + +console.log(task14A(a, b, xn, xk, dx)); +console.log(task14B(a, b, xs)); + +const dish1 = new Dish("Pizza", 15.99, "Italian"); +const dish2 = new Dish("Apple pie", 8.99, "American"); + +console.log(dish1.getinfo); +console.log(dish2.getinfo); diff --git a/rpgsaga/saga/src/lab1.ts b/rpgsaga/saga/src/lab1.ts new file mode 100644 index 000000000..371cdedb7 --- /dev/null +++ b/rpgsaga/saga/src/lab1.ts @@ -0,0 +1,23 @@ +export function calculate14(a: number, b: number, x: number) { + const ans = Math.sqrt(Math.abs(a - b * x) / Math.pow(Math.log10(x), 3)); + return ans; +} + +export function task14A(a: number, b: number, xn: number, xk: number, dx: number) { + const res1: number[] = []; + for (let x = xn; x <= xk; x += dx) { + const y = calculate14(a, b, x); + res1.push(y); + } + return res1; +} + +export function task14B(a: number, b: number, xs: number[]) { + const res2: number[] = []; + let ans = 0; + for (const y of xs) { + ans = calculate14(a, b, y); + res2.push(ans); + } + return res2; +} diff --git a/rpgsaga/saga/src/lab2.ts b/rpgsaga/saga/src/lab2.ts new file mode 100644 index 000000000..357325440 --- /dev/null +++ b/rpgsaga/saga/src/lab2.ts @@ -0,0 +1,43 @@ +export class Dish { + name: string; + price: number; + type: string; + + constructor(dishname: string, dishprice: number, dishtype: string) { + this.name = dishname; + this.price = dishprice; + this.type = dishtype; + } + + get getname(): string { + return this.name; + } + + set setname(valuename: string) { + this.name = valuename; + } + + get getprice(): number { + return this.price; + } + + set setprice(valueprice: number) { + if (valueprice < 0) { + console.error('The price cannot be less than zero!'); + } else { + this.price = valueprice; + } + } + + get gettype(): string { + return this.type; + } + + set settype(valuetype: string) { + this.type = valuetype; + } + + get getinfo(): string { + return `Dish's name: ${this.name}, dish's price: ${this.price}, dish's type: ${this.type}`; + } +} diff --git a/rpgsaga/saga/tests/lab1.test.ts b/rpgsaga/saga/tests/lab1.test.ts new file mode 100644 index 000000000..861eead48 --- /dev/null +++ b/rpgsaga/saga/tests/lab1.test.ts @@ -0,0 +1,30 @@ +import {task14A, task14B} from '../src/lab1'; + + +describe("task14A", () => { + it("should return the correct array of calculated values for task A", () => { + const expectedResult = [ + 4.847251589340839, + 7.235104883316567, + 6.953017445001333, + 6.664008196720523, + 6.4497646073821775 + ]; + const result = task14A(7.2, 4.2, 1.81, 5.31, 0.7); + expect(result).toEqual(expectedResult); + }); + }); + + describe("task14B", () => { + it("should return the correct array of calculated values for task B", () => { + const expectedResult = [ + 7.238670589387374, + 7.141523475559664, + 6.667590340861805, + 6.426877193493389, + 6.976196379251087 + ]; + const result = task14B(7.2, 4.2, [2.4, 2.8, 3.9, 4.7, 3.16]); + expect(result).toEqual(expectedResult); + }); + }); diff --git a/rpgsaga/saga/tests/lab2.test.ts b/rpgsaga/saga/tests/lab2.test.ts new file mode 100644 index 000000000..339a267dc --- /dev/null +++ b/rpgsaga/saga/tests/lab2.test.ts @@ -0,0 +1,45 @@ +import { Dish } from "../src/lab2"; + +describe("Dish", () => { + let dish: Dish; + + beforeEach(() => { + dish = new Dish("Pizza", 15.99, "Italian"); + }); + + it("should create a Dish instance with correct properties", () => { + expect(dish.name).toBe("Pizza"); + expect(dish.price).toBe(15.99); + expect(dish.type).toBe("Italian"); + }); + + it("should have getter and setter for name", () => { + expect(dish.getname).toBe("Pizza"); + + dish.setname = "Pasta"; + expect(dish.getname).toBe("Pasta"); + }); + + it("should have getter and setter for price", () => { + expect(dish.getprice).toBe(15.99); + + dish.setprice = 20.99; + expect(dish.getprice).toBe(20.99); + + console.error = jest.fn(); + dish.setprice = -5; + expect(console.error).toHaveBeenCalledWith("The price cannot be less than zero!"); + expect(dish.getprice).toBe(20.99); + }); + + it("should have getter and setter for type", () => { + expect(dish.gettype).toBe("Italian"); + + dish.settype = "American"; + expect(dish.gettype).toBe("American"); + }); + + it("should return correct info string", () => { + expect(dish.getinfo).toBe("Dish's name: Pizza, dish's price: 15.99, dish's type: Italian"); + }); +});