From ac6f5a40ee758d97a78cea1b0ba7ffd293062c6d Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Fri, 1 Nov 2024 13:16:35 +0300 Subject: [PATCH 1/7] codewars --- codewars/Adding Big Numbers/add.js | 20 ++++++++++ .../Anagram difference/anagramDifference.js | 25 +++++++++++++ codewars/Array Deep Count/deepCount.js | 13 +++++++ codewars/Build Tower/towerBuilder.js | 9 +++++ .../toCamelCase.js | 10 +++++ codewars/Duplicate Encoder/duplicateEncode.js | 12 ++++++ .../findMissingLetter.js | 8 ++++ codewars/Flatten a Nested Map/flattenMap.js | 23 ++++++++++++ codewars/Fun with tree - max sum/maxSum.js | 13 +++++++ .../sortedInsert.js | 23 ++++++++++++ codewars/Merge two arrays/mergeArrays.js | 17 +++++++++ codewars/Moving Zeros To The End/moveZeros.js | 13 +++++++ codewars/Permutations/permutations.js | 18 +++++++++ .../productFib.js | 17 +++++++++ codewars/Simple Pig Latin/pigIt.js | 12 ++++++ codewars/Snail/snail.js | 37 +++++++++++++++++++ .../digitalRoot.js | 19 ++++++++++ codewars/Sum of Intervals/sumIntervals.js | 21 +++++++++++ codewars/Sum of pairs/sumPairs.js | 12 ++++++ codewars/Tic-Tac-Toe Checker/isSolved.js | 35 ++++++++++++++++++ .../Valid Parentheses/validParentheses.js | 15 ++++++++ codewars/Where my anagrams at/anagrams.js | 15 ++++++++ 22 files changed, 387 insertions(+) create mode 100644 codewars/Adding Big Numbers/add.js create mode 100644 codewars/Anagram difference/anagramDifference.js create mode 100644 codewars/Array Deep Count/deepCount.js create mode 100644 codewars/Build Tower/towerBuilder.js create mode 100644 codewars/Convert string to camel case/toCamelCase.js create mode 100644 codewars/Duplicate Encoder/duplicateEncode.js create mode 100644 codewars/Find the missing letter/findMissingLetter.js create mode 100644 codewars/Flatten a Nested Map/flattenMap.js create mode 100644 codewars/Fun with tree - max sum/maxSum.js create mode 100644 codewars/Linked Lists - Sorted Insert/sortedInsert.js create mode 100644 codewars/Merge two arrays/mergeArrays.js create mode 100644 codewars/Moving Zeros To The End/moveZeros.js create mode 100644 codewars/Permutations/permutations.js create mode 100644 codewars/Product of consecutive Fib numbers/productFib.js create mode 100644 codewars/Simple Pig Latin/pigIt.js create mode 100644 codewars/Snail/snail.js create mode 100644 codewars/Sum of Digits - Digital Root/digitalRoot.js create mode 100644 codewars/Sum of Intervals/sumIntervals.js create mode 100644 codewars/Sum of pairs/sumPairs.js create mode 100644 codewars/Tic-Tac-Toe Checker/isSolved.js create mode 100644 codewars/Valid Parentheses/validParentheses.js create mode 100644 codewars/Where my anagrams at/anagrams.js 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 From fe3906452ce6cff5473f1bfca1ca38b238dd4c6e Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Sat, 2 Nov 2024 18:40:43 +0300 Subject: [PATCH 2/7] lab 1 + tests --- rpgsaga/saga/package-lock.json | 2 +- rpgsaga/saga/package.json | 34 ++++++++++++++++----------------- rpgsaga/saga/src/index.ts | 12 +++++++++++- rpgsaga/saga/src/lab1.ts | 29 ++++++++++++++++++++++++++++ rpgsaga/saga/tests/lab1.test.ts | 30 +++++++++++++++++++++++++++++ rpgsaga/saga/tsconfig.json | 8 ++++++-- 6 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 rpgsaga/saga/src/lab1.ts create mode 100644 rpgsaga/saga/tests/lab1.test.ts 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 1bb32af56..bd6d81b51 100644 --- a/rpgsaga/saga/package.json +++ b/rpgsaga/saga/package.json @@ -6,28 +6,28 @@ "scripts": { "dev": "ts-node --script-mode src/index.ts", "test": "jest", - "lint": "eslint src --ext .js --ext .jsx --ext .ts --ext .tsx", - "lint:fix": "npm run lint -- --fix" + "lint": "eslint \"{src,apps,libs,test}//*.ts\"", + "lint:fix": "eslint \"{src,apps,libs,test}//*.ts\" --fix" }, "author": "", "license": "ISC", - "dependencies": {}, "devDependencies": { - "@types/jest": "^27.0.3", - "@types/node": "^16.11.0", - "@typescript-eslint/eslint-plugin": "^5.0.0", - "@typescript-eslint/parser": "^5.0.0", - "eslint": "^7.28.0", - "eslint-config-prettier": "^8.3.0", - "eslint-plugin-import": "^2.23.4", - "eslint-plugin-prettier": "^3.4.0", - "jest": "^27.2.5", - "prettier": "^2.4.1", - "ts-node": "^10.3.0", - "typescript": "^4.4.4", - "ts-jest": "^27.1.2" + "@eslint/compat": "^1.2.1", + "@types/jest": "^29.5.13", + "@types/node": "^22.7.4", + "@typescript-eslint/eslint-plugin": "^8.8.0", + "@typescript-eslint/parser": "^8.8.0", + "eslint": "^9.12.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-import": "^2.31.0", + "eslint-plugin-prettier": "^5.2.1", + "jest": "^29.7.0", + "prettier": "^3.3.3", + "ts-jest": "^29.2.5", + "ts-node": "^10.9.2", + "typescript": "^5.6.2" }, "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..31af2e62f 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1 +1,11 @@ -console.log('Hello world'); +import {task14A, task14B} from './lab1'; + +const a = 7.2; +const b = 4.2; +let xn = 1.81; +let xk = 5.31; +let 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)); diff --git a/rpgsaga/saga/src/lab1.ts b/rpgsaga/saga/src/lab1.ts new file mode 100644 index 000000000..02591cea4 --- /dev/null +++ b/rpgsaga/saga/src/lab1.ts @@ -0,0 +1,29 @@ +declare interface Math { + log10(x: number): number; +} + + export function calculate14(a: number, b: number, x: number) { + let 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) { + let 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 (let y of xs) { + ans = calculate14(a, b, y); + res2.push(ans); + } + return res2 +} \ No newline at end of file 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/tsconfig.json b/rpgsaga/saga/tsconfig.json index 5a0ab7fe9..bd8fbadcb 100644 --- a/rpgsaga/saga/tsconfig.json +++ b/rpgsaga/saga/tsconfig.json @@ -1,5 +1,9 @@ { "compilerOptions": { - "types": ["jest", "node"] - } + "types": ["jest", "node"], + "target": "es6", + "module": "CommonJS", + "outDir": "out", + "sourceMap": true + } } \ No newline at end of file From 29023385b1e93cc7ea625af17ff3e3ac5365591c Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Sat, 2 Nov 2024 19:21:41 +0300 Subject: [PATCH 3/7] lab 2 + tests --- rpgsaga/saga/src/lab2.ts | 44 ++++++++++++++++++++++++++++++++ rpgsaga/saga/tests/lab2.test.ts | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 rpgsaga/saga/src/lab2.ts create mode 100644 rpgsaga/saga/tests/lab2.test.ts diff --git a/rpgsaga/saga/src/lab2.ts b/rpgsaga/saga/src/lab2.ts new file mode 100644 index 000000000..9c94d547f --- /dev/null +++ b/rpgsaga/saga/src/lab2.ts @@ -0,0 +1,44 @@ +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}` + } +} \ No newline at end of file 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"); + }); +}); From bef2291cf7d17189c80f4334e5ad0f7789fab625 Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Tue, 5 Nov 2024 19:06:05 +0300 Subject: [PATCH 4/7] update index.ts and lab1.ts --- rpgsaga/saga/src/index.ts | 6 +++--- rpgsaga/saga/src/lab1.ts | 8 ++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 31af2e62f..26676588b 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -2,9 +2,9 @@ import {task14A, task14B} from './lab1'; const a = 7.2; const b = 4.2; -let xn = 1.81; -let xk = 5.31; -let dx = 0.7; +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)); diff --git a/rpgsaga/saga/src/lab1.ts b/rpgsaga/saga/src/lab1.ts index 02591cea4..6fc3daca1 100644 --- a/rpgsaga/saga/src/lab1.ts +++ b/rpgsaga/saga/src/lab1.ts @@ -1,9 +1,5 @@ -declare interface Math { - log10(x: number): number; -} - - export function calculate14(a: number, b: number, x: number) { - let ans = Math.sqrt(Math.abs(a-b*x)/Math.pow(Math.log10(x), 3)); +export function calculate14(a: number, b: number, x: number) { + let ans = Math.sqrt(Math.abs(a - b * x) / Math.pow(Math.log10(x), 3)); return ans; } From 600520f9050bf0f02b273b7f07615eb8cd189707 Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Thu, 7 Nov 2024 13:18:23 +0300 Subject: [PATCH 5/7] update index.ts and lab1.ts --- rpgsaga/saga/src/index.ts | 2 +- rpgsaga/saga/src/lab1.ts | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 26676588b..b14b31afc 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,4 +1,4 @@ -import {task14A, task14B} from './lab1'; +import { task14A, task14B } from './lab1'; const a = 7.2; const b = 4.2; diff --git a/rpgsaga/saga/src/lab1.ts b/rpgsaga/saga/src/lab1.ts index 6fc3daca1..61455bfb0 100644 --- a/rpgsaga/saga/src/lab1.ts +++ b/rpgsaga/saga/src/lab1.ts @@ -1,25 +1,25 @@ export function calculate14(a: number, b: number, x: number) { - let ans = Math.sqrt(Math.abs(a - b * x) / Math.pow(Math.log10(x), 3)); - return ans; + 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) { - let y = calculate14(a, b, x); - res1.push(y); - } - return res1; -} + 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 (let y of xs) { - ans = calculate14(a, b, y); - res2.push(ans); - } - return res2 + const res2: number[] = []; + let ans = 0; + for (let y of xs) { + ans = calculate14(a, b, y); + res2.push(ans); + } + return res2 } \ No newline at end of file From 6f2332ed2b6b782193114f9437e39f35343dfe83 Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Thu, 21 Nov 2024 12:21:42 +0300 Subject: [PATCH 6/7] lint fix --- rpgsaga/saga/src/lab1.ts | 8 ++--- rpgsaga/saga/src/lab2.ts | 71 ++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/rpgsaga/saga/src/lab1.ts b/rpgsaga/saga/src/lab1.ts index 61455bfb0..371cdedb7 100644 --- a/rpgsaga/saga/src/lab1.ts +++ b/rpgsaga/saga/src/lab1.ts @@ -12,14 +12,12 @@ export function task14A(a: number, b: number, xn: number, xk: number, dx: number return res1; } - - export function task14B(a: number, b: number, xs: number[]) { const res2: number[] = []; let ans = 0; - for (let y of xs) { + for (const y of xs) { ans = calculate14(a, b, y); res2.push(ans); } - return res2 -} \ No newline at end of file + return res2; +} diff --git a/rpgsaga/saga/src/lab2.ts b/rpgsaga/saga/src/lab2.ts index 9c94d547f..357325440 100644 --- a/rpgsaga/saga/src/lab2.ts +++ b/rpgsaga/saga/src/lab2.ts @@ -1,44 +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; - } + name: string; + price: number; + type: string; - set setname(valuename: string) { - this.name = valuename; - } + constructor(dishname: string, dishprice: number, dishtype: string) { + this.name = dishname; + this.price = dishprice; + this.type = dishtype; + } - get getprice(): number { - return this.price; - } + get getname(): string { + return this.name; + } - set setprice(valueprice: number) { - if (valueprice < 0) { - console.error("The price cannot be less than zero!"); - } else { - this.price = valueprice; - } - } + set setname(valuename: string) { + this.name = valuename; + } - get gettype(): string { - return this.type; - } + get getprice(): number { + return this.price; + } - set settype(valuetype: string) { - this.type = valuetype; + set setprice(valueprice: number) { + if (valueprice < 0) { + console.error('The price cannot be less than zero!'); + } else { + this.price = valueprice; } + } - get getinfo(): string { - return `Dish's name: ${this.name}, dish's price: ${this.price}, dish's type: ${this.type}` - } -} \ No newline at end of file + 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}`; + } +} From 4316841157b046ca0e7064a563041ba4f24a4972 Mon Sep 17 00:00:00 2001 From: Ilya Paramonov Date: Thu, 21 Nov 2024 13:00:29 +0300 Subject: [PATCH 7/7] add output for lab2 --- rpgsaga/saga/src/index.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index b14b31afc..35f7dba07 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,4 +1,5 @@ import { task14A, task14B } from './lab1'; +import { Dish } from './lab2'; const a = 7.2; const b = 4.2; @@ -9,3 +10,9 @@ 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);