Duncan's blog

November 1, 2008

Project Euler: problem 22

Filed under: Coldfusion,Project Euler — duncan @ 7:00 am
Tags: , , ,

Problem 22:

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 x 53 = 49714.

What is the total of all the name scores in the file?

Names.txt is in the format “MARY”,”PATRICIA”,”LINDA”,”BARBARA”,”ELIZABETH”, etc.

<cffile	action="read" 
	file="#GetDirectoryFromPath(GetCurrentTemplatePath())#names.txt" 
	variable="namesFile">

<cfset names = ArrayNew(1)>
<cfset total = 0>

<!--- transfer names into an array --->
<cfloop index="i" list="#namesFile#">
	<cfset ArrayAppend(names, ReplaceNoCase(i, '"', '', 'ALL'))>
</cfloop>

<cfset ArraySort(names, "text")>

<cfloop index="i" from="1" to="#ArrayLen(names)#">
	<cfset sum = 0>
	
	<cfloop index="j" from="1" to="#Len(names[i])#">
		<cfset sum = sum + Asc(Mid(names[i], j, 1)) - 64>
	</cfloop>
	
	<cfset total = total + (sum * i)>
</cfloop>

<cfoutput><strong>#total#</strong></cfoutput>

Read in the file using <CFFile action=”read”>. Using GetDirectoryFromPath(GetCurrentTemplatePath()) allows us to avoid having to hardcode a filepath into the tag, it’ll basically put the path for the current page, i.e. the names.txt file is sitting in the same directory as this .cfm file.

Loop through the whole file as a list, and transfer it to an array. In Coldfusion array functions are usually quicker than the equivalent list functions; and being in an array makes our life slightly easier for this problem.

Get rid of the surrounding " " from the names as you insert them to the array. Alternatively we could have just used Replace() against our initial variable namesFile to get rid of them all at the start.

Once the array’s populated, sort it alphabetically. I made the assumption that all names were in upper case, and there wouldn’t be any mixed case. If there was, I’d have had to decide whether to use sort=text (sorts it by all lower case letters, then all upper case letters) or sort=textnocase (ignore the case).

Then loop through the array again. For each name, loop through it, working out the value of each letter from 1-26. As I’d assumed it was all upper case, I’m subtracting 64 from the value, I’m using the Asc() function to get each character’s ASCII value. Upper-case ASCII letters go from 65-90, so I’m subtracting 64 to convert that to 1-26. If it was mixed case, I’d have to be a bit cleverer about it.

Add up the values of those letters, multiply it by i (its alphabetical position in our array), and add that to a running total. Simple!

2 Comments »

  1. […] Euler, triangle number, triangle numbers, triangular number, triangular numbers After completing problem 22, I spotted this one that looked quite […]

    Pingback by Project Euler: problem 42 « Duncan’s blog — November 3, 2008 @ 7:19 am | Reply

  2. […] previously blogged about this Project Euler puzzle nearly 6 years ago, using ColdFusion.  This is my approach using PHP as a simple practical exercise for myself, and […]

    Pingback by Project Euler: problem 22 (PHP) | Duncan's blog — October 5, 2014 @ 8:09 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: