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 7997393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:57:26+00:00 2026-06-04T14:57:26+00:00

Here’s an array, an index, and a number n representing how many items I

  • 0

Here’s an array, an index, and a number n representing how many items I want to slice out

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
index = 5
n = 3

The above arguments translates to give me 3 elements centered around index 5, which would be [4,5,6]

If the number is even, then just make an arbitrary choice of having the extra element on the left or right side. Eg: Given n = 2, [4, 5] and [5,6] are both equally valid.

But then we have to consider boundary cases. Supposing index = 1 and n = 5, it should return [0,1,2,3,4], because we’ve hit the boundary on the left side.

Similarly, Supposing index = 8 and n = 5, it should return [5,6,7,8,9] since we hit the boundary on the right side.

What’s a nice way to write this?

  • 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-06-04T14:57:27+00:00Added an answer on June 4, 2026 at 2:57 pm

    You can get a slice by using Array#[]

    Therefore, something like the following should work fine:

    arr[offset - (count / 2), count]
    

    Provided offset and count are Fixnums, ruby will handle the division and rounding correctly to meet the requirements you gave.

    EDIT | You should probably sanitize the arithmetic too, since negative offsets have a special meaning to #slice. Then you also want to sanitize the end value to allow the entire count to fit:

    arr[
      [[offset - (count / 2), 0].max, arr.size - count].min,
      count
    ]
    

    Getting a bit hairy now, but that’s it anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my php code $titikPetaInti = array(); while($row = mysql_fetch_assoc($hasil2)) { $titikPetaInti[] =
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here's my situation. I have a DotNetNuke application. I want to link to an
here is what I want to do: Add a CALayer as a sublayer to
Here is my code...I have two dimensional matrices A,B. I want to develop the
here i want to understand the architecture of bluez (Bluetooth Stack Protocol). I understood
Here is another spoj problem that asks how to find the number of distinct
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's the context: I have a file (or stream), and I want to process
Here's the thing: I want to add some images programmatically. The images should have

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.