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+tests #106

Open
wants to merge 1 commit into
base: Daniel_Andreevich_Chudov
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
21 changes: 21 additions & 0 deletions codewars/Adding Big Numbers/1cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function addbignumbbnumb(a, b) {
while (a.length < b.length) a = '0' + a;
while (b.length < a.length) b = '0' + b;

let carr = 0;
let result = '';

for (let i = a.length - 1; i >= 0; i--) {
let sum = parseInt(a[i]) + parseInt(b[i]) + carr;
carr = Math.floor(sum / 10);
result = (sum % 10) + result;
}
if (carr > 0) {
result = carr + result;
}

return result;
}

let result = addbignumbbnumb("12331321134145151351", "32123414321424124312341");
console.log(result);
30 changes: 30 additions & 0 deletions codewars/Anagram difference/2cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function smrr(word, wb2) {
for (let j = 0; j < wb2.length; j++) {
if (word === wb2[j]) {
wb2.splice(j, 1);
return word;
}
}
return null;
}

function anargrDif(w1, w2) {
let wb1 = w1.split("");
let wb2 = w2.split("");
let box_of_repeat = [];

for (let i = 0; i < wb1.length; i++) {
let res_word = smrr(wb1[i], wb2);
if (res_word !== null) {
box_of_repeat.push(res_word);
wb1.splice(i, 1);
i--;
}
}

let final_result = (wb1.length + wb2.length);
return final_result;
}

let result = anargrDif("codewars", "hackerrank");
console.log(result);
24 changes: 24 additions & 0 deletions codewars/Array Deep Count/3cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function adcounter(word){
if (Array.isArray(word)) {
console.log("yes");
let count = Count(word);
return count + 1;
}
else {
console.log("no");
return(1);
}
}


function Count(is_here_anyobj){
let Global_count = 0;
for (let i = 0; i < is_here_anyobj.length; i++){
let pre_res = adcounter(is_here_anyobj[i]);
Global_count += pre_res;
}
return(Global_count);
}
let arr = [1, 2, 3];
let result = Count(arr);
console.log(result);
18 changes: 18 additions & 0 deletions codewars/Build Tower/4cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function TowerMake(number_of_blocks){
let pyrimidecanvas = [];
let number_of_space = number_of_blocks - 1;
let counting_stars = 1;
for (let i = 0; i < number_of_blocks; i++){
let floor_pyrimidecanvas = ' '.repeat(number_of_space) + "*".repeat(counting_stars) + ' '.repeat(number_of_space);
pyrimidecanvas.push(floor_pyrimidecanvas);
counting_stars += 2;
number_of_space -=1;
}
return(pyrimidecanvas);
}

let pyrimidecanvas_floor = TowerMake(5);

let formattedpyrimidecanvas = '[\n ' + pyrimidecanvas_floor.map(line => `"${line}"`).join(',\n ') + '\n]';

console.log(formattedpyrimidecanvas);
16 changes: 16 additions & 0 deletions codewars/Convert string to camel case/5cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function CSka(str){
let result = "";
for (i = 0; i < str.length; i++) {
if (str[i] == "-" || str[i] == "_") {
i++;
result += str[i].toUpperCase();
}
else{
result += str[i];
}
}
return(result);
}

let guess_word = CSka("the-stealth_warrior");
console.log(guess_word);
24 changes: 24 additions & 0 deletions codewars/Duplicate Encoder/6cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function duplicEncode(word){
let splitword = word.toLowerCase().split('');

let convertext = [];

for (let i = 0; i < splitword.length; i++) {
let count = 0;
for (let j = 0; j < splitword.length; j++){
if (splitword[i] === splitword[j]){
count++;
}
}
if (count > 1) {
convertext.push(")");
} else {
convertext.push("(");
}
}

return convertext.join('');
}

let result = duplicEncode("dir");
console.log(result);
17 changes: 17 additions & 0 deletions codewars/Duplicate Encoder/7cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function Findmisslett(array)
{
for (let i = 0; i < array.length; i++) {
let currentCharCode = array[i].charCodeAt(0);
let nextCharCode = array[i+1].charCodeAt(0);

if (nextCharCode !== currentCharCode + 1){
return String.fromCharCode(currentCharCode + 1);
}
}
}



