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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:44:14+00:00 2026-06-09T21:44:14+00:00

Usually stack overflow is reserved for tangible problems, but I am having wrapping my

  • 0

Usually stack overflow is reserved for tangible problems, but I am having wrapping my head around a puzzle and was wondering if anyone could explain it. I am going over recursion and came across Dave Thomas’s Code Kata (very cool). I am having fun making my own answers, then trying to whittle them down, but there is one response that I can’t figure out:

Problem: Code Kata #2 http://codekata.pragprog.com/2007/01/kata_two_karate.html

An answer that works, but I can’t understand why is here:

def chop(target, values)  
  # Special handling for zero and single element arrays
  return -1 if values.empty?
  return ((target == values[0]) ? 0 : -1) if values.length == 1

  # Try the bottom half first
  pos = chop(target, values[0, values.length/2])
  return pos if pos != -1

  # Then the upper half ... remember that the returned
  # position is relative to the middle of the array.
  pos = chop(target, values[values.length/2, values.length-1])
  return pos + (values.length/2) if pos != -1

  # Didn't find what we were looking for
  return -1
end

Can anyone please explain to me how the index makes it way back up this recursion pattern?

As I read it, it recurses until it hits its number and returns 0. I can’t figure out for the life of me why/how this thing spits back out the index.

  • 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-09T21:44:15+00:00Added an answer on June 9, 2026 at 9:44 pm

    I find that the best way to work through recursive code like this is to start with the return statements. The “return pos + (values.length/2) if pos != -1” line is where the magic happens.

    Restating the code in English: “Add the position of the found element (0 or a positive number) to the offset of the given array within the original array”.

    Basically, it is called several times on the way back up the call chain, acting as an accumulator for the final answer. To see this in action, try this:

    def chop(target, values)
      return -1 if values.empty?
      if values.length == 1
        return ((target == values[0]) ? 0 : -1)
      end
      pos = chop(target, values[0, values.length/2])
      return pos if pos != -1
      pos = chop(target, values[values.length/2, values.length-1])
      # Print some info
      if pos != -1
        puts [pos, (values.length/2)].inspect
        return pos + (values.length/2)
      end
      return -1
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im totally new at stack overflow, I usually just research stuff myself but I
A simple question, but I haven't found a definitive answer on Stack Overflow. struct
Programming language books usually explain that value types are created on the stack, and
Usually I worked with PostgreSQL and never had a problem, but now I need
Usually I prefer to write my own solutions for trivial problems because generally plugins
We have Request.UserHostAddress to get the IP address in ASP.NET, but this is usually
I know how to implement a wrapping cell renderer, but all popular implementations break
I usually do C# but have inherited a classic ASP project. I have defined
Greetings to all the compiler designers here on Stack Overflow. I am currently working
I understand that a typical stack based buffer overflow attack payload looks something like

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.