Note that there are some explanatory texts on larger screens.

plurals
  1. POPython "IndexError: string index out of range" (Beginner)
    text
    copied!<p>So for a Programming assignment we have to re write the sort function in python to sort a list of words. So far I have made it able to sort the words based of the first letter of each and now im trying to run recursion to still sort it if the first letters or any of the letters are the same. Im having problems with the "IndexError: string index out of range" error. What I have so far is </p> <pre><code>def insertion_sort(bookwords): for index in range(1,len(bookwords)): global word word=bookwords[index] i=index-1 word_checker(bookwords, 0, i) def word_checker(bookwords, num, i): while i&gt;=0: wordleft=bookwords[i] if ord(word[num])&lt;ord(wordleft[num]): bookwords[i+1]=bookwords[i] bookwords[i]=word i=i-1 elif ord(word[num])==ord(wordleft[num]): num=num+1 word_checker(bookwords, num, i) else: break bookwords=["michael", "maddy", "michelle", "monstor", "money", "mountain", "miniscus", "mega"] insertion_sort(bookwords) print bookwords </code></pre> <p>Im guessing num is becoming larger than the words but its running through many times without stopping when the letters arn't the same so im abit confused why its doing that. Any help would be greatly appreciated</p> <p>UPDATE</p> <p>Ok now it works but when I put it into the supplied code to test how fast it is with about 700000 words it went for 30+ until I stopped it where as the sort function took 5 seconds. Here is the code with my part as well</p> <pre><code>import re import pygame # 159.172 assignment 2 # def mysort(words): for index in range(1,len(words)): word=words[index] i=index-1 word_checker(words, i, word) def word_checker(words, i, word): while i&gt;=0: wordleft=words[i] if word==wordleft: break elif word&lt;wordleft: words[i+1]=words[i] words[i]=word i=i-1 else: return # Do NOT change anything below here: # # Compare two lists def compare(l1,l2): if len(l1) != len(l2): return False for a,b in zip(l1,l2): if a!=b: return False return True # Open the book book=open("allsherlock.txt", "rt") # Make a list of all the words in the book bookwords=[] for line in book: for word in re.findall(r'\w+', line): bookwords.append(word.lower()) print "Loaded",len(bookwords),"words" sortedbookwords=bookwords[:] pygame.init() # Use the sort function to sort the words for testing sortedbookwords.sort() starttime=pygame.time.get_ticks() # Call our sort function mysort(bookwords) print "Sort took",pygame.time.get_ticks()-starttime,"ms" print "Correct sort:",compare(bookwords,sortedbookwords) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload