DEV Community

Cover image for The challenge to make a shape area calculation in CodeSignal

The challenge to make a shape area calculation in CodeSignal

Camilo Martinez on September 21, 2018

Languages: [πŸ‡ͺπŸ‡Έ] EspaΓ±ol - [πŸ‡ΊπŸ‡Έ] English Since college, I was not encouraged to do programming challenges, until I got the invitation to CodeSign...
Collapse
 
_bigblind profile image
Frederik πŸ‘¨β€πŸ’»βž‘οΈπŸŒ Creemers

I think think there's an even easier solution:

shapeArea = function(n){
    return 1 + (n-1)*4
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
xerghos profile image
Xerghos

I think this is more appropriate:

function shapeArea(n) {
return (1 + (n-1)*n) *2 - 1
}

Collapse
 
trungsusi97 profile image
TrungSuSi97

Why do you think that formula ?

Thread Thread
 
fromchiapasdev profile image
Axel Espinosa

Did you solve it?

Collapse
 
fromchiapasdev profile image
Axel Espinosa

Hey buddy, can you explain your solution?

Collapse
 
equiman profile image
Camilo Martinez

Yeah, that's exactly the beauty that I talk about code fights. You can learn from other solution.

Collapse
 
kikeflowers profile image
kikeflowers • Edited

I found this solution and just replaced the variables

function shapeArea(n) {
return 1 + 2 * n * (n-1)
//1 + 2 * 4 * (3) = 3*4=12*2=24+1=25
//1 + 2 * 3 * (2) = 2*3=6*2=12+1=13
//1 + 2 * 2 * (1) = 1*2=2*2=4+1=5
//1 + 2 * 1 * (0) = 0*1*=0*2=0+1=1
}

Collapse
 
larawho profile image
Lara Potjewyd • Edited

Hi! Would it be possible for someone to explain what's happening in the code? I'm a beginner to both code and maths. Any resources and tutorials would be appreciated too :)

Collapse
 
equiman profile image
Camilo Martinez • Edited

There is no tutorial about this specific ploblem. You need learn other concepts like recursion and loops.

This function shapeArea is not important. It's used to measure the amount of time.

Relevant things on this exercise is x.loopShapeArea, x.recursionShapeArea and x.mathShapeArea. Three of them solves the problem, but math is more efficient than others.

Collapse
 
stxenhammer profile image
stxenhammer

But where did you get the 4 from & multiplying it ?

For example when I can recite something for 60 times in a min so if i recite for 100 mins i just do 60 * 100 to find out how many times I recite a line.
What was ur reasoning behind using the number 4 ? I am not clear on how I would go on solving this on my own. @

Thread Thread
 
equiman profile image
Camilo Martinez • Edited

I'm using a technic learned in a subject Numerical Methods. You need reproduce step by steep and try to find something familiar between each interaction.

Explained

A square have 4 sides, that's why you need multiply the value by four.

But each side merge a point with other in the other side (red circle), you can't count both. That's why yo need subtract 4, one for each corner.

That's how I found this formula: n * 4 - 4

On first and second iteration is difficult to see it... but next iterations can give you an idea about the solution.

Hope it helps.

Collapse
 
graffitimsx profile image
Rudolf Arthur Frans Gutlich

the simpler way I think is:


return Math.pow(n,2) + Math.pow(n-1,2)

Enter fullscreen mode Exit fullscreen mode

Just rotate the grid view by 45ΒΊ and check the patterns

Collapse
 
akshayd20187103 profile image
Akshay Dalvi

return n*n + (n-1)*(n-1);

Collapse
 
ericcurtin profile image
Eric Curtin

It's more like:

int shapeArea(int n) {
  int area = 1;
  while (n > 1) {
    area += (n-- - 1) * 4;
  }
  return area;
}
Collapse
 
hodasalah profile image
hodasalah • Edited

I tried both solution

function solution(n) {
    return 2*n*(n-1) +1;
    //return n*2+(n-1)*2
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kikeflowers profile image
kikeflowers

function shapeArea(n) {
var res = 1;
for(let i = 1; i < n; i++){
res = res + (i*4)
}
return res;
}

Collapse
 
markkelly00 profile image
Mark Kelly

return n * ((n - 1) * 2) + 1;

Collapse
 
cayalav profile image
Carlos Ayala

n*(n*2*-1)-int((n*2-1/2)

Collapse
 
radwaalaa profile image
Radwa Alaa

this is the shortest valid solution :

int output = 1;
for (int i = 1; i <= n; i++) {
if (i != 1) {
output = output + 4 * (i - 1);
}
}

return output;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hodasalah profile image
hodasalah

i tried both solution
function solution(n) {
return 2*n*(n-1) +1;
//return n*2+(n-1)*2
}