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 + superclass #85

Open
wants to merge 19 commits into
base: Lysov_Dmitrij_Pavlovich
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
16 changes: 16 additions & 0 deletions codewars/Adding Big Numbers/AddingBigNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function add(a, b) {
let total = '';
let base = 0;
let i = a.length - 1;
let j = b.length - 1;
while (i >= 0 || j >= 0 || base > 0) {
const num1 = i >= 0 ? parseInt(a[i], 10) : 0;
const num2 = j >= 0 ? parseInt(b[j], 10) : 0;
const sum = num1 + num2 + base;
base = Math.floor(sum / 10);
total += (sum % 10).toString();
i--;
j--;
}
return total.split('').reverse().join('');
}
13 changes: 13 additions & 0 deletions codewars/Anagram difference/Номер 11 - Anagram difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def anagram_difference(w1, w2):
if type(w1) == str:
w1 = list(w1)
if type(w2) == str:
w2 = list(w2)
ln_w1 = len(w1)
ln_w2 = len(w2)
len_anagram = 0
for i in w1:
if i in w2:
len_anagram += 1
w2.remove(i)
return ln_w1 - len_anagram + ln_w2 - len_anagram
6 changes: 6 additions & 0 deletions codewars/Array Deep Count/Номер 7 - Array Deep Count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def deep_count(a):
answer = len(a)
for i in a:
if type(i) == list:
answer += deep_count(i)
return answer
11 changes: 11 additions & 0 deletions codewars/Build Tower/Номер 6 - Build Tower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def tower_builder(n_floors):
answer = []
mx = 1 + 2 * (n_floors - 1)
for i in range(n_floors):
if i == 0:
count_star = 1
else:
count_star = 1 + 2 * i
count_space = (mx - count_star) // 2
answer.append(' ' * count_space + '*' * count_star + ' ' * count_space)
return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def to_camel_case(text):
answer = ''
k = False
for i in range(len(text)):
if text[i] == '_' or text[i] == '-':
k = True
elif k:
answer += text[i].upper()
k = False
else:
answer += text[i]
return answer
12 changes: 12 additions & 0 deletions codewars/Duplicate Encoder/Номер 1 - Duplicate Encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def duplicate_encode(word):
word_lower = word.lower()
count_letter = {}
answer = ''
for i in word_lower:
if i not in count_letter:
count_letter[i] = word_lower.count(i)
if count_letter[i] > 1:
answer += ')'
else:
answer += '('
return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def find_missing_letter(chars):
print(ord('a'))
print(ord('b'))
for i in range(len(chars) - 1):
if ord(chars[i + 1]) - ord(chars[i]) != 1:
return chr(ord(chars[i]) + 1)
13 changes: 13 additions & 0 deletions codewars/Flatten a Nested Map/FlattenANestedMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function flattenMap(object) {
function iter(part, keys) {
Object.keys(part).forEach(function (k) {
if (part[k] !== null && !Array.isArray(part[k]) && typeof part[k] === 'object') {
return iter(part[k], keys.concat(k));
}
flat[keys.concat(k).join('/')] = part[k];
});
}
var flat = {};
iter(object, []);
return flat;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from preloaded import TreeNode

def max_sum(root: TreeNode):
answer = 0
if root is None:
return answer
answer += root.value
left_root = max_sum(root.left)
right_root = max_sum(root.right)
if left_root == 0 and right_root != 0:
return answer + right_root
elif right_root == 0 and left_root != 0:
return answer + left_root
return answer + max(left_root, right_root)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
""" For your information:
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
"""

def sorted_insert(head, data):
x = head
print(data)
print(head)
if not head:
return Node(data)
while True:
print(head.data)
if head.data > data:
head.data, data = data, head.data
print(head.data, data)
if head.next is None:
head.next = Node(data)
break
head = head.next
return x
21 changes: 21 additions & 0 deletions codewars/Merge two arrays/MergeTwoArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function mergeArrays(a, b) {
console.log(a, b);
console.log(a.length, b.length);
let i = 0;
let answer = [];
while (i < a.length && i < b.length) {
answer.push(a[i]);
answer.push(b[i]);
i++;
}
while (i < a.length) {
answer.push(a[i]);
i++;
}
while (i < b.length) {
answer.push(b[i])
i++;
}
console.log(answer);
return answer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def move_zeros(lst):
arr_no_zero = []
arr_zero = []
for i in lst:
if i == 0:
arr_zero.append(i)
else:
arr_no_zero.append(i)
return arr_no_zero + arr_zero
7 changes: 7 additions & 0 deletions codewars/Permutations/Номер 8 - So Many Permutations!.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import itertools
def permutations(s):
answer = []
a = itertools.permutations(s)
for i in a:
answer.append(''.join(i))
return list(set(answer))
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from math import sqrt


def fib_number(n):
if n > 0:
n += 1
a = 0
b = 1
if n < 0:
print("Incorrect input")
elif n == 0:
return a
elif n == 1:
return b
else:
for i in range(2, n):
c = a + b
a = b
b = c
return b


def product_fib(_prod):
print(_prod)
k = 0
while True:
if fib_number(k) * fib_number(k + 1) == _prod:
return [fib_number(k), fib_number(k + 1), True]
elif fib_number(k) * fib_number(k + 1) > _prod:
return [fib_number(k), fib_number(k + 1), False]
else:
k += 1
15 changes: 15 additions & 0 deletions codewars/Simple Pig Latin/Номер 18 - Simple Pig Latin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def pig_it(text):
arr_letter = [chr(i) for i in range(97, 123)]
x = ''
answer = ''
for i in range(len(text)):
if text[i].lower() in arr_letter:
x += text[i]
if i == len(text) - 1:
answer += x[1:] + x[0] + 'ay'
else:
if text[i].lower() not in arr_letter and x:
answer += x[1:] + x[0] + 'ay'
x = ''
answer += text[i]
return answer
30 changes: 30 additions & 0 deletions codewars/Snail/Номер 16 - Snail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def snail(snail_map):
print(snail_map)
if snail_map == [[]]:
return []
answer = []
x_mn, y_mn = 0, 0
x_mx, y_mx = len(snail_map) - 1, len(snail_map) - 1
y_now, x_now = 0, 0
while True:
answer.append(snail_map[y_now][x_now])
y_mn += 1
while x_now < x_mx:
x_now += 1
answer.append(snail_map[y_now][x_now])
x_mx -= 1
while y_now < y_mx:
y_now += 1
answer.append(snail_map[y_now][x_now])
y_mx -= 1
while x_now > x_mn:
x_now -= 1
answer.append(snail_map[y_now][x_now])
x_mn += 1
while y_now > y_mn:
y_now -= 1
answer.append(snail_map[y_now][x_now])
if x_mn > x_mx or y_mn > y_mx:
break
x_now += 1
return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def digital_root(n):
answer = 0
while n > 0:
answer += n % 10
n //= 10
if answer // 10 != 0:
answer = digital_root(answer)
return answer
13 changes: 13 additions & 0 deletions codewars/Sum of Intervals/Номер 10 - Sum of Intervals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def sum_of_intervals(intervals):
sum_intervals = 0
intervals = sorted(intervals, key=lambda x: x[0])
interval_arr = []
for i in intervals:
if not interval_arr or interval_arr[-1][1] < i[0]:
interval_arr.append([i[0], i[1]])
else:
if interval_arr[-1][1] < i[1]:
interval_arr[-1][1] = i[1]
for i in interval_arr:
sum_intervals += i[1] - i[0]
return sum_intervals
6 changes: 6 additions & 0 deletions codewars/Sum of pairs/Номер 12 - Sum of Pairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def sum_pairs(ints, s):
answer = {}
for i in ints:
if s - i in answer:
return [s - i, i]
answer[i] = True
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def is_solved(board):
if board[0][0] == board[0][1] == board[0][2] and board[0][0] != 0:
return board[0][0]
if board[1][0] == board[1][1] == board[1][2] and board[1][0] != 0:
return board[1][0]
if board[2][0] == board[2][1] == board[2][2] and board[2][0] != 0:
return board[2][0]
if board[0][0] == board[1][0] == board[2][0] and board[0][0] != 0:
return board[0][0]
if board[0][1] == board[1][1] == board[2][1] and board[0][1] != 0:
return board[0][1]
if board[0][2] == board[1][2] == board[2][2] and board[0][2] != 0:
return board[0][2]
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != 0:
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != 0:
return board[0][2]
if 0 in board[0] or 0 in board[1] or 0 in board[2]:
return -1
return 0
12 changes: 12 additions & 0 deletions codewars/Valid Parentheses/Номер 13 - Valid Parentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def valid_parentheses(string):
count_open_brackets = 0
for i in string:
if i == '(':
count_open_brackets += 1
elif i == ')':
if count_open_brackets == 0:
return False
count_open_brackets -= 1
if count_open_brackets == 0:
return True
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def anagram_difference(w1, w2):
if type(w1) == str:
w1 = list(w1)
if type(w2) == str:
w2 = list(w2)
ln_w1 = len(w1)
ln_w2 = len(w2)
len_anagram = 0
for i in w1:
if i in w2:
len_anagram += 1
w2.remove(i)
if len_anagram == ln_w1 and len_anagram == ln_w2:
return True
return False
19 changes: 19 additions & 0 deletions rpgsaga/saga/src/Lab1/formuls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function formula(x: number, a: number = 1.6): number {
return Math.pow(a, (Math.pow(x, 2) - 1)) - Math.log10(Math.pow(x, 2) - 1) + Math.pow((Math.pow(x, 2) - 1), (1/3));
}

export function taskA(a: number, xstart: number, xend: number, xstep: number): number[] {
const y: number[] = [];
for (let i = xstart; i <= xend; i += xstep) {
y.push(formula(i, a));
}
return y;
}

export function taskB(a: number, arr: number[]): number[] {
const y: number[] = [];
for (let i of arr) {
y.push(formula(i, a));
}
return y;
}
19 changes: 19 additions & 0 deletions rpgsaga/saga/src/Lab1/output_lab1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { formula, taskA, taskB } from "./formuls";

const a: number = 1.6;
console.log("Задача А");
let xstart: number = 1.2;
let xend: number = 3.7;
let xstep: number = 0.5;
let yTaskA: number[] = taskA(a, xstart, xend, xstep);
for (let i of yTaskA) {
console.log(`y = ${i}`);
}

console.log("Задача B");
let arr: number[] = [1.28, 1.36, 2.47, 3.68, 4.56];
let yTaskB: number[] = taskB(a, arr);
for (let i of yTaskB) {
console.log(`y = ${i}`);
}
console.log(formula(4.56))
Loading