Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
David Anderson code challenge
// Task: Given a 2D array, an (n) distance, and an array of [x,y] positive cell arrays - print out how many positive neighboring cells are found within the manhattan distance threshhold of the original positive cells. // example: X is the original positive cell. 0 = negative; 1 = positive. n = 2; There are 13 positive cells - 12 neighbors (1), 1 original (X) // 0 0 1 0 0 // 0 1 1 1 0 // 1 1 X 1 1 // 0 1 1 1 0 // 0 0 1 0 0 // Refer back to the grid cell neighborhood PDF for more details, assumptions, and examples // Please do not hesistate to reach out with an questions! // Parameters // collXCount: number - number of columns // rowYCount: number - number of rows // n: number - distance threshold // positiveCellsXYArray: Array<Array>> - array of [x,y] arrays. Ex: [[1,3], [5,5], [5,8]] function main(collXCount, rowYCount, n, positiveCellsXYArray) { var count = 0; for (let i=0; i<rowYCount; i++){ for(let j=0; j<collXCount; j++){ for(let k=1; k<=positiveCellsXYArray.length; k++){ let arrayPosition = k-1; let disToPos = (Math.abs(i - positiveCellsXYArray[arrayPosition][1])) + (Math.abs(j - positiveCellsXYArray[arrayPosition][0])); if (n>=disToPos){ count++; break; } } } } // kept it fairly simple; I loop through each cell, use the loop keys to math out the // Manhatten distance to each positive cell. When we hit a distance within 'n' I increment // the count then break to guarantee each cell is only counted once. This should work in // any configuration of rows and columns because we are running these checks on each cell once only. print('total count of positive cells is: ', count); } // *** General test cases - feel free to expand and add more (we will be adding more when testing your code) *** main(10, 10, 3, [[5,5]]); //will return 25 main(10, 10, 2, [[7,3],[3,7]]); //will return 26 main(10, 10, 3, [[2,2]]); //will return 23
run
|
edit
|
history
|
help
0
color
JS exam part 2 - Find the property
ComposeWordsGrid
//Rhino 1.7.7.1print("Hello, world!")
Change format Numbers
Add more strength to input color
random2
#include
tt
Javascript