From: pharrington Date: 2009-08-08T10:35:05+09:00 Subject: Re: what is "stack level too deep (SystemStackError)" error On Aug 7, 9:04 pm, Mrmaster Mrmaster wrote: > pharrington wrote: > > > When coding recursively, you *always* need to test for the exit > > condition first. With searches, this means ensuring your result set > > isn't empty before continuing. If your code still loops endlessly > > after doing this, it helps to print the search set each step of the > > way to see exactly whats going wrong. > > > (in this case your set is actually the bounds passed to the method, > > not the array itself ***) > > When I use 6 it gave me the answer but when I tried 20 as the target i > received that answer. Wouldn't my exist be the either it found or else > it didn't? > Well, what does "didn't find it" mean? Think about what a binary search is: take a bunch of sorted objects, check if what you're looking for is in the middle; if not divide that list in two and then search on the appropriate half. The set gets smaller and smaller (like little Russian dolls) until either you've found the thing or there's nothing left. Thus, "didn't find it" means that there's nothing left to search. > I'm sorry but i don't know what you mean by your statement: > > >(in this case your set is actually the bounds passed to the method, > > not the array itself ***) > > -- > Posted viahttp://www.ruby-forum.com/. The... more straight-forward? naive? way to do this is to actually create another list that's half of the original list, can search through that. When doing that, you just check of the list itself is empty. In this case, you're changing array bounds to "create" a new list. So instead of checking if the array is empty, check to see if nothing can fit within the bounds.