Naez Posted March 12, 2008 Share Posted March 12, 2008 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 More sharing options...
Naez Posted March 12, 2008 Author Share Posted March 12, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.