let arr = ["b", "c", "e"];
let result = Findmisslett(arr);
console.log(result);
33 changes: 33 additions & 0 deletions codewars/Find the missing letter/8cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function flattenaNestMap(map, keys = '', res = {}) {
let temp_massiv_map = Object.keys(map);

if (temp_massiv_map.length === 0) {
return res;
}

for (let i = 0; i < temp_massiv_map.length; i++) {
let key = temp_massiv_map[i];
let newKey = keys ? `${keys}/${key}` : key;

if (typeof map[key] === 'object' && map[key] !== null && !Array.isArray(map[key])) {
flattenaNestMap(map[key], newKey, res);
} else {
res[newKey] = map[key];
}
}

return res;
}

let mops = {
'a': {
'b': {
'c': 12,
'd': 'Hello World'
},
'e': [1, 2, 3]
}
};

let result = flattenaNestMap(mops);
console.log(result);
30 changes: 30 additions & 0 deletions codewars/Fun with tree - max sum/9cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function havefunandrelax(root) {
if (root === null) {
return 0;
}

if (root.left === null && root.right === null) {
return root.value;
}

const leftSum = root.left ? havefunandrelax(root.left) : -Infinity;
const rightSum = root.right ? havefunandrelax(root.right) : -Infinity;

return root.value + Math.max(leftSum, rightSum);
}

const tree1 = new TreeNode(
17,
new TreeNode(3, new TreeNode(2)),
new TreeNode(-10, new TreeNode(16), new TreeNode(1, new TreeNode(13)))
);

console.log(havefunandrelax(tree1));

const tree2 = new TreeNode(
5,
new TreeNode(4, new TreeNode(-80), new TreeNode(-60)),
new TreeNode(10, new TreeNode(-90))
);

console.log(havefunandrelax(tree2));
56 changes: 56 additions & 0 deletions codewars/Linked Lists - Sorted Insert/10cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}

function sortins(head, data) {
let newNode = new Node(data);

if (!head || data < head.data) {
newNode.next = head;
return newNode;
}

let current = head;
while (current.next && current.next.data < data) {
current = current.next;
}

newNode.next = current.next;
current.next = newNode;

return head;
}

function prlintked(head) {
let current = head;
let result = [];
while (current) {
result.push(current.data);
current = current.next;
}
console.log(result.join(' -> '));
}

let list1 = new Node(1);
list1.next = new Node(2);
list1.next.next = new Node(3);

let newHead1 = sortins(list1, 4);
prlintked(newHead1);

let list2 = new Node(1);
list2.next = new Node(7);
list2.next.next = new Node(8);

let newHead2 = sortins(list2, 5);
prlintked(newHead2);

let list3 = new Node(3);
list3.next = new Node(5);
list3.next.next = new Node(9);

let newHead3 = sortins(list3, 7);
prlintked(newHead3);
19 changes: 19 additions & 0 deletions codewars/Merge two arrays/11cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function mergeAry(a, b) {
let box = [];
for (let i = 0; i < a.length; i++){
if(i >= a.length){
box.push(b[i]);
}
else if(i >= b.length){
box.push(a[i]);
}
else{
box.push(a[i]);
box.push(b[i]);
}
}
return(box);
}

let result = mergeAry(["a", "b", "c", "d", "e", "f"], [1, 2, 3]);
console.log(result);
17 changes: 17 additions & 0 deletions codewars/Moving Zeros To The End/12cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function movingZerosTheEnd(arr) {
let arwz = [];
let arrzero = [];
for (let i = 0; i < arr.length; i++){
if (arr[i] === 0){
arrzero.push(arr[i]);
}
else{
arwz.push(arr[i]);
}
}
return arwz.concat(arrzero);
}


result = movingZerosTheEnd([false,1,0,1,2,0,1,3,"a"]);
console.log(result);
26 changes: 26 additions & 0 deletions codewars/Permutations/13cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function permutat(str) {
if (str.length <= 1) {
return [str];
}

const resultSet = new Set();

for (let i = 0; i < str.length; i++) {
const char = str[i];
const remainingChars = str.slice(0, i) + str.slice(i + 1);
const remainingPermutations = permutat(remainingChars);

for (const perm of remainingPermutations) {
resultSet.add(char + perm);
}
}

return Array.from(resultSet);
}

const input = "aabb";
const permutationsut = permutat(input);

const formattedOutput = permutationsut.map(permutations => `'${permutations}'`);

console.log(`[${formattedOutput.join(', ')}]`);
19 changes: 19 additions & 0 deletions codewars/Product of consecutive Fib numbers/14cod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Fubnumber(prod) {
let a = 0;
let b = 1;

while (a * b < prod) {
let next = a + b;
a = b;
b = next;
}

if (a * b === prod) {
return [a, b, true];
} else {
return [a, b, false];
}
}

console.log(Fubnumber(714));
console.log(Fubnumber(800));
Loading