0

Ok. So I am probably the most stupidest person on this planet, but can someone tell me why this gives me an error (The program is for searching a character in an alphabetically ordered string of characters using bisection search):

def isInIter(char, aStr):
  print('char :'+char)
  print('aStr :'+aStr)
  isin = False
  while(len(aStr) > 0):
    if aStr[len(aStr)/2] == char:
      isin = True
      break
    else:
      if aStr[len(aStr)/2] > char:
        aStr = aStr[0 : len(aStr)/2]
        continue
      if aStr[len(aStr)/2] < char:
        aStr = aStr[(len(aStr)/2 + 1) :]
        continue
return isin

Error:

isInIter('d','cddfggjkkqtwyy')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-66-1de0eb4793fd> in <module>()
----> 1 isInIter('d','cddfggjkkqtwyy')

/home/user/Study/Ex/lec5_BisectionSearch.py in isInIter(char, aStr)
      5   isin = False
      6   while(len(aStr) > 0):
----> 7     if aStr[len(aStr)/2] == char:
      8       isin = True
      9       break

IndexError: string index out of range

But this works as expected:

char = raw_input('char :')
aStr = raw_input('aStr :')

isin = False
while(len(aStr) > 0):
 if aStr[len(aStr)/2] == char:
  isin = True
  break
 else:
  if aStr[len(aStr)/2] > char:
    aStr = aStr[0 : len(aStr)/2]
    continue
  if aStr[len(aStr)/2] < char:
    aStr = aStr[len(aStr)/2 + 1 :]
    continue

print isin

I appreciate your help.

8
  • 3
    you can directly search without looping it. Python provides you 'in' keyword which helps you to search for a item. You can use "if char in string" in a single statement which tells you if char is present in string or not.. Commented Jul 11, 2014 at 6:16
  • 1
    isInIter('d','cddfggjkkqtwyy') doesn't give error for me ? and works fine ? Commented Jul 11, 2014 at 6:24
  • that looks like a very bad way to search a character but your code runs just fine on my machine Commented Jul 11, 2014 at 6:24
  • You are returning the value at the wrong place.Check-out your code again and be sure that your line indentations are correct ! Commented Jul 11, 2014 at 6:25
  • Thanks for the response, but this is a homework problem for a MOOC course that I'm taking. The requirement is to implement the thing using a bisection search, not any other methods. Commented Jul 11, 2014 at 6:32

2 Answers 2

1

When python goes through a while loop, it waits until the end of the loop to check the exit criteria. It might be easier if you condense your function:

def isItIn(streen,carroter):
  while len(streen)>0:
    if streen[len(streen)/2]==carroter:
       return True
    if streen[len(streen)/2]>carroter:
       streen=streen[0:len(streen)/2]
    if len(streen)==0:return False
    if streen[len(streen)/2]<carroter:
        streen=streen[len(streen)/2+1:]

print isItIn('asdilk','a')
print isItIn('kdsoe','a')
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you use str.index(sub\[, start\[, end\]\])? You can bet that it has an optimized implementation.

In [1]: s = "cddfggjkkqtwyy"

In [2]: s.index("d")
Out[2]: 1

In [3]: s.index("d") > -1
Out[3]: True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.