Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False

More on readability

For a really concise and insightful explanation of the purpose of comments, read rolfl's answer to this codereview questionthis codereview question.

I also highly recommend reading Robert Martin's Clean Code, as it goes more deeply into readability (among other things) — beyond comments and naming.

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False

More on readability

For a really concise and insightful explanation of the purpose of comments, read rolfl's answer to this codereview question.

I also highly recommend reading Robert Martin's Clean Code, as it goes more deeply into readability (among other things) — beyond comments and naming.

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False

More on readability

For a really concise and insightful explanation of the purpose of comments, read rolfl's answer to this codereview question.

I also highly recommend reading Robert Martin's Clean Code, as it goes more deeply into readability (among other things) — beyond comments and naming.

Extend answer to provide opportunity to learn more about the whys of commenting and naming advice.
Source Link
AleksG
  • 256
  • 1
  • 6

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False

More on readability

For a really concise and insightful explanation of the purpose of comments, read rolfl's answer to this codereview question.

I also highly recommend reading Robert Martin's Clean Code, as it goes more deeply into readability (among other things) — beyond comments and naming.

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False

More on readability

For a really concise and insightful explanation of the purpose of comments, read rolfl's answer to this codereview question.

I also highly recommend reading Robert Martin's Clean Code, as it goes more deeply into readability (among other things) — beyond comments and naming.

Reducing if-nesting makes the code more readable
Source Link
AleksG
  • 256
  • 1
  • 6

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    
   last_index if= len(input_string) <=- 1:
        return True
    else:
        last_index =if len(input_string) -<= 1:
        
   return True
    ifelif input_string[last_index - 1] <= input_string[last_index]:
            input_string = input_string[:last_index]
            return is_alphabetical_ordered(input_string)
        else:
            return False

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    
    if len(input_string) <= 1:
        return True
    else:
        last_index = len(input_string) - 1
        
        if input_string[last_index - 1] <= input_string[last_index]:
            input_string = input_string[:last_index]
            return is_alphabetical_ordered(input_string)
        else:
            return False

This would work assuming the input is only lowercase or only uppercase.

alphacheck('abc')

would return True, while

alphacheck('aBc')

would return False (because B < a < c)

You can solve this by converting the input string to lower or upper, like this:

x = x.lower()

It's good that you're trying to make the code readable, but IMO you're using too much comments and not expressive enough naming. It's often too easy to write comments that are just plain wrong, e.g. like here

# Using a recursive formula and checking for the base case when the string is of less than one character
if len(x) <= 1:

you're checking if x's length is less than or equal to 1, but in the comment you're saying that the check is for length < 1. It's a pretty unimportant mistake here, but it's just one of the many ways that excessive commenting can be bad. I would like less comments, and more expressive names -- something like this:

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

    input_string = input_string.lower()
    last_index = len(input_string) - 1
    
    if len(input_string) <= 1:
        return True
    elif input_string[last_index - 1] <= input_string[last_index]:
        input_string = input_string[:last_index]
        return is_alphabetical_ordered(input_string)
    else:
        return False
Source Link
AleksG
  • 256
  • 1
  • 6
Loading