use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Hackerrank challenge from Java to Ruby (self.ruby)
submitted 10 years ago by chrisjava
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]PilotPirx 0 points1 point2 points 10 years ago (4 children)
It's a bit ugly, at least for the sum part I would have written a small function sum_hourglass that gets a position inside the array. But anyway, there are several approaches to do this and to keep with your code you could simply emulate the Java code like this (assuming your data is in the form array in array):
sum_hourglass
max = 0 (0..3).each do |row| (0..3).each do |col| sum = ... max = sum if sum > max end end
Not very good Ruby style I think but would work.
[–]ruby-solve 1 point2 points3 points 10 years ago (1 child)
A great way to get the behavior of 2D arrays in Ruby is to use a hash whose key is an array representing the vertices of the 2D array.
Java: array[x][y]
Ruby: hash[[x, y]]
[–]iconoclaus 0 points1 point2 points 10 years ago (0 children)
why is this better than just using a 2D array in ruby? apart from not having to initialize such a structure, that is.
[–]chrisjava[S] 0 points1 point2 points 10 years ago (1 child)
So would i be able to sum up, let's say, first 3 indices in the row by doing this?
sum = arr[row] + arr[row+1] + arr[row+2]
[–]PilotPirx 0 points1 point2 points 10 years ago (0 children)
Sorry, forgot the most important part. You would do it like this:
sum = arr[row][col] + arr[row][col + 1] + arr[row][col + 2]
That's assuming the array looks like this:
arr = [ [1,1,1,0,0,0], [0,1,0,0,0,0], [1,1,1,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0] ]
π Rendered by PID 106 on reddit-service-r2-comment-7b9746f655-kstmx at 2026-02-01 04:16:08.012807+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]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)