Project Euler 17: Number letter counts

Number letter counts

WordItOut-word-cloud-859435

Warning: Please only read this post, when you are absolutely sure, that you don’t want to take part in the challenge that is Project Euler. My posts will spoil the solution for you. I’m posting my solutions to show one possible way on how to solve the problems in case you want to compare your solutions with mine or are stuck and gave up on solving the problem.

To solve Project Euler Problem 17 one has to sum up the number of letters required to write out all natural numbers from 1 to 1000. So you are supposed to count the letters from “one”, “two”, “three” … “one-thousand”. You don’t count spaces and hyphens in, meaning that “one-thousand” only contains eleven letters.

Num 2 Words

To write an algorithm, that returns the written string to a input number can be really complex. Especially since there are many exceptions one has to make in the algorithm for unorthodox exceptions like “eleven” or “twelve” and many others. But since we’re probably not the only ones, that ever needed python code, which translates numerical values to their respective written counterpart, there must be python modules available on the web to download.

It didn’t take me very long to find the python package num2words, which does exactly just that. The website is really helpful for beginners (like me) with a step-by-step guide showing you how to install this python package. After some command line magic this feat is done and we can now simply import the python package into our file like this:

from num2words import num2words

The package contains the function num2words(), that takes a integer as input parameter and returns the written number as string. So now we have all are necessary prerequisites to solve this problem.

Algorithm

I’ve written two helper functions, that simply return the number of hyphens and spaces in a given word:

def countSpaces(word): 
	"""Returns the number of spaces in parameter word"""
	return word.count(' ')


def countHyphens(word): 
	"""Returns the number of hyphens in parameter word"""
	return word.count('-')

It seems pretty odd to create a function, that only executes one line of code, but it helps others and yourself to better understand the code weeks later. Readability should always be a high priority when writing code, because no one wants to debug unreadable code. It’s just another obstacle, that makes reading code even more difficult, then it already is. So when you write some fancy, complicated code, at least write a comment that clearly explains what the code does.

Here is my complete code:

from num2words import num2words
import time

# start process time measure
t0 = time.clock()
# start wall time measure
t1 = time.time()

def countSpaces(word): 
	"""Returns the number of spaces in parameter word"""
	return word.count(' ')


def countHyphens(word): 
	"""Returns the number of hyphens in parameter word"""
	return word.count('-')

def letterCount(number):
	"""
	Returns the letter count of the paramter number, while 
	excluding hyphens and spaces.

	number - number you want to know the letter count of
	"""
	word = num2words(number)  
	return len(word) - countHyphens(word) - countSpaces(word)


"""Sum of all letter counts from 1 to 1000."""
result = sum(map(letterCount,range(1,1001)))	

print("Total letter count: " + str(result))

print("Seconds process time " + str(time.clock() - t0))
print("Seconds wall clock time " + str(time.time() - t1))

In line 30 I calculate the result with a one liner, that looks pretty similar to the code I used to solve Problem 16 (digit sum). Since range(1,1001) returns a list, we can therefore use map() to apply the function letterCount(), which returns the number of letters in the given number, while also excluding the spaces and hyphens, with our two previously defined functions countHyphens() and countSpaces(). Sum()  then simply sums up the list with the letter counts.

The program needs 0.070 seconds of processing time to solve the problem, which is sufficient.

This is the first (of probably many more) Project Euler problems, where I will use different libraries to make the problem easier to solve. It’s always important to be able to come up with your own algorithm, but at the same time you can waste a lot of time writing something, that isn’t really too complicated, but simply time consuming, where it makes more sense to use libraries created by other Python developers.

Hope it was a fun read, don’t be hesitant to send me suggestions on improving the algorithms via email at ratherreadblog@gmail.com or by leaving a comment at the post. You can also subscribe to my mailing list here if you always want to stay up-to-date with my newest posts and changes on the website.

 

The post Project Euler 17: Number letter counts appeared first on Rather Read.