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 + lab1 + lab2 + tests + hard polymorphism #73

Open
wants to merge 10 commits into
base: Spiridonova_Polina_Alekseevna
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
17 changes: 17 additions & 0 deletions codewars/Adding Big Numbers/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function add(a, b) {
let result = "";
let carry = 0;

while (a.length < b.length) a = "0" + a;
while (b.length < a.length) b = "0" + b;

for (let i = a.length - 1; i >= 0; i--) {
let sum = +a[i] + +b[i] + carry;
result = (sum % 10) + result;
carry = Math.floor(sum / 10);
}

if (carry) result = carry + result;

return result;
}
17 changes: 17 additions & 0 deletions codewars/Anagram difference/anagram_difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def anagram_difference(w1, w2):
count_w1 = {}
count_w2 = {}

for char in w1:
count_w1[char] = count_w1.get(char, 0) + 1

for char in w2:
count_w2[char] = count_w2.get(char, 0) + 1

deletions = 0
all_chars = set(count_w1.keys()).union(count_w2.keys())

for char in all_chars:
deletions += abs(count_w1.get(char, 0) - count_w2.get(char, 0))

return deletions
9 changes: 9 additions & 0 deletions codewars/Array Deep Count/deep_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def deep_count(a):
sum_total = 0

for i in a:
sum_total += 1
if isinstance(i, list):
sum_total += deep_count(i)

return sum_total
7 changes: 7 additions & 0 deletions codewars/Build Tower/tower_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def tower_builder(n_floors):
tower = []
for i in range(n_floors):
stars = "*" * (2 * i + 1)
spaces = " " * (n_floors - i - 1)
tower.append(spaces + stars + spaces)
return tower
6 changes: 6 additions & 0 deletions codewars/Convert string to camel case/toCamelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function toCamelCase(str) {
if (str === "") return str;

const words = str.split(/[-_]/);
return words[0] + words.slice(1).map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
}
12 changes: 12 additions & 0 deletions codewars/Duplicate Encoder/duplicate_encode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def duplicate_encode(word):
word = word.lower()
count = {}

for char in word:
count[char] = count.get(char, 0) + 1

result = ""
for char in word:
result += "(" if count[char] == 1 else ")"

return result
7 changes: 7 additions & 0 deletions codewars/Find the missing letter/find_missing_letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def find_missing_letter(chars):
for i in range(len(chars) - 1):
curr_char = ord(chars[i])
next_char = ord(chars[i + 1])

