From 883638ab21c57642487de6793cd1a03920f9619d Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:42:51 +0300 Subject: [PATCH 01/15] mathematical task.js --- .../mathematical task.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js diff --git a/codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js b/codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js new file mode 100644 index 000000000..534bdeb89 --- /dev/null +++ b/codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js @@ -0,0 +1,23 @@ +function calculate(a: number, x: number): number { + return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); +} + + +const a = 2.0; +const xStart = 1.2; +const xEnd = 4.2; +const delX = 0.6; +const = xValues = [1.16, 1.32, 1.47, 1.65, 1.93]; + + +for (let x = xStart; x <= xEnd; x += delX) { + const y = calculate(a, x); + console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); +} + + +xValues.forEach(x => { + const y = calculate(a, x); + console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); +}); + From 1649f12765b9583eb995a7cebb4cf4c795270c49 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:47:34 +0300 Subject: [PATCH 02/15] mathematical task --- codewars/mathematical task | 1 + 1 file changed, 1 insertion(+) create mode 100644 codewars/mathematical task diff --git a/codewars/mathematical task b/codewars/mathematical task new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/codewars/mathematical task @@ -0,0 +1 @@ + From fdac8c5a3405c20635e906476a504482723a6caf Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:22:20 +0300 Subject: [PATCH 03/15] mathematical function --- rpgsaga/mathematical function | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 rpgsaga/mathematical function diff --git a/rpgsaga/mathematical function b/rpgsaga/mathematical function new file mode 100644 index 000000000..64062b6d0 --- /dev/null +++ b/rpgsaga/mathematical function @@ -0,0 +1,22 @@ +function calculate(a: number, x: number): number { + return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); +} + + +const a = 2.0; +const xStart = 1.2; +const xEnd = 4.2; +const delX = 0.6; +const = xValues = [1.16, 1.32, 1.47, 1.65, 1.93]; + + +for (let x = xStart; x <= xEnd; x += delX) { + const y = calculate(a, x); + console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); +} + + +xValues.forEach(x => { + const y = calculate(a, x); + console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); +}); From 4929984ccf159df0d27527dd3ada5c3cb8e0846d Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:43:29 +0300 Subject: [PATCH 04/15] Delete rpgsaga/mathematical function --- rpgsaga/mathematical function | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 rpgsaga/mathematical function diff --git a/rpgsaga/mathematical function b/rpgsaga/mathematical function deleted file mode 100644 index 64062b6d0..000000000 --- a/rpgsaga/mathematical function +++ /dev/null @@ -1,22 +0,0 @@ -function calculate(a: number, x: number): number { - return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); -} - - -const a = 2.0; -const xStart = 1.2; -const xEnd = 4.2; -const delX = 0.6; -const = xValues = [1.16, 1.32, 1.47, 1.65, 1.93]; - - -for (let x = xStart; x <= xEnd; x += delX) { - const y = calculate(a, x); - console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); -} - - -xValues.forEach(x => { - const y = calculate(a, x); - console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); -}); From e28254ee0f85574297ac2cd0cc14ef47403349fc Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:44:22 +0300 Subject: [PATCH 05/15] Delete codewars/mathematical task --- codewars/mathematical task | 1 - 1 file changed, 1 deletion(-) delete mode 100644 codewars/mathematical task diff --git a/codewars/mathematical task b/codewars/mathematical task deleted file mode 100644 index 8b1378917..000000000 --- a/codewars/mathematical task +++ /dev/null @@ -1 +0,0 @@ - From 68cee8b985ac53e3bc25078e586b44b2b12fc268 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:33:01 +0300 Subject: [PATCH 06/15] Delete codewars/codewars directory --- .../mathematical task.js | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js diff --git a/codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js b/codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js deleted file mode 100644 index 534bdeb89..000000000 --- a/codewars/codewars/mathematical_task.jsfunction calculate(a: number, x: number): number { return Math.pow(Math.log10(a + x), 2) /mathematical task.js +++ /dev/null @@ -1,23 +0,0 @@ -function calculate(a: number, x: number): number { - return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); -} - - -const a = 2.0; -const xStart = 1.2; -const xEnd = 4.2; -const delX = 0.6; -const = xValues = [1.16, 1.32, 1.47, 1.65, 1.93]; - - -for (let x = xStart; x <= xEnd; x += delX) { - const y = calculate(a, x); - console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); -} - - -xValues.forEach(x => { - const y = calculate(a, x); - console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); -}); - From ec9764e1405818e6e0de8f1607729538447d238e Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:03:57 +0300 Subject: [PATCH 07/15] codewars --- .../Adding Big Numbers/Adding Big Numbers.js | 19 ++++++++ .../Anagram difference/Anagram defferende.js | 28 ++++++++++++ codewars/Array Deep Count/Array Deep Count.js | 16 +++++++ codewars/Build Tower/Build Tower.js | 19 ++++++++ .../Convert string to camel case.js | 16 +++++++ .../Duplicate Encoder/Duplicate Encoder.js | 16 +++++++ .../Find the missing letter.js | 27 ++++++++++++ .../Flatten a Nested Map.js | 14 ++++++ .../Fun with tree - max sum.js | 0 .../Linked Lists - Sorted Insert.js | 26 +++++++++++ codewars/Merge two arrays/Merge two arrays.js | 18 ++++++++ .../Moving Zeros To The End.js | 18 ++++++++ codewars/Permutations/Permutations.js | 18 ++++++++ .../Product of consecutive Fib numbers.js | 10 +++++ codewars/Simple Pig Latin/Simple Pig Latin.js | 12 ++++++ codewars/Snail/Snail.js | 43 +++++++++++++++++++ .../Sum of Digits - Digital Root.js | 13 ++++++ codewars/Sum of Intervals/Sum of Intervals.js | 0 .../Tic-Tac-Toe Cheacker.js | 34 +++++++++++++++ .../Valid Parentheses/Valid Parentheses.js | 17 ++++++++ .../Where my anagrams at.js | 4 ++ 21 files changed, 368 insertions(+) create mode 100644 codewars/Adding Big Numbers/Adding Big Numbers.js create mode 100644 codewars/Anagram difference/Anagram defferende.js create mode 100644 codewars/Array Deep Count/Array Deep Count.js create mode 100644 codewars/Build Tower/Build Tower.js create mode 100644 codewars/Convert string to camel case/Convert string to camel case.js create mode 100644 codewars/Duplicate Encoder/Duplicate Encoder.js create mode 100644 codewars/Find the missing letter/Find the missing letter.js create mode 100644 codewars/Flatten a Nested Map/Flatten a Nested Map.js create mode 100644 codewars/Fun with tree - max sum/Fun with tree - max sum.js create mode 100644 codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js create mode 100644 codewars/Merge two arrays/Merge two arrays.js create mode 100644 codewars/Moving Zeros To The End/Moving Zeros To The End.js create mode 100644 codewars/Permutations/Permutations.js create mode 100644 codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js create mode 100644 codewars/Simple Pig Latin/Simple Pig Latin.js create mode 100644 codewars/Snail/Snail.js create mode 100644 codewars/Sum of Digits - Digital Root/Sum of Digits - Digital Root.js create mode 100644 codewars/Sum of Intervals/Sum of Intervals.js create mode 100644 codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Cheacker.js create mode 100644 codewars/Valid Parentheses/Valid Parentheses.js create mode 100644 codewars/Where my anagrams at/Where my anagrams at.js 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..87563575d --- /dev/null +++ b/codewars/Permutations/Permutations.js @@ -0,0 +1,18 @@ +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); + } \ No newline at end of file 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 From ec2f6fead206972ce6f32d0138a711ea85f3be03 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:11:38 +0300 Subject: [PATCH 08/15] lab 1 --- codewars/Permutations/Permutations.js | 3 ++- rpgsaga/saga/src/math.ts | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 rpgsaga/saga/src/math.ts diff --git a/codewars/Permutations/Permutations.js b/codewars/Permutations/Permutations.js index 87563575d..38472770b 100644 --- a/codewars/Permutations/Permutations.js +++ b/codewars/Permutations/Permutations.js @@ -15,4 +15,5 @@ function permutations(string) { backtrack('', string); return Array.from(result); - } \ No newline at end of file + +} diff --git a/rpgsaga/saga/src/math.ts b/rpgsaga/saga/src/math.ts new file mode 100644 index 000000000..61cbebb3d --- /dev/null +++ b/rpgsaga/saga/src/math.ts @@ -0,0 +1,24 @@ +function calculate(a: number, x: number): number { + return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); + } + + + const a = 2.0; + const xStart = 1.2; + const xEnd = 4.2; + const delX = 0.6; + const xValues = [1.16, 1.32, 1.47, 1.65, 1.93]; + + + for (let x = xStart; x <= xEnd; x += delX) { + const y = calculate(a, x); + console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); + } + + + xValues.forEach(x => { + const y = calculate(a, x); + console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); + }); + + \ No newline at end of file From 073fb232c793d3d2655d42ec67b2070e4088f472 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:13:01 +0300 Subject: [PATCH 09/15] Update index.ts --- rpgsaga/saga/src/index.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7bc4a71de..8ef628f88 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1 +1,12 @@ -console.log('Hello world'); +import {task19A, task19B} from './math'; + +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]; From 4cc94e38cc73a95beb02c21f1933440ed2b5d6a1 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:13:35 +0300 Subject: [PATCH 10/15] Update math.ts --- rpgsaga/saga/src/math.ts | 43 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/rpgsaga/saga/src/math.ts b/rpgsaga/saga/src/math.ts index 61cbebb3d..9c589dae8 100644 --- a/rpgsaga/saga/src/math.ts +++ b/rpgsaga/saga/src/math.ts @@ -1,24 +1,19 @@ -function calculate(a: number, x: number): number { - return Math.pow(Math.log10(a + x), 2) / Math.pow(a + x, 2); - } - - - const a = 2.0; - const xStart = 1.2; - const xEnd = 4.2; - const delX = 0.6; - const xValues = [1.16, 1.32, 1.47, 1.65, 1.93]; - - - for (let x = xStart; x <= xEnd; x += delX) { - const y = calculate(a, x); - console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); - } - - - xValues.forEach(x => { - const y = calculate(a, x); - console.log(`x = ${x.toFixed(2)}, y = ${y.toFixed(4)}`); - }); - - \ No newline at end of file +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)); +} From 2018a7f323beb788f987ccdfa7cd73076795655f Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:14:58 +0300 Subject: [PATCH 11/15] tests.ts --- rpgsaga/saga/src/tests.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 rpgsaga/saga/src/tests.ts 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); + }); + }); + From 64f3d11313a862529a94daa9a3eb3652e423b6a0 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Wed, 27 Nov 2024 20:27:28 +0300 Subject: [PATCH 12/15] Create testslab1.ts --- rpgsaga/saga/tests/testslab1.ts | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 rpgsaga/saga/tests/testslab1.ts 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); + }); + }); + From 939472a66a372f06834d146b45652a4540884597 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Wed, 27 Nov 2024 20:34:00 +0300 Subject: [PATCH 13/15] Create labclass2.ts --- rpgsaga/saga/src/labclass2.ts | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 rpgsaga/saga/src/labclass2.ts 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("; + } + } +} From 11d1938af785e4e3cf92ba8d30e81660972a2bc0 Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Wed, 27 Nov 2024 22:35:03 +0300 Subject: [PATCH 14/15] Create lab2tests.ts --- rpgsaga/saga/tests/lab2tests.ts | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 rpgsaga/saga/tests/lab2tests.ts 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("); + }); +}); From 5a40508a45cee993f95c2f744a1e46cae8ebb70c Mon Sep 17 00:00:00 2001 From: rusafob <147656000+rusafob@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:37:02 +0300 Subject: [PATCH 15/15] Update index.ts --- rpgsaga/saga/src/index.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 8ef628f88..2fcf9bea3 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,4 +1,6 @@ import {task19A, task19B} from './math'; +import { Mouse } from './src/labclass2'; + const a = 2.0; const x1 = 1.16; @@ -10,3 +12,26 @@ 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')); + + + + + + + + + + + + + + + + +