Skip to content

Instantly share code, notes, and snippets.

@akrami
Last active October 3, 2022 19:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akrami/e11941303da32829c5b1b6578a4ef8de to your computer and use it in GitHub Desktop.
Save akrami/e11941303da32829c5b1b6578a4ef8de to your computer and use it in GitHub Desktop.
Code Signal Arcade - Intro level solutions (javascript)
/*
Write a function that returns the sum of two numbers.
*/
function add(param1, param2) {
return param1+param2;
}
/*
Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100,
the second - from the year 101 up to and including the year 200, etc.
*/
function centuryFromYear(year) {
return Math.ceil(year/100);
}
/*
Given the string, check if it is a palindrome.
*/
function checkPalindrome(inputString) {
return inputString.split("").reverse().join("") === inputString;
}
/*
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
*/
function adjacentElementsProduct(inputArray) {
var max = -Infinity;
for(var i=0; i<inputArray.length-1; i++){
max = inputArray[i]*inputArray[i+1]>max?inputArray[i]*inputArray[i+1]:max;
}
return max;
}
/*
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained
by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side.
#
# ###
# ### #####
# ###
#
*/
function shapeArea(n) {
if(n==1) return 1;
return (n-1)*4+shapeArea(n-1);
}
/*
Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer
size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger
than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure
out the minimum number of additional statues needed.
*/
function makeArrayConsecutive2(statues) {
return Math.max(...statues) - Math.min(...statues) - statues.length + 1;
}
/*
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing
no more than one element from the array.
Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only
one element is also considered to be strictly increasing.
*/
function almostIncreasingSequence(sequence) {
if(sequence.length==1) return true;
var first = increasingSequence(sequence);
if(first==-1) return true;
for(var i=first; i<sequence.length; i++){
var temp = [...sequence];
temp.splice(i,1);
if(increasingSequence(temp)==-1) return true;
}
return false;
}
function increasingSequence(sequence){
for(var i = 0; i<sequence.length-1; i++){
if(sequence[i+1]-sequence[i]<=0) return i;
}
return -1;
}
/*
After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost,
and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious,
they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.
Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return
the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).
*/
function matrixElementsSum(matrix) {
var h = matrix.length;
var l = matrix[0].length;
for(var i = 0; i<h; i++){
for(var j=0; j<l; j++){
if(matrix[i][j]==0 && typeof matrix[i+1] !== 'undefined') matrix[i+1][j]=0;
}
}
return arrSum(matrix);
}
const arrSum = array =>
array.reduce(
(sum, num) => sum + (Array.isArray(num) ? arrSum(num) : num * 1),
0
);
/*
Given an array of strings, return another array containing all of its longest strings.
*/
function allLongestStrings(inputArray) {
var maxLength = Math.max(...inputArray.map(element=>element.length));
return inputArray.filter(element=>element.length==maxLength);
}
@mino9421
Copy link

08.js line 23 needs explanation I am trying to understand it

@akrami
Copy link
Author

akrami commented Oct 3, 2022

08.js line 23 needs explanation I am trying to understand it

This is a reducer function that generate a final result based on running a function on all array entries. I have already written some examples in : https://alireza.akrami.io/blog/javascript-array-reduce
Here it checks if each entry (num) is array it will recursive run the parent function (arrSum) on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment