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

Задачки с кодэварэс + задачка на класс и игра #2

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

усложнено решение, можно было сильно проще

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' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};
Loading