1

I am learning python. My current self taught assignment is to write a program that takes a string and checks weather or not the the letters in the string are in alphabetical order. If they are not, the program is to return the number of instances that the letters are out of order. I ask the help of stackoverflow, as the code to me looks good but I keep getting an error, that of which is included below.

the code

def inversions  (string):
    res = 0
    for i  in  string:
        if string[i+1] < string[i]:
            res += 1
            print( res)

my errors

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    inversions  ('ghutinhjdbessadfg')
  File "C:\Users\Christopher\Downloads\pratice.py", line 63, in inversions
    if string[i+1] < string[i]:
TypeError: Can't convert 'int' object to str implicitly

I kinda get what the error is saying, but I am unsure of what to do about it.

Thank you very much

Christopher Jakob
  • 91
  • 1
  • 2
  • 11
  • 1
    the `i` variable from the for-loop represents a string...not a number, so using it as an index doesn't make sense unless `string` is a dictionary – smac89 Nov 10 '14 at 21:01
  • Doesn't look like a duplicate of that question. He also wants to know the number of consecutive letters that are out of order. – David Sanders Nov 10 '14 at 21:07
  • @alexthornton, padraiccunningham. Did you guys even read the question? clearly any guy with half a brain could clearly see this isn't a duplicate. I checked the other questions and they did not satisfy. Do not mark my questions unless you actually read the problem given in them. WTF – Christopher Jakob Nov 10 '14 at 21:20

3 Answers3

1

i is iterating over each letter, so you don't need to use it to index the string. The simpler way to do this would be to use the sorted function.

def alphabetical(s):
    return list(s) == sorted(s)

>>> alphabetical('abcxyz')
True
>>> alphabetical('zyxcba')
False
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

it is ambiguous what you mean by how many are out of order - for example the string

zabcdef

has all of the letters out of order. Is this what you mean, or do you want the number of operations required to re-order the string?

to check to see if strings is alphabetical, try the answer to this question:

checking if a string is in alphabetical order in python

def isInAlphabeticalOrder(word):
 for i in range(len(word) - 1):
    if word[i] > word[i + 1]:

        return False
return True

your problem is because you are using strings as list indices.

Taking your code as an example, I have modified the above to give

def number_out_of_place(word):
  out_of_place = 0
  for i in range(len(word) - 1):
    if word[i] > word[i + 1]:
        out_of_place += 1
  return out_of_place
Community
  • 1
  • 1
user3684792
  • 2,542
  • 2
  • 18
  • 23
  • It's not really ambiguous. His code demonstrates what he means. – David Sanders Nov 10 '14 at 21:04
  • the return false return true, statements are not useful though.. As you can see I have an accumulator worked into the code. The main purpose is to just count the number of times a letter is out of order. Example abdcgf has 2 letters out of order. – Christopher Jakob Nov 10 '14 at 21:10
  • i see, so your saying that if I rather define a range with i as you did on line three, then the computer will no longer view I as a string but as an index as I have attempted already? – Christopher Jakob Nov 10 '14 at 21:16
  • yes. you are trying to lookup string['a'] for example, when you want to look up string[1]. The list get item takes integers not chars – user3684792 Nov 10 '14 at 21:19
  • thank you all very much. And thank you @user3684792 your answer has been most helpful. – Christopher Jakob Nov 10 '14 at 21:21
0

Hint:

You can use ord(s) to convert a letter to a number representation. For a-z are in the right order for this to work, but you will have to handle the case of uppercase letters.

Dair
  • 15,910
  • 9
  • 62
  • 107