Skip to content
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

Codewars + Func + Classes + Hard Polymorphism #78

Open
wants to merge 5 commits into
base: Korotkov_Danila_Maksimovich
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions codewars/Adding Big Numbers/Adding Big Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function add(num1, num2) {
let sum = "";
let k = 0;
while (num1.length < num2.length) num1 = "0" + num1;
while (num2.length < num1.length) num2 = "0" + num2;
for (let i = num1.length - 1; i >= 0; i--) {
let digitSum = +num1[i] + +num2[i] + k;
sum = (digitSum % 10) + sum;
k = Math.floor(digitSum / 10);}
if (k) sum = k + sum;
return sum;
}
9 changes: 9 additions & 0 deletions codewars/Anagram difference/Anagram difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def anagram_difference(w1, w2):
count_w1 = [0] * 26
count_w2 = [0] * 26
for char in w1:
count_w1[ord(char) - ord('a')] += 1
for char in w2:
count_w2[ord(char) - ord('a')] += 1
deletions = sum(abs(count_w1[i] - count_w2[i]) for i in range(26))
return deletions
7 changes: 7 additions & 0 deletions codewars/Array Deep Count/Array_Deep_Count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def deep_count(x):
summ = 0
for i in x:
summ += 1
if isinstance(i, list):
summ += deep_count(i)
return summ
9 changes: 9 additions & 0 deletions codewars/Build Tower/Build_Tower.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function towerBuilder(floors: number): string[] {
const tower: string[] = [];
for (let i = 0; i < floors; i++) {
const stars = '*'.repeat(2 * i + 1);
const spaces = ' '.repeat(floors - i - 1);
tower.push(spaces + stars + spaces);
}
return tower;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function toCamelCase(input: string): string {
if (input === "") return input;
const words = input.split(/[-_]/);
const transformedWords = words.map((word, index) => {
if (index === 0) return word;
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
});
return transformedWords.join("");
}
12 changes: 12 additions & 0 deletions codewars/Duplicate Encoder/Duplicate_Encoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function duplicateEncode(word: string): string {
const lowerCaseWord = word.toLowerCase();
const charCount: { [key: string]: number } = {};
for (const char of lowerCaseWord) {
charCount[char] = (charCount[char] || 0) + 1;
}
let result = '';
for (const char of lowerCaseWord) {
result += charCount[char] === 1 ? '(' : ')';
}
return result;
}
7 changes: 7 additions & 0 deletions codewars/Find the missing letter/Find_the_missing_letter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function findMissingLetter(array: string[]): string {
for (let i = 0; i < array.length - 1; i++) {
if (array[i].charCodeAt(0) + 1 !== array[i + 1].charCodeAt(0)) {
return String.fromCharCode(array[i].charCodeAt(0) + 1);
}}
return '';
}
14 changes: 14 additions & 0 deletions codewars/Flatten a Nested Map/Flatten a Nested Map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function flattenMap(input) {
const result = {};
function traverse(current, path) {
for (const key in current) {
const newPath = path ? `${path}/${key}` : key;
const value = current[key];
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
traverse(value, newPath);
} else {
result[newPath] = value;
}}}
traverse(input, '');
return result;
}
23 changes: 23 additions & 0 deletions codewars/Fun with tree - max sum/Fun with trees_max sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class TreeNode:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right

