-
Notifications
You must be signed in to change notification settings - Fork 30
codewars and moodle #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mary2811
wants to merge
5
commits into
ISUCT:Ivanova_Marija_Alekseevna
Choose a base branch
from
Mary2811:master
base: Ivanova_Marija_Alekseevna
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| class Person { | ||
| constructor(name, age, gender) { | ||
| if (typeof name !== 'string' || typeof gender !== 'string' || typeof age !== 'number') { | ||
| throw new Error('Invalid argument type'); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в ts не пришлось бы такое писать + тесты на этот случай надо |
||
| this.name = name; | ||
| this.age = age; | ||
| this.gender = gender; | ||
| } | ||
|
|
||
| static createMan(name, age) { | ||
| return new Person(name, age, 'man'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'man' 'wooman' в enum (но он только в ts вроде бы |
||
| } | ||
|
|
||
| static createWoman(name, age) { | ||
| return new Person(name, age, 'woman'); | ||
| } | ||
|
|
||
| greet() { | ||
| return `Hello, my name is ${this.name}. I am ${this.age} years old and I am a ${this.gender}.`; | ||
| } | ||
|
|
||
| isAdult() { | ||
| return this.age >= 18; | ||
| } | ||
|
|
||
| toString() { | ||
| return `Person: ${this.name}, ${this.age}, ${this.gender}`; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| try { | ||
| const person1 = new Person('John', 25, 'man'); | ||
| console.log(person1.greet()); // Output: Hello, my name is John. I am 25 years old and I am a man. | ||
| console.log(person1.isAdult()); // Output: true | ||
| console.log(person1.toString()); // Output: Person: John, 25, man | ||
|
|
||
| const person2 = Person.createWoman('Jane', 17); | ||
| console.log(person2.greet()); // Output: Hello, my name is Jane. I am 20 years old and I am a woman. | ||
| console.log(person2.isAdult()); // Output: true | ||
| console.log(person2.toString()); // Output: Person: Jane, 20, woman | ||
|
|
||
| const person3 = new Person(123, '30', 'man'); // Invalid argument type | ||
| } catch (error) { | ||
| console.log(error.message); | ||
| } | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| function getSumOfDeletedLetters(word1, word2) { | ||
| // второе слово в обратный порядок | ||
| const reversedWord2 = word2.split('').reverse().join(''); | ||
| let sum = 0; | ||
| // по каждой букве первого слова | ||
| for (let i = 0; i < word1.length; i++) { | ||
| if (word1[i] !== reversedWord2[i]) { | ||
| sum++; | ||
| } | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
|
|
||
| // Пример использования | ||
| const word1 = 'code wars'; | ||
| const word2 = 'hack er rank'; | ||
| const result1 = getSumOfDeletedLetters(word1, word2); | ||
| console.log(result1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| function countElements(arr) { | ||
| let count = 0; | ||
|
|
||
| function countRecursive(subArr) { | ||
| for (let i = 0; i < subArr.length; i++) { | ||
| if (Array.isArray(subArr[i])) { | ||
| countRecursive(subArr[i]); | ||
| } else { | ||
| count++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| countRecursive(arr); | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| // Пример использования | ||
| const array = [1, 2, [3, 4, [5, 6]], 7, [8, 9]]; | ||
| console.log(countElements(array)); // Output: 9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| function build (pyramidHeight) { | ||
| for (let i = 1; i <= pyramidHeight; i++) { | ||
| let row = ''; | ||
| for(let j = 1; j <= (pyramidHeight - i); j++) { | ||
| row += ' '; | ||
| } | ||
| for(let k = 1; k <= (2 * i - 1); k++) { | ||
| row += '*'; | ||
| } | ||
| console.log(row); | ||
| } | ||
| } | ||
|
|
||
| console.log(build(5)) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function convertToCamelCase(input) { | ||
| var words = input.split(/[-_]/); // Разделить строку по тире или нижнему подчеркиванию | ||
| for (var i = 1; i < words.length; i++) { | ||
| words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase(); | ||
| } // Соединить слова, пропустив разделители | ||
| var camelCase = words.join(""); | ||
| return camelCase; | ||
| } | ||
|
|
||
| // Пример использования | ||
| var input = "hello-world"; | ||
| var camelCase = convertToCamelCase(input); | ||
| console.log(camelCase); // Выводит "helloWorld" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| function transformString(str) { | ||
| let lowerCaseStr = str.toLowerCase(); | ||
| let result = ""; | ||
| for (let i = 0; i < lowerCaseStr.length; i++) { | ||
| let currentChar = lowerCaseStr[i]; | ||
| let count = lowerCaseStr.split(currentChar).length - 1; | ||
| if (count === 1) { | ||
| result += "("; | ||
| } else { | ||
| result += ")"; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // Пример использования: | ||
| let inputString = "din"; | ||
| let transformedString = transformString(inputString); | ||
| console.log(transformedString); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function findMissingLetter(array) { | ||
| for (let i = 0; i < array.length - 1; i++) { | ||
| if (array[i + 1].charCodeAt(0) - array[i].charCodeAt(0) > 1) { | ||
| return String.fromCharCode(array[i].charCodeAt(0) + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //Пример использования: | ||
| console.log(findMissingLetter(['a', 'b', 'c', 'e'])); | ||
| console.log(findMissingLetter(['O','Q','R','S'])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| function flattenMap(map, parentKey = '') { | ||
| let flatMap = {}; | ||
|
|
||
| for (let key in map) { | ||
| if (typeof map[key] === 'object') { | ||
| let nestedMap = flattenMap(map[key], parentKey + key + '/'); | ||
| flatMap = { ...flatMap, ...nestedMap }; | ||
| } else { | ||
| flatMap[parentKey + key] = map[key]; | ||
| } | ||
| } | ||
|
|
||
| return flatMap; | ||
| } | ||
|
|
||
|
|
||
| let hierarchicalMap = { | ||
| prop1: 'value1', | ||
| prop2: { | ||
| nestedProp1: 'value2', | ||
| nestedProp2: { | ||
| deeplyNestedProp: 'value3' | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let flatMap = flattenMap(hierarchicalMap); | ||
| console.log(flatMap); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class Node { | ||
| constructor(value, left=null, right=null) { | ||
| this.value = value; | ||
| this.left = left; | ||
| this.right = right; | ||
| } | ||
| } | ||
|
|
||
| class BinaryTree { | ||
| constructor(root) { | ||
| this.root = root; | ||
| } | ||
|
|
||
| maxsum() { | ||
| // Рекурсивная функция для обхода дерева и подсчета максимальной суммы | ||
| const traverse = (node, currentSum) => { | ||
| if (node === null) { | ||
| return currentSum; | ||
| } | ||
|
|
||
| // Вычисляем сумму для левого и правого поддерева | ||
| const leftSum = traverse(node.left, currentSum + node.value); | ||
| const rightSum = traverse(node.right, currentSum + node.value); | ||
|
|
||
| // Возвращаем максимальную сумму из левого и правого поддерева | ||
| return Math.max(leftSum, rightSum); | ||
| }; | ||
|
|
||
| // Начинаем обход с корневого узла и текущей суммой 0 | ||
| return traverse(this.root, 0); | ||
| } | ||
| } | ||
|
|
||
| // Пример использования | ||
| const leaf1 = new Node(4); | ||
| const leaf2 = new Node(3); | ||
| const leaf3 = new Node(5); | ||
| const leaf4 = new Node(6); | ||
| const node3 = new Node(2, leaf1, leaf2); | ||
| const node2 = new Node(1, leaf3, leaf4); | ||
| const root = new Node(0, node2, node3); | ||
|
|
||
| const tree = new BinaryTree(root); | ||
| console.log(tree.maxsum()); //должна вывести: 11 (0 -> 1 -> 5 -> 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| function mergeArrays(numbers, letters) { | ||
| const mergedArray = []; | ||
| const maxLength = Math.max(numbers.length, letters.length); | ||
| for (let i = 0; i < maxLength; i++) { | ||
| // Если есть элемент в числовом массиве на данной позиции, добавляем его в объединенный массив | ||
| if (i < numbers.length) { | ||
| mergedArray.push(numbers[i]); | ||
| } | ||
|
|
||
| // Если есть элемент в массиве с буквами на данной позиции, добавляем его в объединенный массив | ||
| if (i < letters.length) { | ||
| mergedArray.push(letters[i]); | ||
| } | ||
| } | ||
|
|
||
| return mergedArray; | ||
| } | ||
|
|
||
| // Пример использования | ||
| const numbers = [1, 2, 3, 4, 5]; | ||
| const letters = ['a', 'b', 'c', 'd', 'e', 'f']; | ||
|
|
||
| const mergedArray = mergeArrays(numbers, letters); | ||
| console.log(mergedArray); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function moveZerosToEnd(arr) { | ||
| let zeros = []; | ||
| const nonZeros = arr.filter((element) => { | ||
| if (element === 0) { | ||
| zeros.push(element); | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
|
|
||
| return nonZeros.concat(zeros); | ||
| } | ||
|
|
||
| // Пример использования: | ||
|
|
||
| const inputArray = [false, 0, 1, "hello", 0, 3, "world"]; | ||
| const resultArray = moveZerosToEnd(inputArray); | ||
| console.log(resultArray); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| function getPermutationsWithoutDuplicates(string) { | ||
| const result = []; | ||
| // Проверка на непустую входную строку | ||
| if (!string || typeof string !== 'string') { | ||
| throw new Error('Invalid input. Please provide a non-empty string.'); | ||
| } | ||
| function permute(str, prefix = '') { | ||
| if (str.length === 1) { | ||
| result.push(prefix + str); | ||
| return; | ||
| } | ||
|
|
||
| for (let i = 0; i < str.length; i++) { | ||
| const currentChar = str[i]; | ||
| const remainingChars = str.slice(0, i) + str.slice(i + 1); | ||
|
|
||
| // Проверка на дубликаты | ||
| if (str.indexOf(currentChar) !== i) { | ||
| continue; | ||
| } | ||
|
|
||
| permute(remainingChars, prefix + currentChar); | ||
| } | ||
| } | ||
|
|
||
| permute(string); | ||
|
|
||
| // Удаление дубликатов | ||
| const uniquePermutations = [...new Set(result)]; | ||
|
|
||
| return uniquePermutations; | ||
| } | ||
|
|
||
| // Пример использования | ||
| const inputString1 = 'abc'; | ||
| const permutations = getPermutationsWithoutDuplicates(inputString1); | ||
| console.log(permutations); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| function productFib(prod) { | ||
| let [a, b] = [0, 1]; | ||
| while (a * b < prod) { | ||
| [a, b] = [b, a + b]; | ||
| } | ||
| return [a, b, a * b === prod]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| function modifyString(input) { | ||
| const words = input.split(' '); | ||
| const modifiedWords = words.map(word => { | ||
| const firstLetter = word.charAt(0); | ||
| const restOfWord = word.slice(1); | ||
| const modifiedWord = restOfWord + firstLetter + 'ау'; | ||
| return modifiedWord; | ||
| }); | ||
| const result = modifiedWords.join(' \t'); | ||
| return result; | ||
| } | ||
|
|
||
| // Пример использования | ||
| const inputString12 = 'привет всем'; | ||
| const modifiedString = modifyString(inputString12); | ||
| console.log(modifiedString); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| function snail(arr) { | ||
| const n = arr.length; | ||
| const result = []; | ||
| if (n !== arr[0].length) { | ||
| return null; | ||
| } | ||
| let row = 0, col = 0; | ||
| let count = n; | ||
| while (count > 0) { | ||
| for (let i = 0; i < count; i++) { | ||
| result.push(arr[row][col++]); | ||
| } | ||
| col--; | ||
| row++; | ||
| for (let i = 0; i < count - 1; i++) { | ||
| result.push(arr[row++][col]); | ||
| } | ||
| row--; | ||
| col--; | ||
| for (let i = 0; i < count - 1; i++) { | ||
| result.push(arr[row][col--]); | ||
| } | ||
| col++; | ||
| row--; | ||
| for (let i = 0; i < count - 2; i++) { | ||
| result.push(arr[row--][col]); | ||
| } | ||
| row++; | ||
| col++; | ||
| count -= 2; | ||
| } | ||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| function recursiveSum(num) { | ||
| if (num < 10) { | ||
| return num; | ||
| } | ||
| let digitsSum = 0; | ||
| while (num > 0) { | ||
| digitsSum += num % 10; | ||
| num = Math.floor(num / 10); | ||
| } | ||
| return recursiveSum(digitsSum); | ||
| } | ||
|
|
||
| // Пример использования | ||
| const number = 123456789; | ||
| const result = recursiveSum(number); | ||
| console.log("Рекурсивная сумма числа", number, "до однозначного значения равна", result); | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не в том каталоге размещен код - размещайте внутри саги, ну и не js а ts