What number is spelled in alphabetical order? (With Python Code to prove it)

What Number Is Spelled in Alphabetical Order? (With Python code to prove it)

One of the best parts of mastering a programming language is that you can apply it to almost anything in life. For example, imagine someone’s told you that 40 is the only number that, when written out in English, is in alphabetical order. You could just nod and take their word for it. Or you could actually prove whether it’s true or not by writing a Python script. This might seem like a convoluted idea at first. And it’s true that in many programming languages you’d have to do some fairly tedious juggling of variables as you converted and managed them. But as you’ll soon see, we can prove whether or not 40 is the only alphabetical number in English relatively easily by using Python.

Creating a Solid Foundation

It’s always a good idea to think about our methodology before writing a Python script. The most important question to go over at the start of the process is whether or not we’ll make use of third-party code. Python’s popular enough that the vast majority of tasks have pre-written libraries that can make the process easier. In this particular case, we could use num2words to easily convert ints to their equivalent word in English. But it’s generally best to begin by understanding how these feats are accomplished in the first place. So instead of using an existing library, we’ll write all of the functionality ourselves. This will also let us tailor everything to our needs.

We know that verifying whether forty is the only numerical representation in English that’s in alphabetical order is our ultimate end goal. And we can generally reason backward from that point. The term “only” is a little nebulous though. How big of a scope does that actually entail? Thankfully, the English language inserts more and more repetition into the names of numbers as they go higher. Once we’re past one hundred the additional iterations won’t change whether it’s in alphabetical order or not. So we can limit the scope of our test to numbers under 101. We can exclude negative numbers and decimals for a similar reason.

We also want to be able to easily show people the logic behind our results if they question our methodology. As such, we need to structure the code so it’s fairly self-evident how and why we’re performing any given action. With all of that in mind, we can actually lay out our code.

Diving Into the Code

Python’s a flexible language that gives us a multitude of different ways to approach any topic. But with the previous criteria in mind, we have some best-use options laid out for us in advance. Take a look at the following code.

def fillNumberList(filledList):
    singleDigits = [“one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”]
    teenDigits = [“ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”]
    tensDigits = [“twenty”, “thirty”, “forty”, “fifty”, “sixty”, “seventy”, “eighty”, “ninety”]

    for singleName in singleDigits + teenDigits:
        filledList.append(singleName)
    for tensName in tensDigits:
        filledList.append(tensName)
        for combinedName in singleDigits:
            filledList.append(tensName + “-” + combinedName)
    filledList.append(“one hundred”)
    return filledList

def isAlphabetical(ourNumber):
    ourNumber = ourNumber.replace(“-“, “”)
    toSortList = list(ourNumber)
    toSortList.sort()
    return toSortList == list(ourNumber)

def testListAlphabetical(ourTestingList):
    for i in ourTestingList:
        if isAlphabetical(i):
            print(i + ” is in alphabetical order”)
def testListReverseAlphabetical(ourTestingList):
    for i in ourTestingList:
        if isAlphabetical(i[::-1]):
            print(i + ” is in reverse alphabetical order”)
def testListNotAlphabetical(ourTestingList):
    for i in ourTestingList:
        if not isAlphabetical(i):
            print(i + ” is not in alphabetical order”)

ourNumList = []
ourNumList = fillNumberList(ourNumList)

testListAlphabetical(ourNumList)
#testListReverseAlphabetical(ourNumList)
#testListNotAlphabetical(ourNumList)

We begin with a number of new functions, the first of which is fillNumberList. This function will take a list and populate it with the English words for all of the numbers between one and one hundred. Unfortunately, the English language isn’t as elegant with numbers as we might wish. We essentially need to write out the numbers one to nineteen by hand. We’ll break them up even further into single and teen digits. One to nine are in the single digits. And ten to nineteen are in the teen digits. Finally, we make a list with the text for the tens position. We have all the words needed to write out the numbers between one and one hundred stored within these three lists.

And we proceed to do so in the following loops. As we loop through the names we append them into filledList. When we come to numbers that combine two separate words we need to hyphenate them for proper grammar. This is somewhat at odds with our later work alphabetizing them. But we want to make sure we’re working with properly generated and formatted strings. Finally, we top off the list with a “one hundred” string. If we were going to cycle past that point we’d want to use an additional hundredsDigits list. But, since we’re not, we just return the newly filled word list of one hundred strings.

The next function is called isAlphabetical, and it uses a single string as an argument. It’s a fairly short function that begins by stripping the hyphen from the string passed as ourNumber. We create a list from the characters in ourNumber as toSortList. We then use the sort method on it. Sort defaults to ordering items by descending value, and this includes the letters of the English alphabet. If the original string was already in alphabetical order then it will have the same structure as the alphabetical sequence in toSortList. And if that’s the case we return true. If not, we return false.

The next function is called testListAlphabetical and it takes a list as an argument. This will be the list of numbers generated in fillNumberList. The function simply loops through that list and passes each string to isAlphabetical. If the string is alphabetical the function will print it and the positive result to the screen before continuing on to the next item.

After testListAlphabetical we have a slight variation on it called testListReverseAlphabetical. This demonstrates how easy it is to perform a similar operation on our data now that it’s been properly formatted. We essentially perform the same task, but this time around we flip the number strings in order to see if any of them are in reverse alphabetical order. We handle this by using a string slice at a negative position. This starts us at the end of the string and iterates the numerical order backward. It then proceeds in the same way as testListAlphabetical.

The next function is called testListNotAlphabetical and it operates in a similar way to the two previous items. This is a test function that can tell us what numbers aren’t ordered alphabetically.

Following the functions, we come to the code which will actually get things moving. We begin by declaring a new list called ourNumList and passing it to our fillNumberList function. With the list generated, we can pass it to any of the TestList functions. For this example only testListAlphabetical is active. But you can try out the others by simply uncommenting them.

When you run the code you’ll see that the earlier statement about alphabetical numbers is indeed correct. Forty is the only number that’s ordered alphabetically. Though if you run the testListReverseAlphabetical function you’ll see that one is the sole representative that’s ordered alphabetically in reverse.

It’s also important to note that this is just one way to approach the problem. If we wanted to create a more robust library rather than put a single idea to the test we could consolidate some of the functionality distributed in multiple functions. But since clarity is ultimately the most important part of the script it’s better to separate functionality into distinct blocks.

What number is spelled in alphabetical order? (With Python Code to prove it)
Scroll to top