Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 511317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:12:15+00:00 2026-05-13T07:12:15+00:00

I have an array of 16 squares and I would like to auto complete

  • 0

I have an array of 16 squares and I would like to auto complete it with integers values depending of the position in the array and the vector TOP_RIGHT.

TOP_RIGHT = 3

# Build the array...
@top_right = Array.new(16, 0)
@top_right.each_index do |square|
  @top_right[square] = square / TOP_RIGHT if (0...16).include?(square - TOP_RIGHT)
end

# Print the array...
i = 0
@top_right.each do |square|
  puts if i % 4 == 0
  print "#{square} "
i += 1
end

My code seems to be okay, but after testing the result is:

0 0 0 1 
1 1 2 2 
2 3 3 3 
4 4 4 5

I would like to get this array:

0 0 0 0 
1 1 1 0 
2 2 1 0 
3 2 1 0

Do you think it is possible, using array and simple Ruby methods?

Thanks, and happy end year!

Edit:

In the previous example, TOP_RIGHT is a vector like and its value is the number of cases that we can add or sub in order to go from the source square to the destination square (which is on top-right).

For example if I get this other vector: TOP, its value is 4 and the generated array can be:

# print_array(4)
0 0 0 0 
1 1 1 1 
2 2 2 2
3 3 3 3

…and if the vector is RIGHT, its value is -1 and the array can be:

# print_array(-1)
3 2 1 0 
3 2 1 0
3 2 1 0
3 2 1 0

Isn’t it a little beat tricky? 🙂
I don’t see how to process for designing a constructor which can build such arrays, by passing a value of a vector.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:12:15+00:00Added an answer on May 13, 2026 at 7:12 am

    If you use an array to hold an array, and don’t try to do it in a vector, it’s not too bad:

    #!/usr/bin/ruby1.8
    
    top_right = 3
    array_size = 4
    a = (0...array_size).collect do |i|
      (0...array_size).collect do |j|
        [i, j].collect do |e|
          top_right - e
        end.min
      end
    end.reverse
    a.each do |e|
      p e 
    end
    
    => [0, 0, 0, 0]
    => [1, 1, 1, 0]
    => [2, 2, 1, 0]
    => [3, 2, 1, 0]
    

    And if you really need the array squished into a vector, then:

    v = a.flatten
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
We have a grid with red squares on it. Meaning we have an array
When I have an array (of 1D, 2D, 3D, or more dimensions), I would
Before you start reading I would like to clarify: I have already thought of
I have a 1-dimensional float array of root mean square values, each calculated with
I have a perfectly square 64x64 2D array of integers that will never have
Right now I have an x by y array to hold integers that decide
I have created an array square(40) as picturebox. I have to assign 40 picture

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.