-
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 #33 from karetkaaa/karetina_labs_TP
Code Wars
- Loading branch information
Showing
19 changed files
with
304 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,20 @@ | ||
function add(n, m) { | ||
while (n.length < m.length) { | ||
n = '0' + n; | ||
} | ||
while (m.length < n.length) { | ||
m = '0' + m; | ||
} | ||
let big_digit = 0; | ||
let result = ''; | ||
|
||
for (let i = n.length - 1; i >= 0; i--) { | ||
const sum = parseInt(n[i]) + parseInt(m[i]) + big_digit; | ||
result = (sum % 10) + result; | ||
big_digit = Math.floor(sum / 10); | ||
} | ||
if (big_digit > 0) { | ||
result = big_digit + 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,10 @@ | ||
def deep_count(n): | ||
sum = 0 | ||
for element in n: | ||
sum += 1 | ||
if isinstance(element, list) == True: | ||
sum += deep_count(element) | ||
return sum | ||
|
||
if __name__ == "__main__": | ||
print(deep_count(list(input()))) |
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 @@ | ||
def tower_builder(n_floors): | ||
tower = list() | ||
for i in range(n_floors): | ||
if i == 0: | ||
tower_sector = (" " * (n_floors - 1)) + "*" + (" " * (n_floors - 1)) | ||
tower.append(tower_sector) | ||
else: | ||
tower_sector = (" " * (n_floors - 1 - i)) + ("*" * (1 + (i * 2))) + (" " * (n_floors - 1 - i)) | ||
tower.append(tower_sector) | ||
|
||
return tower | ||
|
||
if __name__ == '__main__': | ||
print(tower_builder(int(input()))) |
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 to_camel_case(text): | ||
ctr = text.replace("_", " ").replace("-", " ") | ||
ctr = ctr.split() | ||
if len(text) == 0: | ||
return text | ||
return ctr[0] + ''.join(i.capitalize() for i in ctr[1:]) | ||
|
||
if __name__ == '__main__': | ||
print(to_camel_case(input())) |
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,16 @@ | ||
def duplicate_encode(word): | ||
word = word.lower() | ||
letter_count = {} | ||
|
||
for char in word: | ||
if char in letter_count: | ||
letter_count[char] += 1 | ||
else: | ||
letter_count[char] = 1 | ||
|
||
final_string = ''.join('(' if letter_count[char] == 1 else ')' for char in word) | ||
|
||
return final_string | ||
|
||
if __name__ == "__main__": | ||
print(duplicate_encode(input("Enter your word: "))) |
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 @@ | ||
ef find_missing_letter(chars): | ||
|
||
full_set = set(chr(i) for i in range(ord(chars[0]), ord(chars[-1]) + 1)) | ||
chars_set = set(chars) | ||
missing_chars = full_set - chars_set | ||
return missing_chars.pop() if missing_chars else "" | ||
|
||
if __name__ == "__main__": | ||
input_chars = input("Enter letters: ") | ||
print(find_missing_letter(input_chars)) |
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 @@ | ||
function flattenMap(map, path = "", result = {}) { | ||
Object.entries(map).forEach(([key, value]) => { | ||
const currPath = path ? `${path}/${key}` : key; | ||
const isObject = value !== null && typeof value === "object" && !Array.isArray(value); | ||
|
||
if (isObject) { | ||
flattenMap(value, currPath, result); | ||
} else { | ||
result[currPath] = value; | ||
} | ||
}); | ||
|
||
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 |
---|---|---|
@@ -1 +1,18 @@ | ||
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 max_sum_path(node): | ||
if node is None: | ||
return 0 | ||
left_sum = max(max_sum_path(node.left), 0) | ||
right_sum = max(max_sum_path(node.right), 0) | ||
return node.value + max(left_sum, right_sum) | ||
|
||
return max_sum_path(root) |
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,35 @@ | ||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.next = None | ||
|
||
class LinkedList: | ||
def __init__(self): | ||
self.head = None | ||
|
||
def sorted_insert(self, data): | ||
new_node = Node(data) | ||
if self.head is None or self.head.data >= new_node.data: | ||
new_node.next = self.head | ||
self.head = new_node | ||
return | ||
current = self.head | ||
while current.next is not None and current.next.data < new_node.data: | ||
current = current.next | ||
new_node.next = current.next | ||
current.next = new_node | ||
|
||
def print_list(self): | ||
current = self.head | ||
while current: | ||
print(current.data, end=" -> ") | ||
current = current.next | ||
print("None") | ||
if __name__ == "__main__": | ||
ll = LinkedList() | ||
ll.sorted_insert(5) | ||
ll.sorted_insert(3) | ||
ll.sorted_insert(8) | ||
ll.sorted_insert(1) | ||
|
||
ll.print_list() |
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 mergeArrays(n, m) { | ||
let result = []; | ||
const minLength = Math.min(n.length, m.length); | ||
for (let i = 0; i < minLength; i++) { | ||
result.push(n[i]); | ||
result.push(m[i]); | ||
} | ||
result = result.concat(n.length > m.length ? n.slice(minLength) : m.slice(minLength)); | ||
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,13 @@ | ||
def move_zeros(lst): | ||
non_zero_index = 0 | ||
for el in lst: | ||
if el != 0: | ||
lst[non_zero_index] = el | ||
non_zero_index += 1 | ||
for i in range(non_zero_index, len(lst)): | ||
lst[i] = 0 | ||
return lst | ||
|
||
if __name__ == "__main__": | ||
input_list = list(map(int, input().split())) | ||
print(move_zeros(input_list)) |
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 @@ | ||
import itertools | ||
|
||
def permutations(string): | ||
if len(string)<2: return [string] | ||
else: return sorted(list({''.join(x) for x in itertools.permutations(string)})) | ||
|
||
if __name__ == "__main__": | ||
print(permutations(input())) |
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,13 @@ | ||
def product_fib(prod): | ||
num_1, num_2 = 0, 1 | ||
|
||
while num_1 * num_2 < prod: | ||
num_1, num_2 = num_2, num_1 + num_2 | ||
|
||
if num_1 * num_2 == prod: | ||
return [num_1, num_2, True] | ||
else: | ||
return [num_1, num_2, False] | ||
|
||
if __name__ == "__main__": | ||
print(product_fib(int(input()))) |
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 pig_it(text): | ||
text = list(text.split()) | ||
for i in range(len(text)): | ||
if text[i].isalpha(): | ||
text[i] = text[i][1:] + text[i][0] + 'ay' | ||
|
||
return ' '.join(text) | ||
|
||
if __name__ == "__main__": | ||
print(pig_it(input())) |
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,23 @@ | ||
def reverse_function(snail_map): | ||
return [row[::-1] for row in snail_map[::-1]] | ||
|
||
def snail(snail_map): | ||
result = [] | ||
|
||
while snail_map: | ||
result += snail_map.pop(0) | ||
if snail_map and snail_map[0]: | ||
for row in snail_map: | ||
result.append(row.pop()) | ||
if snail_map: | ||
result += snail_map.pop()[::-1] | ||
if snail_map and snail_map[0]: | ||
for row in snail_map[::-1]: | ||
result.append(row.pop(0)) | ||
|
||
return result | ||
|
||
if __name__ == "__main__": | ||
import ast | ||
snail_map = ast.literal_eval(input()) | ||
print(snail(snail_map)) |
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 digital_root(number): | ||
while number > 9: | ||
number = sum([int(x) for x in str(number)]) | ||
|
||
return number | ||
|
||
if __name__ == "__main__": | ||
print(digital_root(int(input()))) |
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,26 @@ | ||
def sum_of_intervals(intervals): | ||
merged_intervals = merge_intervals(intervals) | ||
return sum(end - start for start, end in merged_intervals) | ||
|
||
def merge_intervals(intervals): | ||
if not intervals: | ||
return [] | ||
intervals.sort(key=lambda x: x[0]) | ||
merged = [] | ||
|
||
current_start, current_end = intervals[0] | ||
|
||
for start, end in intervals[1:]: | ||
if start > current_end: | ||
merged.append([current_start, current_end]) | ||
current_start, current_end = start, end | ||
else: | ||
current_end = max(current_end, end) | ||
|
||
merged.append([current_start, current_end]) | ||
return merged | ||
|
||
if __name__ == "__main__": | ||
import ast | ||
intervals = ast.literal_eval(input()) | ||
print(sum_of_intervals(intervals)) |
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 @@ | ||
def sum_pairs(ints, s): | ||
seen = set() | ||
for number in ints: | ||
target = s - number | ||
if target in seen: | ||
return [target, number] | ||
seen.add(number) | ||
return None | ||
|
||
if __name__ == "__main__": | ||
import ast | ||
ints = ast.literal_eval(input("Введите список чисел: ")) | ||
s = int(input("Введите сумму: ")) | ||
print(sum_pairs(ints, s)) |
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 @@ | ||
def is_solved(board): | ||
if there_are_empty_spots(board): | ||
return -1 | ||
return the_result_of_the_game(board) | ||
|
||
def there_are_empty_spots(board): | ||
for row in board: | ||
if 0 in row: | ||
return True | ||
return False | ||
|
||
def the_result_of_the_game(board: tuple): | ||
diagonal_1 = {board[i][i] for i in range(len(board))} | ||
diagonal_2 = {board[i][len(board) - i - 1] for i in range(len(board))} | ||
|
||
for i in range(3): | ||
line = set(board[i]) | ||
column = {board[j][i] for j in range(3)} | ||
if len(line) == 1 and list(line)[0] != 0: | ||
return list(line)[0] | ||
elif len(column) == 1 and list(column)[0] != 0: | ||
return list(column)[0] | ||
|
||
if len(diagonal_1) == 1 and list(diagonal_1)[0] != 0: | ||
return list(diagonal_1)[0] | ||
elif len(diagonal_2) == 1 and list(diagonal_2)[0] != 0: | ||
return list(diagonal_2)[0] | ||
|
||
return 0 # Ничья | ||
|
||
if __name__ == "__main__": | ||
import ast | ||
board = ast.literal_eval(input("Enter the state of the board (for example, [[1, 2, 0], [0, 1, 2], [2, 1, 0]]): ")) | ||
print(is_solved(board)) |