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, class and tests, polymorphizm and test #7

Open
wants to merge 5 commits into
base: Aminov_Nikita_Andreevich
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
34 changes: 34 additions & 0 deletions codewars/Adding Big Numbers/Adding Big Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function add(a, b) {
const numLen = Math.max(a.length, b.length);
a = a.padStart(numLen, "0");
b = b.padStart(numLen, "0");

let res = [];

let remainder = false;
for(let i = a.length - 1; i >= 0; i--) {
let sum = Number(a[i]) + Number(b[i]);

if(remainder) {
sum++;
remainder = false;
}

sum = sum.toString();

if(sum.length > 1) {
remainder = true;
sum = sum.slice(1);
}

res.push(sum);
}

if(remainder) {
res.push("1");
}

res.reverse();
res = res.join("");
return res;
}
13 changes: 13 additions & 0 deletions codewars/Anagram difference/Anagram difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from collections import Counter

def anagram_difference(w1, w2):

count1 = Counter(w1)
count2 = Counter(w2)
total_removals = 0

all_letters = set(count1.keys()).union(set(count2.keys()))
for letter in all_letters:
total_removals += abs(count1.get(letter, 0) - count2.get(letter, 0))

return total_removals
10 changes: 10 additions & 0 deletions codewars/Array Deep Count/Array Deep Count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function deepCount(a){
let len = 0;
for(let i = 0; i < a.length; i++) {
if(Array.isArray(a[i])) {
len += deepCount(a[i]);
}
len++;
}
return len;
}
11 changes: 11 additions & 0 deletions codewars/Build Tower/Build Tower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def tower_builder(n_floors):
tower = []
width = 2 * n_floors - 1
for i in range(n_floors):
stars = 2 * i + 1
spaces = (width - stars) // 2

floor = ' ' * spaces + '*' * stars + ' ' * spaces
tower.append(floor)

return tower
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def to_camel_case(text):
if not text:
return text

words = text.replace('-', ' ').replace('_', ' ').split()

if words[0][0].isupper():
words[0] = words[0].capitalize()
else:
words[0] = words[0].lower()

camel_case_words = [words[0]] + [word.capitalize() for word in words[1:]]

return ''.join(camel_case_words)
17 changes: 17 additions & 0 deletions codewars/Duplicate Encoder/Duplicate Encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def duplicate_encode(word):
word=word.lower()

char_count={}
for char in word:
if char in char_count:
char_count[char]+=1
else:
char_count[char]=1

result=""
for char in word:
if char_count[char]==1:
result+="("
else:
result+=")"
return result
8 changes: 8 additions & 0 deletions codewars/Find the missing letter/Find the missing letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def find_missing_letter(chars):
for i in range(1,len(chars)):
tekush_char_code=ord(chars[i])
next_char_code=ord(chars[i-1])

if tekush_char_code-next_char_code>1:
return chr(next_char_code+1)
return
15 changes: 15 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,15 @@
function flattenMap(map) {
let res = {};
for(let key in map) {
if(typeof map[key] === 'object' && !Array.isArray(map[key]) && map[key] !== null) {
let flatMap = flattenMap(map[key]);
for(let i = 0; i < Object.keys(flatMap).length; i++) {
res[key + "/" + Object.keys(flatMap)[i]] = Object.values(flatMap)[i];
}
continue;
}
res[key] = map[key];
}

return res;
}
30 changes: 30 additions & 0 deletions codewars/Fun with tree - max sum/Fun with trees max sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function maxSum(root) {
if(root == null) {
return 0;
}

return checkTheTree(root);
}

