python - checking a string to see if it is in alphabetical order -
this question has reply here:
how check if characters in string alphabetically ordered 5 answersi learning python. current self taught assignment write programme takes string , checks weather or not the letters in string in alphabetical order. if not, programme homecoming number of instances letters out of order. inquire help of stackoverflow, code me looks maintain getting error, of included below.
the code
def inversions (string): res = 0 in string: if string[i+1] < string[i]: res += 1 print( res)
my errors
traceback (most recent phone 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 str implicitly
i kinda error saying, unsure of it.
thank much
it ambiguous mean how many out of order - illustration string
zabcdef
has of letters out of order. mean, or want number of operations required re-order string?
to check see if strings alphabetical, seek reply question:
checking if string in alphabetical order in python
def isinalphabeticalorder(word): in range(len(word) - 1): if word[i] > word[i + 1]: homecoming false homecoming true
your problem because using strings list indices.
taking code example, have modified above give
def number_out_of_place(word): out_of_place = 0 in range(len(word) - 1): if word[i] > word[i + 1]: out_of_place += 1 homecoming out_of_place
python
No comments:
Post a Comment