Jump to content

Recommended Posts

The following code does a sequential search of a string (phonebook).

 

def main()    #input
    inp = (raw_input('Input: '))

    #given string
    phonebook = 'bcdfghjklmnpqrstvwxyz'

    # counter variables.
    count = 0
    totalbad = 0
    totalgood = 0
    fails = 0
    success = 0
    
    for char in inp:
        found = 0
        compares = 1
        for char in phonebook:
            if char > inp[count]:
                break
            if char == inp[count]:
                found = 1
                break
            else:
                compares = compares + 1
        if found:
            print inp[count] + " found with " + str(compares) + " compares"
            totalgood += compares
            success += 1
        else:
            print inp[count] + " not found with " + str(compares) + " compares"
            totalbad += compares
            fails += 1
        count += 1

    if fails != 0:   
        failavg = 1.0 * totalbad / fails
    if success != 0:
        sucavg = 1.0 * totalgood / success 

    print "number success = " + str(success) + "; total compares = " + str(totalgood) + "; average = " + str(sucavg)
    print "number failures = " + str(fails) + "; total compares = " + str(totalbad) + "; average = " + str(failavg)
   
if __name__ == '__main__':
    main() 

 

 

I have to modify the code to do a binary search, starting from the middle of the string... for instance:

 

 

Given: phonebook = 'bfmrs'

Output:

search char 'm' - found compares 1

search char 'n' - not found compares 2

search char 'b' - found compares 3

search char 'c' not found compares 3

 

 

 

My deadline is tomorrow.  If anyone can help me figure this out I would greatly appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/95747-any-python-gurus-here/
Share on other sites

Not the prettiest code, but I think I got it working

 

def main()
    #input
    inp = (raw_input('Input: '))

    #given string
    phonebook = 'bcdfghjklmnpqrstvwxyz'
    #phonebook = 'bfmrs'

    # counter variables.
    count = 0
    totalbad = 0
    totalgood = 0
    fails = 0
    success = 0

    mid = phonebook[len(phonebook)/2:]
    start = phonebook[:len(phonebook)/2]
    
    for char in inp:
        found = 0
        compares = 1
        middie = 0
        for char in mid:
            if char <= inp[count]:
                middie = 1
            else:
                break
            if char == inp[count]:
                found = 1
                break
            else:
                compares = compares + 1

        if (found == 0) & (middie == 0):
            compares = len(mid)
            for char in start:
                if char > inp[count]:
                    break
                if char == inp[count]:
                    found = 1
                    break
                else:
                    compares = compares +1
            
        if found:
            print inp[count] + " found with " + str(compares) + " compares"
            totalgood += compares
            success += 1
        else:
            print inp[count] + " not found with " + str(compares) + " compares"
            totalbad += compares
            fails += 1
        count += 1

    sucavg = 0.0
    failavg = 0.0
    if fails != 0:   
        failavg = 1.0 * totalbad / fails
    if success != 0:
        sucavg = 1.0 * totalgood / success 

    print "number success = " + str(success) + "; total compares = " + str(totalgood) + "; average = " + str(sucavg)
    print "number failures = " + str(fails) + "; total compares = " + str(totalbad) + "; average = " + str(failavg)

if __name__ == '__main__':
    main()    

Link to comment
https://forums.phpfreaks.com/topic/95747-any-python-gurus-here/#findComment-490268
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.