-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
spleekz
committed
Sep 26, 2023
1 parent
c8965a3
commit 1c563f6
Showing
14 changed files
with
494 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,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; | ||
|
||
}; |
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,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 , | ||
); | ||
|
||
}; |
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,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; | ||
|
||
} , | ||
[] , | ||
) | ||
); | ||
|
||
}; |
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,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 ); | ||
|
||
}; |
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,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; | ||
} | ||
|
||
}; | ||
|
||
}; |
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 @@ | ||
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 ); | ||
|
||
}; |
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,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; | ||
|
||
} , | ||
[] , | ||
) | ||
); | ||
|
||
}; |
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,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 ) ); | ||
|
||
}; |
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 @@ | ||
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 ); | ||
|
||
}; |
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 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 ); | ||
}; |
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,3 @@ | ||
function pig_latin_task( str ) { | ||
return str.replace( /(\w)(\w+)/g , '$2$1ay' ); | ||
}; |
Oops, something went wrong.