function checkTheTree(node) {
if(node.left == null && node.right == null) {
return node.value;
}

let finSum = 0;

if(node.left != null && node.right == null) {
finSum = checkTheTree(node.left);
}
else if(node.right != null && node.left == null) {
finSum = checkTheTree(node.right);
}
else {
let sum1 = checkTheTree(node.left);
let sum2 = checkTheTree(node.right);

finSum = Math.max(sum1, sum2);
}

return finSum + node.value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function Node(data) {
this.data = data;
this.next = null;
}

function sortedInsert(head, data) {
let newNode = new Node(data);
newNode.next = head;

if(head == null) {
return newNode;
}

let currentNode = newNode;

while(true) {
if(currentNode.next != null && currentNode.data > currentNode.next.data) {
let oldData = currentNode.data;
currentNode.data = currentNode.next.data;
currentNode.next.data = oldData;

currentNode = currentNode.next;
continue;
}

return newNode;
}
}
17 changes: 17 additions & 0 deletions codewars/Merge two arrays/Merge two arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function mergeArrays(a, b) {
let res = [];

let len = Math.max(a.length, b.length);

for(let i = 0; i < len; i++) {
if(i < a.length) {
res.push(a[i]);
}

if(i < b.length) {
res.push(b[i]);
}
}

return res;
}
8 changes: 8 additions & 0 deletions codewars/Moving Zeros To The End/Moving Zeros To The End.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def move_zeros(lst):

non_zeros = [x for x in lst if x != 0]

zero_count = len(lst) - len(non_zeros)

lst = non_zeros + [0] * zero_count
return lst
15 changes: 15 additions & 0 deletions codewars/Permutations/So Many Permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def permutations(s):
if len(s) == 1:
return [s]

result_set = set()

for i in range(len(s)):
char = s[i]
remaining_chars = s[:i] + s[i+1:]
remaining_permutations = permutations(remaining_chars)

for perm in remaining_permutations:
result_set.add(char + perm)

return list(result_set)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def product_fib(prod):

a, b = 0, 1

while a * b < prod:
a, b = b, a + b
if a * b == prod:
return [a, b, True]
else:
return [a, b, False]

pass
12 changes: 12 additions & 0 deletions codewars/Simple Pig Latin/Simple Pig Latin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import string
def pig_it(text):
words = text.split()
pig_latin_words = []
for word in words:
if word in string.punctuation:
pig_latin_words.append(word)
else:
pig_latin_word = word[1:] + word[0] + 'ay'
pig_latin_words.append(pig_latin_word)

return ' '.join(pig_latin_words)
29 changes: 29 additions & 0 deletions codewars/Snail/Snail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def snail(array):
if not array or not array[0]:
return []

result = []
top, bottom = 0, len(array) - 1
left, right = 0, len(array[0]) - 1

while top <= bottom and left <= right:

for i in range(left, right + 1):
result.append(array[top][i])
top += 1

for i in range(top, bottom + 1):
result.append(array[i][right])
right -= 1
if top <= bottom:
for i in range(right, left - 1, -1):
result.append(array[bottom][i])
bottom -= 1

if left <= right:
for i in range(bottom, top - 1, -1):
result.append(array[i][left])
left += 1

return result
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def digital_root(n):
while n >= 10:
n = sum(int(digit) for digit in str(n))
return n
23 changes: 23 additions & 0 deletions codewars/Sum of Intervals/Sum of Intervals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def sum_of_intervals(intervals):

if not intervals:
return 0

intervals.sort(key=lambda x: x[0])

merged_intervals = []
current_start, current_end = intervals[0]

for start, end in intervals[1:]:
if start <= current_end:
current_end = max(current_end, end)
else:
merged_intervals.append((current_start, current_end))
current_start, current_end = start, end

merged_intervals.append((current_start, current_end))

total_length = sum(end - start for start, end in merged_intervals)

return total_length
pass
16 changes: 16 additions & 0 deletions codewars/Sum of pairs/Sum of Pairs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function sumPairs(ints: number[], s: number): [number, number] | void {
let pairs = new Map<number, number>();
let minIndex: number = Number.MAX_SAFE_INTEGER;

for(let i = 0; i < ints.length; i++) {
let secondNum: number = s - ints[i];

if(pairs.has(secondNum)) {
return [secondNum, ints[i]];
}

pairs.set(ints[i], secondNum);
}

return undefined;
}
23 changes: 23 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,23 @@
def is_solved(board):

for i in range(3):

if board[i][0] == board[i][1] == board[i][2] != 0:
return board[i][0]

if board[0][i] == board[1][i] == board[2][i] != 0:
return board[0][i]


if board[0][0] == board[1][1] == board[2][2] != 0:
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] != 0:
return board[0][2]

for row in board:
if 0 in row:
return -1


return 0
pass
Loading