From 12069951cc9c07e12751cb24da473bb3c72f9ea1 Mon Sep 17 00:00:00 2001 From: fuyom Date: Tue, 19 Dec 2023 16:07:38 +0300 Subject: [PATCH 1/2] add codewars --- .../Adding Big Numbers/Adding Big Numbers.js | 15 ++++++++++ codewars/Array Deep Count/Array Deep Count.js | 13 +++++++++ codewars/Build Tower/Build Tower.js | 10 +++++++ .../Duplicate Encoder/Duplicate Encoder.js | 14 +++++++++ .../Find the missing letter.js | 13 +++++++++ .../Linked Lists - Sorted Insert.js | 21 ++++++++++++++ codewars/Merge two arrays/Merge two arrays.js | 15 ++++++++++ .../moving zeros to the end.js | 15 ++++++++++ .../Product of consecutive.js | 21 ++++++++++++++ codewars/Simple Pig Latin/Simple Pig Latin.js | 11 +++++++ codewars/Snail/Snail.js | 21 ++++++++++++++ .../Sum of Digits.js | 11 +++++++ codewars/Sum of Intervals/sum of intervals.js | 21 ++++++++++++++ codewars/Sum of pairs/sum of pairs.js | 16 ++++++++++ .../Valid Parentheses/Valid Parentheses.js | 29 +++++++++++++++++++ 15 files changed, 246 insertions(+) create mode 100644 codewars/Adding Big Numbers/Adding Big Numbers.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/Duplicate Encoder/Duplicate Encoder.js create mode 100644 codewars/Find the missing letter/Find the missing letter.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/Product of consecutive Fib numbers/Product of consecutive.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.js create mode 100644 codewars/Sum of Intervals/sum of intervals.js create mode 100644 codewars/Sum of pairs/sum of pairs.js create mode 100644 codewars/Valid Parentheses/Valid Parentheses.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 0000000..877de62 --- /dev/null +++ b/codewars/Adding Big Numbers/Adding Big Numbers.js @@ -0,0 +1,15 @@ +function add(num1, num2) { + let res = ""; + let carry = 0; + let len1 = num1.length, len2 = num2.length; + let i = len1 - 1, j = len2 - 1; + while (i >= 0 || j >= 0 || carry > 0) { + let sum = (i >= 0 ? parseInt(num1[i]) : 0) + + (j >= 0 ? parseInt(num2[j]) : 0) + + carry; + carry = Math.floor(sum / 10); + res = (sum % 10) + res; + i--; j--; + } + return res; +} \ 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 0000000..6264ffb --- /dev/null +++ b/codewars/Array Deep Count/Array Deep Count.js @@ -0,0 +1,13 @@ +function deepCount(arr) { + let count = 0; + + for (let i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + count += deepCount(arr[i]); + } else { + count++; + } + } + + return count; + } \ No newline at end of file diff --git a/codewars/Build Tower/Build Tower.js b/codewars/Build Tower/Build Tower.js new file mode 100644 index 0000000..9fc16c2 --- /dev/null +++ b/codewars/Build Tower/Build Tower.js @@ -0,0 +1,10 @@ +function towerBuilder(nFloors) { + let tower = []; + + for (let i = 0; i < nFloors; i++) { + let row = " ".repeat(nFloors - i - 1) + "*".repeat(2 * i + 1) + " ".repeat(nFloors - i - 1); + tower.push(row); + } + + return tower; +} \ 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 0000000..1ec0fec --- /dev/null +++ b/codewars/Duplicate Encoder/Duplicate Encoder.js @@ -0,0 +1,14 @@ +function duplicateEncode(word) { + const lowerCaseWord = word.toLowerCase(); + let result = ''; + + for (let i = 0; i < lowerCaseWord.length; i++) { + if (lowerCaseWord.indexOf(lowerCaseWord[i]) === lowerCaseWord.lastIndexOf(lowerCaseWord[i])) { + result += '('; + } else { + result += ')'; + } + } + + return result; + } \ 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 0000000..78b00ab --- /dev/null +++ b/codewars/Find the missing letter/Find the missing letter.js @@ -0,0 +1,13 @@ +function findMissingLetter(array) { + let sortedArray = array.sort(); + let result = ''; + + for (let i = 0; i < sortedArray.length - 1; i++) { + if (sortedArray[i + 1].charCodeAt(0) !== sortedArray[i].charCodeAt(0) + 1) { + result = String.fromCharCode(sortedArray[i].charCodeAt(0) + 1); + break; + } + } + + return result; + } \ No newline at end of file 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 0000000..e50f80d --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js @@ -0,0 +1,21 @@ +function Node(data) { + this.data = data; + this.next = null; + } + + function sortedInsert(head, data) { + if (head === null || head.data > data) { + let newNode = new Node(data); + newNode.next = head; + return newNode; + } + + if (head.next === null) { + let newNode = new Node(data); + head.next = newNode; + return head; + } + + head.next = sortedInsert(head.next, data); + 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 0000000..14d74f2 --- /dev/null +++ b/codewars/Merge two arrays/Merge two arrays.js @@ -0,0 +1,15 @@ +function mergeArrays(a, b) { + let result = []; + let maxLength = Math.max(a.length, b.length); + + for (let i = 0; i < maxLength; i++) { + if (i < a.length) { + result.push(a[i]); + } + if (i < b.length) { + result.push(b[i]); + } + } + + return result; + } \ 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 0000000..4e9203f --- /dev/null +++ b/codewars/Moving Zeros To The End/moving zeros to the end.js @@ -0,0 +1,15 @@ +function moveZeroes(arr) { + let index = 0; + + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== 0) { + arr[index] = arr[i]; + if (i !== index) { + arr[i] = 0; + } + index++; + } + } + + return arr; + } \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/Product of consecutive.js b/codewars/Product of consecutive Fib numbers/Product of consecutive.js new file mode 100644 index 0000000..c5a1213 --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/Product of consecutive.js @@ -0,0 +1,21 @@ +function productFib(prod) { + let n = 0; + let n1 = 1; + let n2 = 1; + let arr = [0, 1, 1]; + + while (n2 < prod) { + n = n1; + n1 = n2; + n2 = n + n1; + arr = [n, n1, n2]; + } + + if (n2 * n1 === prod) { + arr[2] = true; + } else { + arr[2] = false; + } + + return arr; + } \ 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 0000000..3490d3e --- /dev/null +++ b/codewars/Simple Pig Latin/Simple Pig Latin.js @@ -0,0 +1,11 @@ +function pigIt(str) { + let words = str.split(' '); + let piggedWords = words.map(word => { + if (/^[aeiou]/i.test(word)) { + return word + 'ay'; + } else { + return word.slice(1) + word.charAt(0) + 'ay'; + } + }); + return piggedWords.join(' '); + } \ No newline at end of file diff --git a/codewars/Snail/Snail.js b/codewars/Snail/Snail.js new file mode 100644 index 0000000..b9c91f0 --- /dev/null +++ b/codewars/Snail/Snail.js @@ -0,0 +1,21 @@ +let snail = function(array) { + if (!array.length) return []; + const R = array.length, C = array[0].length; + const seen = Array.from({length: R}, () => Array(C).fill(false)); + const dr = [0, 1, 0, -1], dc = [1, 0, -1, 0]; + let ans = [], r = 0, c = 0, di = 0; + for (let _ = 0; _ < R * C; _++) { + ans.push(array[r][c]); + seen[r][c] = true; + const cr = r + dr[di], cc = c + dc[di]; + if (0 <= cr && cr < R && 0 <= cc && cc < C && !seen[cr][cc]) { + r = cr; + c = cc; + } else { + di = (di + 1) % 4; + r += dr[di]; + c += dc[di]; + } + } + return ans; +} \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/Sum of Digits.js b/codewars/Sum of Digits - Digital Root/Sum of Digits.js new file mode 100644 index 0000000..2e5b96c --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/Sum of Digits.js @@ -0,0 +1,11 @@ +function digitalRoot(n) { + while (n >= 10) { + let sum = 0; + while (n > 0) { + sum += n % 10; + n = Math.floor(n / 10); + } + n = sum; + } + return n; +} \ 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 0000000..de39285 --- /dev/null +++ b/codewars/Sum of Intervals/sum of intervals.js @@ -0,0 +1,21 @@ +function sumIntervals(intervals) { + // Объединение перекрывающихся интервалов + intervals.sort((a, b) => a[0] - b[0]); + + let merged = []; + for (let i = 0; i < intervals.length; i++) { + if (merged.length === 0 || merged[merged.length - 1][1] < intervals[i][0]) { + merged.push(intervals[i]); + } else { + merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], intervals[i][1]); + } + } + + // Вычисление суммы длин интервалов + let sum = 0; + for (let i = 0; i < merged.length; i++) { + sum += merged[i][1] - merged[i][0]; + } + + return sum; +} \ No newline at end of file diff --git a/codewars/Sum of pairs/sum of pairs.js b/codewars/Sum of pairs/sum of pairs.js new file mode 100644 index 0000000..9e2295a --- /dev/null +++ b/codewars/Sum of pairs/sum of pairs.js @@ -0,0 +1,16 @@ +function sumPairs(ints, s) { + let obj = {}; + let arr; + + for (let i = 0; i < ints.length; i++) { + let complement = s - ints[i]; + if (complement in obj) { + arr = [ints[i], complement]; + break; + } else { + obj[ints[i]] = true; + } + } + + return arr || undefined; + } \ 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 0000000..5820957 --- /dev/null +++ b/codewars/Valid Parentheses/Valid Parentheses.js @@ -0,0 +1,29 @@ +function validParentheses(parens) { + const stack = []; + + for (let i = 0; i < parens.length; i++) { + if (parens[i] === '(') { + stack.push(parens[i]); + } + + else if (parens[i] === ')') { + if (stack.length === 0) { + return false; + } + + if (stack[stack.length - 1] === '(') { + stack.pop(); + } + + else { + return false; + } + } + } + + if (stack.length !== 0) { + return false; + } + + return true; + } \ No newline at end of file From 265c5de3433fa2649836ab5250dcb1afc398b7e1 Mon Sep 17 00:00:00 2001 From: fuyom Date: Tue, 19 Dec 2023 16:17:12 +0300 Subject: [PATCH 2/2] add class fox --- rpgsaga/saga/src/fox.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rpgsaga/saga/src/fox.ts diff --git a/rpgsaga/saga/src/fox.ts b/rpgsaga/saga/src/fox.ts new file mode 100644 index 0000000..565c5b3 --- /dev/null +++ b/rpgsaga/saga/src/fox.ts @@ -0,0 +1,34 @@ +class Fox { + private name: string; + private age: number; + private color: string; + + constructor(name: string, age: number, color: string) { + if (!name || !color || age < 0) { + throw new Error("Invalid arguments"); + } + this.name = name; + this.age = age; + this.color = color; + } + + public introduce(): string { + return `Hello, I am ${this.name}, a ${this.color} fox and I am ${this.age} years old.`; + } + + public makeSound(): string { + return "Ring-ding-ding-ding-dingeringeding!"; + } + + public getView(): number[] { + const asciiCodes: number[] = []; + for (let i = 0; i < this.name.length; i++) { + asciiCodes.push(this.name.charCodeAt(i)); + } + return asciiCodes; + } + + public toString(): string { + return this.introduce(); + } + } \ No newline at end of file