-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from GoidGoidGoida/Gorjunov_Aleksandr_Alekseevich
CodeWars solutions
- Loading branch information
Showing
22 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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; | ||
} |
This file contains 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,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 |
This file contains 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,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; | ||
} |
This file contains 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 @@ | ||
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 |
This file contains 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,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) |
This file contains 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 @@ | ||
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 |
This file contains 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,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 |
This file contains 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 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; | ||
} |
This file contains 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 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; | ||
} |
This file contains 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 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; | ||
} | ||
} |
This file contains 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 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; | ||
} |
This file contains 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,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 |
This file contains 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,6 @@ | ||
import itertools | ||
|
||
def permutations(s): | ||
perm = itertools.permutations(s) | ||
unique_permutations = set(''.join(p) for p in perm) | ||
return list(unique_permutations) |
This file contains 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,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] |
This file contains 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,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) |
This file contains 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 @@ | ||
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 |
This file contains 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,4 @@ | ||
def digital_root(n): | ||
while n >= 10: | ||
n = sum(int(digit) for digit in str(n)) | ||
return n |
This file contains 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,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; | ||
} |
This file contains 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,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 |
This file contains 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 @@ | ||
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 |
This file contains 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 @@ | ||
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 |
This file contains 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,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 |