0

I wrote this function to check if a word is abecedarian but I can't seem to use a string as an argument. I didn't know this since I've never had to use a string argument until now. Is there a way around this?

def is_abecedarian(word):
    prev_char_ord = 0

    for char in word.lower():
        if prev_char_ord <= ord(char):
            prev_char_ord = ord(char)
        else:
            return False
    return True
2
  • 1
    "I can't seem to use a string as an argument" - what happens if you try? Commented Sep 5, 2014 at 22:32
  • Strings are just objects are just values and there is nothing inherently different when using them as arguments. If there is an error (or "don't work" behavior) is is in the usage of such argument/value - also do explain the problem in questions. Commented Sep 5, 2014 at 22:47

2 Answers 2

1

You can check the original word against the sorted word

def is_abecedarian(word):
    return word == ''.join(sorted(word))

Testing

>>> is_abecedarian('test')
False

>>> is_abecedarian('abcde')
True
Sign up to request clarification or add additional context in comments.

Comments

0

If you're setting prev_char_ord to 0 at the start of the function, no matter what character your string starts with, it will be higher than prev_char_ord.

Set prev_char_ord to either 255 (since ord only works for ascii characters), or use the first letter of your string to set the value (which can be accessed like an array, so the first character in lowercase would be word.lower()[0]).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.