Skip to main content
Tweeted twitter.com/StackCodeReview/status/777711984930324481
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Make title a summary of the code as per site guidelines (see /help/how-to-ask)
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

My code for a function that checks Checking if a given string is in alphabetical order?

I wrote this function to check if a given string is in alphabetic order. How can I improve?

def alphacheck(x):
    '''This function checks if a given string is in Alphabetical order'''

    # Creating a variable to allow me to efficiently index a string of any length
    i = len(x) - 1
    # Using a recursive formula and checking for the base case when the string is of less than one character
    if len(x) <= 1:
        return True
    # This the recursive portion of the formula
    else:
        # This code checks if the last two characters are in alphabetical order
        # By utilizing the property that the letters of the alphabet increase in "value" accroding to python
        if x[i - 1] <= x[i]:
            # I remove the last letter of the string to reduce the probelm size
            x = x[:i]
            # I then repeat this on the smaller string until it reaches the base case or
            # Finds out the string is not in alphabetical order and returns False
            return alphacheck(x)
        else:
            return False

My code for a function that checks if a given string is in alphabetical order?

def alphacheck(x):
    '''This function checks if a given string is in Alphabetical order'''

    # Creating a variable to allow me to efficiently index a string of any length
    i = len(x) - 1
    # Using a recursive formula and checking for the base case when the string is of less than one character
    if len(x) <= 1:
        return True
    # This the recursive portion of the formula
    else:
        # This code checks if the last two characters are in alphabetical order
        # By utilizing the property that the letters of the alphabet increase in "value" accroding to python
        if x[i - 1] <= x[i]:
            # I remove the last letter of the string to reduce the probelm size
            x = x[:i]
            # I then repeat this on the smaller string until it reaches the base case or
            # Finds out the string is not in alphabetical order and returns False
            return alphacheck(x)
        else:
            return False

Checking if a given string is in alphabetical order

I wrote this function to check if a given string is in alphabetic order. How can I improve?

def alphacheck(x):
    '''This function checks if a given string is in Alphabetical order'''

    # Creating a variable to allow me to efficiently index a string of any length
    i = len(x) - 1
    # Using a recursive formula and checking for the base case when the string is of less than one character
    if len(x) <= 1:
        return True
    # This the recursive portion of the formula
    else:
        # This code checks if the last two characters are in alphabetical order
        # By utilizing the property that the letters of the alphabet increase in "value" accroding to python
        if x[i - 1] <= x[i]:
            # I remove the last letter of the string to reduce the probelm size
            x = x[:i]
            # I then repeat this on the smaller string until it reaches the base case or
            # Finds out the string is not in alphabetical order and returns False
            return alphacheck(x)
        else:
            return False
Source Link
ChrisIkeokwu
  • 495
  • 1
  • 9
  • 16

My code for a function that checks if a given string is in alphabetical order?

def alphacheck(x):
    '''This function checks if a given string is in Alphabetical order'''

    # Creating a variable to allow me to efficiently index a string of any length
    i = len(x) - 1
    # Using a recursive formula and checking for the base case when the string is of less than one character
    if len(x) <= 1:
        return True
    # This the recursive portion of the formula
    else:
        # This code checks if the last two characters are in alphabetical order
        # By utilizing the property that the letters of the alphabet increase in "value" accroding to python
        if x[i - 1] <= x[i]:
            # I remove the last letter of the string to reduce the probelm size
            x = x[:i]
            # I then repeat this on the smaller string until it reaches the base case or
            # Finds out the string is not in alphabetical order and returns False
            return alphacheck(x)
        else:
            return False