Skip to content

Commit

Permalink
Merge pull request #14 from GoidGoidGoida/Gorjunov_Aleksandr_Alekseevich
Browse files Browse the repository at this point in the history
CodeWars solutions
  • Loading branch information
jskonst authored Oct 22, 2024
2 parents 58b5956 + 737c4f2 commit 7e29f3f
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 0 deletions.
34 changes: 34 additions & 0 deletions codewars/Adding Big Numbers/1.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;
}
14 changes: 14 additions & 0 deletions codewars/Anagram difference/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from collections import Counter

def anagram_difference(word1, word2):
count1 = Counter(word1)
count2 = Counter(word2)

removals = 0

all_chars = set(count1.keys()).union(set(count2.keys()))

for char in all_chars:
removals += abs(count1.get(char, 0) - count2.get(char, 0))

return removals
10 changes: 10 additions & 0 deletions codewars/Array Deep Count/1.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/1.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
8 changes: 8 additions & 0 deletions codewars/Convert string to camel case/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def to_camel_case(text):
if not text:
return text

words = text.replace('-', ' ').replace('_', ' ').split()
camel_case_words = [words[0]] + [word.capitalize() for word in words[1:]]

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

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

result = ""
for char in s:
if char_count[char] == 1:
result += "("
else:
result += ")"

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

if next_ascii - current_ascii > 1:
return chr(current_ascii + 1)

return None
15 changes: 15 additions & 0 deletions codewars/Flatten a Nested Map/1.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;
}
17 changes: 17 additions & 0 deletions codewars/Fun with tree - max sum/1.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;
}
28 changes: 28 additions & 0 deletions codewars/Linked Lists - Sorted Insert/1.js
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/1.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;
}
9 changes: 9 additions & 0 deletions codewars/Moving Zeros To The End/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def find_missing_letter(letters):
for i in range(len(letters) - 1):
current_ascii = ord(letters[i])
next_ascii = ord(letters[i + 1])

if next_ascii - current_ascii > 1:
return chr(current_ascii + 1)

return None
6 changes: 6 additions & 0 deletions codewars/Permutations/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import itertools

def permutations(s):
perm = itertools.permutations(s)
unique_permutations = set(''.join(p) for p in perm)
return list(unique_permutations)
10 changes: 10 additions & 0 deletions codewars/Product of consecutive Fib numbers/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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]
14 changes: 14 additions & 0 deletions codewars/Simple Pig Latin/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import re

def pig_it(text):
words = re.split(r'(\W+)', text)

transformed_words = []
for word in words:
if re.match(r'[a-zA-Z]', word):
transformed_word = word[1:] + word[0] + 'ay'
transformed_words.append(transformed_word)
else:
transformed_words.append(word)

return ''.join(transformed_words)
28 changes: 28 additions & 0 deletions codewars/Snail/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
4 changes: 4 additions & 0 deletions codewars/Sum of Digits - Digital Root/1.py
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
20 changes: 20 additions & 0 deletions codewars/Sum of Intervals/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function sumOfIntervals(intervals: [number, number][]) {
let res: number = 0;
for(let i = 0; i < intervals.length - 1; i++) {
for(let j = i + 1; j < intervals.length; j++) {
if(intervals[i][1] > intervals[j][0] && intervals[i][0] < intervals[j][1]) {
let newIntStart: number = Math.min(intervals[i][0], intervals[j][0]);
let newIntEnd: number = Math.max(intervals[i][1], intervals[j][1]);
intervals[i][0] = newIntStart;
intervals[i][1] = newIntEnd;
intervals.splice(j, 1);
j = i;
}
}
}

for(let i = 0; i < intervals.length; i++) {
res += intervals[i][1] - intervals[i][0];
}
return res;
}
8 changes: 8 additions & 0 deletions codewars/Sum of pairs/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def sum_pairs(ints, s):
seen = {}
for i, num in enumerate(ints):
complement = s - num
if complement in seen:
return [complement, num]
seen[num] = i
return None
17 changes: 17 additions & 0 deletions codewars/Tic-Tac-Toe Checker/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
11 changes: 11 additions & 0 deletions codewars/Valid Parentheses/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def valid_parentheses(s):
stack = []
for char in s:
if char == '(':
stack.append(char)
elif char == ')':
if stack and stack[-1] == '(':
stack.pop()
else:
return False
return len(stack) == 0
12 changes: 12 additions & 0 deletions codewars/Where my anagrams at/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def anagrams(word, words):
sorted_word = sorted(word)

anagram_list = []

for w in words:
sorted_w = sorted(w)

if sorted_w == sorted_word:
anagram_list.append(w)

return anagram_list

0 comments on commit 7e29f3f

Please sign in to comment.