From 1c563f6c79a0a2349accf52ba6995bf2f0ae5a9f Mon Sep 17 00:00:00 2001 From: spleekz Date: Tue, 26 Sep 2023 11:55:36 +0300 Subject: [PATCH] init codewars tasks --- codewars/Adding Big Numbers/sum_strings.js | 39 +++++ codewars/Array Deep Count/count_deep.js | 18 +++ codewars/Build Tower/tower.js | 30 ++++ codewars/Convert string to camel case/case.js | 33 +++++ .../Find the missing letter/missing_letter.js | 15 ++ codewars/Flatten a Nested Map/flat.js | 35 +++++ codewars/Merge two arrays/array_merge.js | 33 +++++ .../Moving Zeros To The End/move_zeros.js | 15 ++ codewars/Permutations/permuts.js | 34 +++++ .../fib_prod.js | 20 +++ codewars/Simple Pig Latin/pig_latin.js | 3 + codewars/Snail/snail.js | 140 ++++++++++++++++++ .../digit_root.js | 26 ++++ codewars/Tic-Tac-Toe Checker/tictac.js | 53 +++++++ 14 files changed, 494 insertions(+) create mode 100644 codewars/Adding Big Numbers/sum_strings.js create mode 100644 codewars/Array Deep Count/count_deep.js create mode 100644 codewars/Build Tower/tower.js create mode 100644 codewars/Convert string to camel case/case.js create mode 100644 codewars/Find the missing letter/missing_letter.js create mode 100644 codewars/Flatten a Nested Map/flat.js create mode 100644 codewars/Merge two arrays/array_merge.js create mode 100644 codewars/Moving Zeros To The End/move_zeros.js create mode 100644 codewars/Permutations/permuts.js create mode 100644 codewars/Product of consecutive Fib numbers/fib_prod.js create mode 100644 codewars/Simple Pig Latin/pig_latin.js create mode 100644 codewars/Snail/snail.js create mode 100644 codewars/Sum of Digits - Digital Root/digit_root.js create mode 100644 codewars/Tic-Tac-Toe Checker/tictac.js diff --git a/codewars/Adding Big Numbers/sum_strings.js b/codewars/Adding Big Numbers/sum_strings.js new file mode 100644 index 0000000..ec5acfc --- /dev/null +++ b/codewars/Adding Big Numbers/sum_strings.js @@ -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; + +}; diff --git a/codewars/Array Deep Count/count_deep.js b/codewars/Array Deep Count/count_deep.js new file mode 100644 index 0000000..531998c --- /dev/null +++ b/codewars/Array Deep Count/count_deep.js @@ -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 , + ); + +}; diff --git a/codewars/Build Tower/tower.js b/codewars/Build Tower/tower.js new file mode 100644 index 0000000..b41c401 --- /dev/null +++ b/codewars/Build Tower/tower.js @@ -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; + + } , + [] , + ) + ); + +}; diff --git a/codewars/Convert string to camel case/case.js b/codewars/Convert string to camel case/case.js new file mode 100644 index 0000000..f5b2944 --- /dev/null +++ b/codewars/Convert string to camel case/case.js @@ -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 ); + +}; diff --git a/codewars/Find the missing letter/missing_letter.js b/codewars/Find the missing letter/missing_letter.js new file mode 100644 index 0000000..45531a8 --- /dev/null +++ b/codewars/Find the missing letter/missing_letter.js @@ -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; + } + + }; + +}; diff --git a/codewars/Flatten a Nested Map/flat.js b/codewars/Flatten a Nested Map/flat.js new file mode 100644 index 0000000..6708cc4 --- /dev/null +++ b/codewars/Flatten a Nested Map/flat.js @@ -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 ); + +}; diff --git a/codewars/Merge two arrays/array_merge.js b/codewars/Merge two arrays/array_merge.js new file mode 100644 index 0000000..6bf990a --- /dev/null +++ b/codewars/Merge two arrays/array_merge.js @@ -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; + + } , + [] , + ) + ); + +}; diff --git a/codewars/Moving Zeros To The End/move_zeros.js b/codewars/Moving Zeros To The End/move_zeros.js new file mode 100644 index 0000000..a074a07 --- /dev/null +++ b/codewars/Moving Zeros To The End/move_zeros.js @@ -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 ) ); + +}; diff --git a/codewars/Permutations/permuts.js b/codewars/Permutations/permuts.js new file mode 100644 index 0000000..5dd21c1 --- /dev/null +++ b/codewars/Permutations/permuts.js @@ -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 ); + +}; diff --git a/codewars/Product of consecutive Fib numbers/fib_prod.js b/codewars/Product of consecutive Fib numbers/fib_prod.js new file mode 100644 index 0000000..123a8ea --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/fib_prod.js @@ -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 ); +}; diff --git a/codewars/Simple Pig Latin/pig_latin.js b/codewars/Simple Pig Latin/pig_latin.js new file mode 100644 index 0000000..1d22b73 --- /dev/null +++ b/codewars/Simple Pig Latin/pig_latin.js @@ -0,0 +1,3 @@ +function pig_latin_task( str ) { + return str.replace( /(\w)(\w+)/g , '$2$1ay' ); +}; diff --git a/codewars/Snail/snail.js b/codewars/Snail/snail.js new file mode 100644 index 0000000..21f5057 --- /dev/null +++ b/codewars/Snail/snail.js @@ -0,0 +1,140 @@ +function matrix_flat( mat ) { + + return ( + mat.reduce( + ( res , sub_arr )=> { + + res.push( ... sub_arr ); + return res; + + } , + [] , + ) + ); + +}; + +function arr_step_right( arr , from , fn ) { + + var next_idx = from + 1; + var next_el = arr[ next_idx ]; + + fn( next_el , next_idx ); + +}; + +function arr_step_left( arr , from , fn ) { + + var next_idx = from - 1; + var next_el = arr[ next_idx ]; + + fn( next_el , next_idx ); + +}; + +function arr_step_up( arr , size , from , fn ) { + + var next_idx = from - size; + var next_el = arr[ next_idx ]; + + fn( next_el , next_idx ); + +}; + +function arr_step_down( arr , size , from , fn ) { + + var next_idx = from + size; + var next_el = arr[ next_idx ]; + + fn( next_el , next_idx ); + +}; + +function arr_step( direction , arr , size , from , fn ) { + + if( direction === 0 ) { + arr_step_right( arr , from ,fn ); + } + else if( direction === 1 ) { + arr_step_down( arr , size , from , fn ); + } + else if( direction === 2 ) { + arr_step_left( arr , from , fn ); + } + else if( direction === 3 ) { + arr_step_up( arr , size , from , fn ); + } + +}; + +function snail_task( mat ) { + + var n = mat.length; + + var arr_flat = matrix_flat( mat ); + + var steps = ( + Array.from( + { length : ( n << 1 ) - 1 } , + ) + .reduce( + ( res , _ , idx )=> { + + var val = n - idx - 1; + + if( val > 0 ) { + var repeat_times = ( idx === 0 ? 3 : 2 ); + res.push( ... Array.from( { length : repeat_times } , ()=> val ) ); + } + + return res; + + } , + [] , + ) + ); + + var current_direction = 0; + function update_direction() { + + if( current_direction === 3 ) { + current_direction = 0; + } + else { + current_direction += 1; + } + + }; + + var stop_at = 0; + var arr_jogged = ( + steps.reduce( + ( res , step_count )=> { + + for( var i = 1 ; i <= step_count ; i += 1 ) { + + arr_step( + current_direction , + arr_flat , + n , + stop_at , + ( el , idx )=> { + res.push( el ); + stop_at = idx; + } , + ); + + }; + + update_direction(); + + return res; + + } , + [ arr_flat[ stop_at ] ] , + ) + ); + + return arr_jogged; + +}; diff --git a/codewars/Sum of Digits - Digital Root/digit_root.js b/codewars/Sum of Digits - Digital Root/digit_root.js new file mode 100644 index 0000000..bb507e9 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/digit_root.js @@ -0,0 +1,26 @@ +function digit_sum( num ) { + + var rest = num; + + var result = 0; + while ( rest !== 0 ) { + + result += rest % 10; + rest = Math.floor( rest / 10 ); + + }; + + return result; + +}; + +function digit_root_task( num ) { + + var sum = digit_sum( num ); + while( sum >= 10 ) { + sum = digit_sum( sum ); + }; + + return sum; + +}; diff --git a/codewars/Tic-Tac-Toe Checker/tictac.js b/codewars/Tic-Tac-Toe Checker/tictac.js new file mode 100644 index 0000000..cd1077b --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/tictac.js @@ -0,0 +1,53 @@ +function arr_from_matrix( matrix ) { + return ( + matrix.reduce( + ( res , row )=> { + res.push( ... row ); + return res; + } , + [] , + ) + ); +}; + +function tictac_task( field ) { + + var win_combos = [ + [ 0 , 1 , 2 ] , + [ 3 , 4 , 5 ] , + [ 6 , 7 , 8 ] , + [ 0 , 3 , 6 ] , + [ 1 , 4 , 7 ] , + [ 2 , 5 , 8 ] , + [ 0 , 4 , 8 ] , + [ 2 , 4 , 6 ] , + ]; + + var field_arr = arr_from_matrix( field ); + + for( var i = 0 ; i < win_combos.length ; i += 1 ) { + + var combo = win_combos[ i ]; + + if( + ( combo.every( + ( idx )=> ( field_arr[ idx ] === 1 ) + ) ) + || + ( combo.every( + ( idx )=> ( field_arr[ idx ] === 2 ) + ) ) + ) { + return field_arr[ combo[ 0 ] ]; + } + + }; + + if( field_arr.includes( 0 ) ) { + return -1; + } + else { + return 0; + } + +};