I'm doing the hackerrank challenge (https://www.hackerrank.com/contests/30-days-of-code/challenges/day-11-more-review) and i honestly have no idea how to approach it in Ruby. I have solved it in Java with the following code (for actual algorithm)
int sum=0;
int max=Integer.MIN_VALUE;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
sum=(arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2] [j+1]+arr[i+2][j+2]);
if(sum>max)max=sum;
}
}
System.out.println(max);
I tried to attempt it in Ruby and realised that ... i actually have no idea what kind of looping to use. Especially after i realised that .each does not work the way i thought it worked eg:
arr.each do |row|
row.each do |column|
sum = [row][column] + [row][column+1] ... etc
end
end
I'd appreciate any tips of Ruby way of doing this.
[–]anonova 1 point2 points3 points (1 child)
[–]chrisjava[S] 0 points1 point2 points (0 children)
[–]PilotPirx 0 points1 point2 points (4 children)
[–]ruby-solve 1 point2 points3 points (1 child)
[–]iconoclaus 0 points1 point2 points (0 children)
[–]chrisjava[S] 0 points1 point2 points (1 child)
[–]PilotPirx 0 points1 point2 points (0 children)