Skip to content

Commit

Permalink
init codewars tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
spleekz committed Sep 26, 2023
1 parent c8965a3 commit 1c563f6
Show file tree
Hide file tree
Showing 14 changed files with 494 additions and 0 deletions.
39 changes: 39 additions & 0 deletions codewars/Adding Big Numbers/sum_strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function sum_strings_task( str1 , str2 ) {

var len = str1.length > str2.length ? str1.length : str2.length;

var delta1 = len - str1.length;
var delta2 = len - str2.length;

str1 = '0'.repeat( delta1 ) + str1;
str2 = '0'.repeat( delta2 ) + str2;

var idx = len - 1;
var dec_over = 0;
var result = '';
while( idx !== -1 ) {

var digit1 = parseInt( str1[ idx ] , 10 );
var digit2 = parseInt( str2[ idx ] , 10 );

var sum = digit1 + digit2 + dec_over;

var unit = sum % 10;
var dec = Math.floor( sum / 10 );

dec_over = dec;

if( idx === 0 && dec > 0 ) {
result = `${ dec }${ unit }${ result }`;
}
else {
result = `${ unit }${ result }`;
}

idx -= 1;

};

return result;

};
18 changes: 18 additions & 0 deletions codewars/Array Deep Count/count_deep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function count_deep_task( arr ) {

return arr.reduce(
( res , el , idx )=> {

if( Array.isArray( el ) ) {

return res += count_deep_task( el ) + 1;

}

return res += 1;

} ,
0 ,
);

};
30 changes: 30 additions & 0 deletions codewars/Build Tower/tower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function tower_task( floors_count ) {

var space = ' ';
var star = '*';

return (
Array.from(
{ length : floors_count } ,
)
.reduce(
( res , _ , idx )=> {

var space_count = floors_count - 1 - idx;
var space_str = space.repeat( space_count );

var star_count = 1 + ( idx << 1 );
var star_str = star.repeat( star_count );

var floor = `${ space_str }${ star_str }${ space_str }`;

res.push( floor );

return res;

} ,
[] ,
)
);

};
33 changes: 33 additions & 0 deletions codewars/Convert string to camel case/case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function capitalize( str ) {
return (
str[ 0 ].toUpperCase()
+
str.slice( 1 )
);
};


function join_camel( parts ) {

return parts.reduce(
( res , part , idx )=> {

if( idx === 0 ) {
return res += part;
}
return res += capitalize( part );

} ,

);

};

function case_camel_task( str ) {

var str_dash = str.replace( /[-_]/g , '_' );
var str_parts = str_dash.split( '_' );

return join_camel( str_parts );

};
15 changes: 15 additions & 0 deletions codewars/Find the missing letter/missing_letter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function missing_letter_task( arr ) {

var start = arr[ 0 ].charCodeAt( 0 );

for( var i = start ; i < ( arr.length + start ) ; i += 1 ) {

var letter = String.fromCharCode( i );

if( arr[ i - start ] !== letter ) {
return letter;
}

};

};
35 changes: 35 additions & 0 deletions codewars/Flatten a Nested Map/flat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function is_obj( val ) {
return val?.constructor === Object;
};

function flat( val , delimiter , prev_key , res ) {

prev_key ??= '';
res ??= {};

if( !is_obj( val ) ) {
res[ prev_key ] = val;
return res;
}

Object.keys(
val ,
).forEach(
( key )=> {

var flat_key = `${ prev_key ? `${ prev_key }${ delimiter }` : '' }${ key }`;
flat( val[ key ] , delimiter , flat_key , res ?? {} );

} ,
);

return res;

};

function flat_task( struct ) {

var delimiter = '/';
return flat( struct , delimiter );

};
33 changes: 33 additions & 0 deletions codewars/Merge two arrays/array_merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function array_merge_task( arr1 , arr2 ) {

var arrs = [ arr1 , arr2 ];

var len = (
( arr1.length > arr2.length )
? arr1.length
: arr2.length
);

return (
Array.from(
{ length : len } ,
).reduce(
( res , _ , idx )=> {

arrs.forEach(
( arr )=> {
var val = arr[ idx ]
if( val !== undefined ) {
res.push( val );
}
}
);

return res;

} ,
[] ,
)
);

};
15 changes: 15 additions & 0 deletions codewars/Moving Zeros To The End/move_zeros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function move_zeros_task( arr ) {

var res = [];

var zero_count = 0;
arr.forEach(
( el )=> {
if( el === 0 ) { zero_count += 1 }
else { res.push( el ) }
}
);

return res.concat( Array.from( { length : zero_count } , ()=> 0 ) );

};
34 changes: 34 additions & 0 deletions codewars/Permutations/permuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function permuts_task( str ) {

var units = str.split( '' );

var permuts_set = units.reduce(
( res , unit , idx )=> {

var str_no_unit = (
str.slice( 0 , idx )
+
str.slice( idx + 1 )
);

var str_no_unit_permuts = permuts( str_no_unit );
if( str_no_unit.length === 0 ) { res.add( str ); return res };

str_no_unit_permuts.forEach(
( u )=> {

var permut = unit + u;
res.add( permut );

}
);

return res;

} ,
new Set() ,
);

return Array.from( permuts_set );

};
20 changes: 20 additions & 0 deletions codewars/Product of consecutive Fib numbers/fib_prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function fib_prod_task( prod , fib1 , fib2 ) {

var current_prod = fib1 * fib2;
if( current_prod === prod ) {
return [ fib1 , fib2 , true ];
}
else if( current_prod > prod ) {
return [ fib2 - fib1 , fib1 , false ];
}
else {
var next = fib1 + fib2;
var nenext = next + fib2;
return fib_prod( prod , next , nenext );
};

};

function fib_prod_task( prod ) {
return fib_prod( prod , 0 , 1 );
};
3 changes: 3 additions & 0 deletions codewars/Simple Pig Latin/pig_latin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function pig_latin_task( str ) {
return str.replace( /(\w)(\w+)/g , '$2$1ay' );
};
Loading

0 comments on commit 1c563f6

Please sign in to comment.