From b3e22537da52deb0120069fac00b82dc57344ea9 Mon Sep 17 00:00:00 2001 From: ThisIsVladis Date: Sat, 30 Sep 2023 23:51:10 +0300 Subject: [PATCH 1/9] CodeWars yse --- .idea/inspectionProfiles/Project_Default.xml | 6 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 89 +++++++++++++++++++ codewars/Adding Big Numbers/index.js | 22 +++++ codewars/Anagram difference/index.js | 24 +++++ codewars/Array Deep Count/index.js | 16 ++++ codewars/Build Tower/index.js | 18 ++++ .../Convert string to camel case/index.js | 11 +++ codewars/Duplicate Encoder/index.js | 22 +++++ codewars/Find the missing letter/index.js | 11 +++ codewars/Flatten a Nested Map/index.js | 30 +++++++ codewars/Fun with tree - max sum/.gitkeep | 2 +- codewars/Fun with tree - max sum/index.js | 26 ++++++ .../Linked Lists - Sorted Insert/index.js | 27 ++++++ codewars/Merge two arrays/index.js | 5 ++ codewars/Moving Zeros To The End/index.js | 16 ++++ codewars/Permutations/index.js | 25 ++++++ .../index.js | 17 ++++ codewars/Simple Pig Latin/index.js | 16 ++++ codewars/Snail/index.js | 14 +++ .../Sum of Digits - Digital Root/index.js | 9 ++ codewars/Sum of Intervals/index.js | 25 ++++++ codewars/Sum of pairs/index.js | 20 +++++ codewars/Tic-Tac-Toe Checker/index.js | 27 ++++++ codewars/Valid Parentheses/index.js | 13 +++ codewars/Where my anagrams at/index.js | 15 ++++ 26 files changed, 511 insertions(+), 1 deletion(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 codewars/Adding Big Numbers/index.js create mode 100644 codewars/Anagram difference/index.js create mode 100644 codewars/Array Deep Count/index.js create mode 100644 codewars/Build Tower/index.js create mode 100644 codewars/Convert string to camel case/index.js create mode 100644 codewars/Duplicate Encoder/index.js create mode 100644 codewars/Find the missing letter/index.js create mode 100644 codewars/Flatten a Nested Map/index.js create mode 100644 codewars/Fun with tree - max sum/index.js create mode 100644 codewars/Linked Lists - Sorted Insert/index.js create mode 100644 codewars/Merge two arrays/index.js create mode 100644 codewars/Moving Zeros To The End/index.js create mode 100644 codewars/Permutations/index.js create mode 100644 codewars/Product of consecutive Fib numbers/index.js create mode 100644 codewars/Simple Pig Latin/index.js create mode 100644 codewars/Snail/index.js create mode 100644 codewars/Sum of Digits - Digital Root/index.js create mode 100644 codewars/Sum of Intervals/index.js create mode 100644 codewars/Sum of pairs/index.js create mode 100644 codewars/Tic-Tac-Toe Checker/index.js create mode 100644 codewars/Valid Parentheses/index.js create mode 100644 codewars/Where my anagrams at/index.js diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..67cd363 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1696105752635 + + + + + + \ No newline at end of file diff --git a/codewars/Adding Big Numbers/index.js b/codewars/Adding Big Numbers/index.js new file mode 100644 index 0000000..8161ec9 --- /dev/null +++ b/codewars/Adding Big Numbers/index.js @@ -0,0 +1,22 @@ +function add(a, b) { + let result = '' + let carry = 0 + let i = a.length - 1 + let j = b.length - 1 + + while (i >= 0 || j >= 0 || carry > 0) { + const digit1 = i >= 0 ? parseInt(a[i]) : 0 + const digit2 = j >= 0 ? parseInt(b[j]) : 0 + const sum = digit1 + digit2 + carry + + carry = Math.floor(sum / 10) + result = (sum % 10) + result + + i-- + j-- + } + + return result +} + +add() \ No newline at end of file diff --git a/codewars/Anagram difference/index.js b/codewars/Anagram difference/index.js new file mode 100644 index 0000000..8c6ed63 --- /dev/null +++ b/codewars/Anagram difference/index.js @@ -0,0 +1,24 @@ +function anagramDifference(w1,w2){ + const charCount = {} + let deletedCount = 0 + + for (const char of w1) { + charCount[char] = (charCount[char] || 0) + 1 + } + + for (const char of w2) { + if (!charCount[char]) { + deletedCount++ + } else { + charCount[char]-- + } + } + + for (const char in charCount) { + deletedCount += charCount[char] + } + + return deletedCount +} + +anagramDifference() \ No newline at end of file diff --git a/codewars/Array Deep Count/index.js b/codewars/Array Deep Count/index.js new file mode 100644 index 0000000..11309ad --- /dev/null +++ b/codewars/Array Deep Count/index.js @@ -0,0 +1,16 @@ +function deepCount(a){ + let count = 0 + + a.forEach((el) => { + if (Array.isArray(el)) { + count += 1 + count += deepCount(el) + }else { + count += 1 + } + }) + + return count +} + +deepCount() \ No newline at end of file diff --git a/codewars/Build Tower/index.js b/codewars/Build Tower/index.js new file mode 100644 index 0000000..5c06aa5 --- /dev/null +++ b/codewars/Build Tower/index.js @@ -0,0 +1,18 @@ +function towerBuilder(nFloors) { + if (nFloors === 1) return ["*"] + + let newList = "*".repeat(nFloors * 2 - 1).split("") + let resArr = [] + + for (i = 0; i < nFloors; i++) { + resArr.push(newList.join('')); + + newList[i] = " " + newList[newList.length - 1 - i] = " " + } + + + return resArr.reverse(); +} + +towerBuilder() \ No newline at end of file diff --git a/codewars/Convert string to camel case/index.js b/codewars/Convert string to camel case/index.js new file mode 100644 index 0000000..e491548 --- /dev/null +++ b/codewars/Convert string to camel case/index.js @@ -0,0 +1,11 @@ +function toCamelCase(str){ + const words = str.split(/[-_]/) + + for (i = 1; i < words.length; i++) { + words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1) + } + + return words.join('') +} + +toCamelCase() \ No newline at end of file diff --git a/codewars/Duplicate Encoder/index.js b/codewars/Duplicate Encoder/index.js new file mode 100644 index 0000000..5357c08 --- /dev/null +++ b/codewars/Duplicate Encoder/index.js @@ -0,0 +1,22 @@ +function duplicateEncode(word){ + const lowWord = word.toLowerCase() + const charCountMap = new Map() + + let newStr = "" + + for (const char of lowWord) { + charCountMap.set(char, (charCountMap.get(char) || 0) + 1); + } + + for (const char of lowWord) { + if (charCountMap.get(char) === 1) { + newStr += '('; + } else { + newStr += ')'; + } + } + + return newStr; +} + +duplicateEncode() \ No newline at end of file diff --git a/codewars/Find the missing letter/index.js b/codewars/Find the missing letter/index.js new file mode 100644 index 0000000..c6433e1 --- /dev/null +++ b/codewars/Find the missing letter/index.js @@ -0,0 +1,11 @@ +function findMissingLetter(array) { + let firstlet = array[0].charCodeAt(0) + + for (i = 1; i < array.length; i++) { + if (firstlet + i !== array[i].charCodeAt(0)) { + return String.fromCharCode(firstlet + i) + } + } +} + +findMissingLetter() \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/index.js b/codewars/Flatten a Nested Map/index.js new file mode 100644 index 0000000..5313cbf --- /dev/null +++ b/codewars/Flatten a Nested Map/index.js @@ -0,0 +1,30 @@ +function flattenMap(map) { + const resObj = {} + const nameMap = '' + + if (Object.keys(map).length === 0) return {} + + const recurseMap = (obj, path) => { + + if (Array.isArray(obj) || obj === null) { + resObj[path.join('/')] = obj + } + + else { + for (const key in obj) { + const newPath = path.concat(key) + if (typeof obj[key] === 'object') { + recurseMap(obj[key], newPath); + } else { + resObj[newPath.join('/')] = obj[key] + } + } + } + } + + recurseMap(map, []) + + return resObj +} + +flattenMap() \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/.gitkeep b/codewars/Fun with tree - max sum/.gitkeep index d3f5a12..8b13789 100644 --- a/codewars/Fun with tree - max sum/.gitkeep +++ b/codewars/Fun with tree - max sum/.gitkeep @@ -1 +1 @@ - + diff --git a/codewars/Fun with tree - max sum/index.js b/codewars/Fun with tree - max sum/index.js new file mode 100644 index 0000000..044ad06 --- /dev/null +++ b/codewars/Fun with tree - max sum/index.js @@ -0,0 +1,26 @@ +function maxSum(root) { + if (!root) { + return 0; + } + + function dfs(node, currentSum) { + if (!node) { + return currentSum; + } + + currentSum += node.value; + + if (!node.left && !node.right) { + return currentSum; + } + + const leftSum = dfs(node.left, currentSum); + const rightSum = dfs(node.right, currentSum); + + return Math.max(leftSum, rightSum); + } + + return dfs(root, 0); +} + +maxSum() \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/index.js b/codewars/Linked Lists - Sorted Insert/index.js new file mode 100644 index 0000000..644b030 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/index.js @@ -0,0 +1,27 @@ +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +function sortedInsert(head, data) { + const newNode = new Node(data); + console.log(newNode, data, head) + + if (!head || data <= head.data) { + newNode.next = head; + return newNode; + } + + let current = head; + while (current.next && data > current.next.data) { + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + + return head; +} +sortedInsert() \ No newline at end of file diff --git a/codewars/Merge two arrays/index.js b/codewars/Merge two arrays/index.js new file mode 100644 index 0000000..f086993 --- /dev/null +++ b/codewars/Merge two arrays/index.js @@ -0,0 +1,5 @@ +function mergeArrays(a, b) { + return (b.forEach((el, id) => a.splice(id * 2 + 1, 0, el)), a) +} + +mergeArrays() \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/index.js b/codewars/Moving Zeros To The End/index.js new file mode 100644 index 0000000..7407a8d --- /dev/null +++ b/codewars/Moving Zeros To The End/index.js @@ -0,0 +1,16 @@ +function moveZeros(arr) { + let nonZeroElements = []; + let zeroElements = []; + + for (let element of arr) { + if (element === 0) { + zeroElements.push(element); + } else { + nonZeroElements.push(element); + } + } + + return nonZeroElements.concat(zeroElements); +} + +moveZeros() \ No newline at end of file diff --git a/codewars/Permutations/index.js b/codewars/Permutations/index.js new file mode 100644 index 0000000..16faff0 --- /dev/null +++ b/codewars/Permutations/index.js @@ -0,0 +1,25 @@ +function permutations(string) { + const strArr = string.split('') + const resArr = new Set() + + function generatePermutations(arr, length) { + if (length === 1) { + resArr.add(arr.join('')) + } else { + for (let i = 0; i < length; i++) { + generatePermutations(arr, length - 1) + if (length % 2 === 0) { + [arr[i], arr[length - 1]] = [arr[length - 1], arr[i]]; + } else { + [arr[0], arr[length - 1]] = [arr[length - 1], arr[0]] + } + } + } + } + + generatePermutations(strArr, strArr.length) + + return Array.from(resArr) +} + +permutations() \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/index.js b/codewars/Product of consecutive Fib numbers/index.js new file mode 100644 index 0000000..aeea6f9 --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/index.js @@ -0,0 +1,17 @@ +function productFib(prod){ + let [fibPrev, fibCur] = [0, 1] + + while (fibPrev * fibCur < prod ){ + console.log([fibPrev, fibCur, fibPrev * fibCur === prod]) + + const fibNext = fibPrev + fibCur + + fibPrev = fibCur + fibCur = fibNext + } + + console.log([fibPrev, fibCur, fibPrev * fibCur === prod]) + return [fibPrev, fibCur, fibPrev * fibCur === prod] +} + +productFib() \ No newline at end of file diff --git a/codewars/Simple Pig Latin/index.js b/codewars/Simple Pig Latin/index.js new file mode 100644 index 0000000..2901ec6 --- /dev/null +++ b/codewars/Simple Pig Latin/index.js @@ -0,0 +1,16 @@ +function pigIt(str){ + let newWord = [] + const strArr = str.split(' ') + + for (i = 0; i < strArr.length; i++) { + if (!(/[a-zA-Z0-9]/.test(strArr[i]))) { + newWord.push(strArr[i]) + }else{ + console.log(str) + newWord.push(strArr[i].slice(1) + strArr[i].charAt(0) + 'ay') + } + } + return newWord.join(' ') +} + +pigIt() \ No newline at end of file diff --git a/codewars/Snail/index.js b/codewars/Snail/index.js new file mode 100644 index 0000000..2a16b4d --- /dev/null +++ b/codewars/Snail/index.js @@ -0,0 +1,14 @@ +snail = function(array) { + const result = []; + while (array.length > 0) { + result.push(...array[0]); + + array.shift(); + + //перевот матрицы на 90 + array = array[0] ? array[0].map((_, i) => array.map(row => row[i])).reverse() : []; + } + return result; +} + +snail() \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/index.js b/codewars/Sum of Digits - Digital Root/index.js new file mode 100644 index 0000000..9ceb3cc --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/index.js @@ -0,0 +1,9 @@ +function digitalRoot(n) { + if (n < 10) { + return n + } + + return digitalRoot(n.toString().split('').reduce((acc, cur) => acc + parseFloat(cur), 0)) +} + +digitalRoot() \ No newline at end of file diff --git a/codewars/Sum of Intervals/index.js b/codewars/Sum of Intervals/index.js new file mode 100644 index 0000000..188137d --- /dev/null +++ b/codewars/Sum of Intervals/index.js @@ -0,0 +1,25 @@ +function sumIntervals(intervals) { + intervals.sort((a, b) => a[0] - b[0]) + + let mergedIntervals = [intervals[0]] + + for (let i = 1; i < intervals.length; i++) { + const currentInterval = intervals[i] + const lastMergedInterval = mergedIntervals[mergedIntervals.length - 1] + + if (currentInterval[0] <= lastMergedInterval[1]) { + lastMergedInterval[1] = Math.max(lastMergedInterval[1], currentInterval[1]) + } else { + mergedIntervals.push(currentInterval) + } + } + + let totalLength = 0; + for (const interval of mergedIntervals) { + totalLength += interval[1] - interval[0] + } + + return totalLength +} + +sumIntervals() \ No newline at end of file diff --git a/codewars/Sum of pairs/index.js b/codewars/Sum of pairs/index.js new file mode 100644 index 0000000..72a2d19 --- /dev/null +++ b/codewars/Sum of pairs/index.js @@ -0,0 +1,20 @@ +function sumPairs(ints, s) { + const numIndex = {}; + + for (let i = 0; i < ints.length; i++) { + const currentNum = ints[i] + const numPair = s - currentNum + + if (numIndex.hasOwnProperty(numPair)) { + return [numPair, currentNum] + } + + if (!numIndex.hasOwnProperty(currentNum)) { + numIndex[currentNum] = i + } + } + + return undefined +} + +sumPairs() \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/index.js b/codewars/Tic-Tac-Toe Checker/index.js new file mode 100644 index 0000000..483b89a --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/index.js @@ -0,0 +1,27 @@ +function isSolved(board) { + for (let i = 0; i < 3; i++) { + if (board[i][0] === board[i][1] && board[i][1] === board[i][2]) { + if (board[i][0] === 1) return 1 + if (board[i][0] === 2) return 2 + } + } + + if (board[0][0] === board[1][1] && board[1][1] === board[2][2]) { + if (board[0][0] === 1) return 1 + if (board[0][0] === 2) return 2 + } + if (board[0][2] === board[1][1] && board[1][1] === board[2][0]) { + if (board[0][2] === 1) return 1 + if (board[0][2] === 2) return 2 + } + + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (board[i][j] === 0) return -1 + } + } + + return 0; +} + +isSolved() \ No newline at end of file diff --git a/codewars/Valid Parentheses/index.js b/codewars/Valid Parentheses/index.js new file mode 100644 index 0000000..b86d5da --- /dev/null +++ b/codewars/Valid Parentheses/index.js @@ -0,0 +1,13 @@ +function validParentheses(parens) { + let count = 0 + + for (i = 0; i < parens.length; i++) { + if (parens[i] === '(') count++; + if (parens[i] === ')') count--; + if (count < 0) return false; + } + + return count === 0; +} + +validParentheses() \ No newline at end of file diff --git a/codewars/Where my anagrams at/index.js b/codewars/Where my anagrams at/index.js new file mode 100644 index 0000000..9d813ff --- /dev/null +++ b/codewars/Where my anagrams at/index.js @@ -0,0 +1,15 @@ +function anagrams(word, words) { + const wordArr = word.split("") + const resArr = [] + + for (i = 0; i < words.length; i++) { + + if (wordArr.sort().join('') === words[i].split('').sort().join('')) { + resArr.push(words[i]) + } + } + + return resArr +} + +anagrams() \ No newline at end of file From 606901636601b3d565664823a691fd210d92d74b Mon Sep 17 00:00:00 2001 From: ThisIsVladis Date: Sat, 30 Sep 2023 23:51:53 +0300 Subject: [PATCH 2/9] CodeWars yse --- codewars/Flatten a Nested Map/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/codewars/Flatten a Nested Map/index.js b/codewars/Flatten a Nested Map/index.js index 5313cbf..fb93aa4 100644 --- a/codewars/Flatten a Nested Map/index.js +++ b/codewars/Flatten a Nested Map/index.js @@ -1,6 +1,5 @@ function flattenMap(map) { const resObj = {} - const nameMap = '' if (Object.keys(map).length === 0) return {} From d6038c3835bfa3af22dd4882eaf6f58c22c78cd4 Mon Sep 17 00:00:00 2001 From: ThisIsVladis Date: Sun, 1 Oct 2023 03:33:17 +0300 Subject: [PATCH 3/9] RpgSaga yse --- .idea/workspace.xml | 133 +++++++++++++++++++++++++------ rpgsaga/saga/src/fox.ts | 67 ++++++++++++++++ rpgsaga/saga/src/index.ts | 16 ---- rpgsaga/saga/src/phone.ts | 28 ------- rpgsaga/saga/tests/fox.spec.ts | 82 +++++++++++++++++++ rpgsaga/saga/tests/phone.spec.ts | 46 ----------- rpgsaga/saga/tsconfig.json | 4 +- 7 files changed, 260 insertions(+), 116 deletions(-) create mode 100644 rpgsaga/saga/src/fox.ts delete mode 100644 rpgsaga/saga/src/phone.ts create mode 100644 rpgsaga/saga/tests/fox.spec.ts delete mode 100644 rpgsaga/saga/tests/phone.spec.ts diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 67cd363..6dc8578 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,30 +4,13 @@