def max_sum(root: TreeNode) -> int:
if root is None:
return 0
def dfs(node):
if not node:
return float('-inf')
if not node.left and not node.right:
return node.value
left_sum = dfs(node.left)
right_sum = dfs(node.right)
return node.value + max(left_sum, right_sum)
return dfs(root)
root = TreeNode(
5,
TreeNode(4, TreeNode(-80), TreeNode(-60)),
TreeNode(10, TreeNode(-90)))
print(max_sum(root))
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None
def sorted_insert(head, data):
new_node = Node(data)
if head is None or data < head.data:
new_node.next = head
return new_node
current = head
while current.next is not None and current.next.data < data:
current = current.nex
new_node.next = current.next
current.next = new_node
return head
12 changes: 12 additions & 0 deletions codewars/Merge two arrays/Merge two arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function mergeArrays(array1, array2) {
const maxLength = Math.max(array1.length, array2.length);
const mergedArray = [];
for (let i = 0; i < maxLength; i++) {
if (i < array1.length) {
mergedArray.push(array1[i]);
}
if (i < array2.length) {
mergedArray.push(array2[i]);
}}
return mergedArray;
}
11 changes: 11 additions & 0 deletions codewars/Moving Zeros To The End/Moving_Zeros_To_The_End.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function moveZeros(array) {
let nonZeroElements = [];
let zeroElements = [];
for (let element of array) {
if (element === 0) {
zeroElements.push(element);
} else {
nonZeroElements.push(element);
}}
return nonZeroElements.concat(zeroElements);
}
11 changes: 11 additions & 0 deletions codewars/Permutations/So_Many_Permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function permutations(input) {
if (input.length === 1) return [input];
const res = [];
for (let i = 0; i < input.length; i++) {
const currentChar = input[i];
if (input.indexOf(currentChar) === i) {
const remainingChars = input.slice(0, i) + input.slice(i + 1);
res.push(...permutations(remainingChars).map(perm => currentChar + perm));
}}
return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function productFib(prod: number): [number, number, boolean] {
let fib1: number = 1;
let fib2: number = 1;
while (fib1 * fib2 < prod) {
const nextFib: number = fib1 + fib2;
fib1 = fib2;
fib2 = nextFib;
}
return [fib1, fib2, fib1 * fib2 === prod];
}
9 changes: 9 additions & 0 deletions codewars/Simple Pig Latin/Simple Pig Latin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function pigIt(sentence) {
const suffix = "ay";
return sentence.split(" ").map(word => {
if (/^[a-zA-Z]+$/.test(word)) {
return word.slice(1) + word[0] + suffix;
}
return word;
}).join(" ");
}
22 changes: 22 additions & 0 deletions codewars/Snail/Snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
snail = function (array) {
if (array.length === 0) return [];
let 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;
};
10 changes: 10 additions & 0 deletions codewars/Sum of Digits - Digital Root/Sum of DigitsDigital Root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def digital_root(n):
k=n
b=0
while len(str(k))>1:
b=0
k=str(k)
for i in k:
b+=int(i)
k=b
return (k)
14 changes: 14 additions & 0 deletions codewars/Sum of Intervals/Sum of Intervals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function sumOfIntervals(intervals: [number, number][]): number {
intervals.sort((a, b) => a[0] - b[0]);
let totalLength = 0;
let lastEnd = -Infinity;
for (const [start, end] of intervals) {
if (start > lastEnd) {
totalLength += end - start;
} else if (end > lastEnd) {
totalLength += end - lastEnd;
}
lastEnd = Math.max(lastEnd, end);
}
return totalLength;
}
11 changes: 11 additions & 0 deletions codewars/Sum of pairs/Sum of Pairs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function sumPairs(ints: number[], sum: number): [number, number] | undefined {
const seen = new Set<number>();
for (const num of ints) {
const complement = sum - num;
if (seen.has(complement)) {
return [complement, num];
}
seen.add(num);
}
return undefined;
}
17 changes: 17 additions & 0 deletions codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def is_solved(board):
def check_line(a, b, c):
return a if a != 0 and a == b and a == c else None
for i in range(3):
if check_line(board[i][0], board[i][1], board[i][2]):
return board[i][0]
if check_line(board[0][i], board[1][i], board[2][i]):
return board[0][i]
if check_line(board[0][0], board[1][1], board[2][2]):
return board[0][0]
if check_line(board[0][2], board[1][1], board[2][0]):
return board[0][2]
for i in range(3):
for j in range(3):
if board[i][j] == 0:
return -1
return 0
10 changes: 10 additions & 0 deletions codewars/Valid Parentheses/Valid_Parenthesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def valid_parentheses(parens):
count = 0
for char in parens:
if char == "(":
count += 1
elif char == ")":
count -= 1
if count < 0:
return False
return count == 0
4 changes: 4 additions & 0 deletions codewars/Where my anagrams at/Where my anagrams at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function anagrams(targetWord, wordList) {
const sortedTarget = targetWord.split("").sort().join("");
return wordList.filter(word => word.split("").sort().join("") === sortedTarget);
}
2 changes: 1 addition & 1 deletion rpgsaga/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ test_saga:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
changes:
- rpgsaga/**/*
- rpgsaga/**/*
1 change: 0 additions & 1 deletion rpgsaga/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/saga/src/index.ts",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
Expand Down
5 changes: 5 additions & 0 deletions rpgsaga/saga/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export default [{
}, {
selector: "typeProperty",
format: ["snake_case", "camelCase"],
}, {
selector: ['memberLike'],
modifiers: ['protected'],
format: ['camelCase'],
leadingUnderscore: 'allow',
}],
},
}];
Loading
Loading