if next_char != curr_char + 1:
return chr(curr_char + 1)
17 changes: 17 additions & 0 deletions codewars/Flatten a Nested Map/flattenMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function flattenMap(input) {
const result = {};

function flatten(obj, prefix = '') {
for (const key in obj) {
const newKey = prefix ? `${prefix}/${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
flatten(obj[key], newKey);
} else {
result[newKey] = obj[key];
}
}
}

flatten(input);
return result;
}
27 changes: 27 additions & 0 deletions codewars/Fun with tree - max sum/max_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from preloaded import TreeNode

def max_sum(root: TreeNode) -> int:
if root is None:
return 0

def dfs(node):
if node is None:
return float('-inf')

if node.left is None and node.right is None:
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))
22 changes: 22 additions & 0 deletions codewars/Linked Lists - Sorted Insert/sortedInsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function Node(data) {
this.data = data;
this.next = null;
}

function sortedInsert(head, data) {
const newNode = new Node(data);

if (!head || data < head.data) {
newNode.next = head;
return newNode;
}

let current = head;
while (current.next && current.next.data < data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;

return head;
}
18 changes: 18 additions & 0 deletions codewars/Merge two arrays/mergeArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function mergeArrays(a, b) {
let maxLength = Math.max(a.length, b.length);
let totalLength = maxLength * 2;
let arraysToMerge = [a, b];
let mergedResult = [];

for (let i = 0; i < totalLength; i++) {
let currentArrayIndex = i % 2;
let currentElementIndex = Math.floor(i / 2);
let value = arraysToMerge[currentArrayIndex][currentElementIndex];

if (value !== undefined) {
mergedResult.push(value);
}
}

return mergedResult;
}
17 changes: 17 additions & 0 deletions codewars/Moving Zeros To The End/moveZeros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function moveZeros(arr) {
const zeros = [];
const nonZeros = [];

for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
zeros.push(arr[i]);
} else {
nonZeros.push(arr[i]);
}
}
for (let j = 0; j < zeros.length; j++) {
nonZeros.push(zeros[j]);
}

return nonZeros;
}
14 changes: 14 additions & 0 deletions codewars/Permutations/permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function permutations(string) {
if (string.length <= 1) return [string];

let result = [];
for (let i = 0; i < string.length; i++) {
let char = string[i];
if (string.indexOf(char) === i) {
let remainingStr = string.slice(0, i) + string.slice(i + 1);
result = result.concat(permutations(remainingStr).map(perm => char + perm));
}
}

return result;
}
12 changes: 12 additions & 0 deletions codewars/Product of consecutive Fib numbers/productFib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function productFib(prod) {
let fib1 = 1;
let fib2 = 1;

while (fib1 * fib2 < prod) {
let nextFib = fib1 + fib2;
fib1 = fib2;
fib2 = nextFib;
}

return [fib1, fib2, fib1 * fib2 === prod];
}
18 changes: 18 additions & 0 deletions codewars/Simple Pig Latin/pigIt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function pigIt(str) {
str = str.split(" ");
let lastLetters = "ay";
let result = [];

for (let word of str) {
if (word === "!" || word === "?") {
result.push(word);
} else {
word = word.split("");
let firstLetter = word.splice(0, 1)[0];
word.push(firstLetter, lastLetters);
result.push(word.join(""));
}
}

return result.join(" ");
}
29 changes: 29 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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;
};
12 changes: 12 additions & 0 deletions codewars/Sum of Digits - Digital Root/digital_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def digital_root(n):
str_n = str(n)
result = 0
for char in str_n:
num = int(char)
result += num

str_result = str(result)
if len(str_result) >= 2:
return digital_root(str_result)

return result
18 changes: 18 additions & 0 deletions codewars/Sum of Intervals/sumIntervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function sumIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);

let sum = 0;
let currentEnd = -Infinity;

for (const [start, end] of intervals) {
if (start > currentEnd) {
sum += end - start;
} else if (end > currentEnd) {
sum += end - currentEnd;
}
currentEnd = Math.max(currentEnd, end);
}

return sum;
}

11 changes: 11 additions & 0 deletions codewars/Sum of pairs/sumPairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function sumPairs(ints, s) {
let seen = new Set();
for (let num of ints) {
let complement = s - num;
if (seen.has(complement)) {
return [complement, num];
}
seen.add(num);
}
return undefined;
}
19 changes: 19 additions & 0 deletions codewars/Tic-Tac-Toe Checker/isSolved.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function isSolved(board) {
const checkLine = (a, b, c) => a !== 0 && a === b && a === c ? a : null;

for (let i = 0; i < 3; i++) {
if (checkLine(board[i][0], board[i][1], board[i][2])) return board[i][0];
if (checkLine(board[0][i], board[1][i], board[2][i])) return board[0][i];
}

if (checkLine(board[0][0], board[1][1], board[2][2])) return board[0][0];
if (checkLine(board[0][2], board[1][1], board[2][0])) return board[0][2];

for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === 0) return -1;
}
}

return 0;
}
16 changes: 16 additions & 0 deletions codewars/Valid Parentheses/validParentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function validParentheses(parens) {
let balance = 0;

for (let char of parens) {
if (char === "(") {
balance++;
} else if (char === ")") {
balance--;
if (balance < 0) {
return false;
}
}
}

return balance === 0;
}
4 changes: 4 additions & 0 deletions codewars/Where my anagrams at/anagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function anagrams(word, words) {
const sortedWord = word.split("").sort().join("");
return words.filter((w) => w.split("").sort().join("") === sortedWord);
}
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