Project Euler — problem 22

November 7, 2012
By

(This article was first published on Tony's bubble universe » R, and kindly contributed to R-bloggers)

Just had my supper. Stomach is full of stewed beef and potato.  I’d like to solve the 22nd Euler problem before tonight work (right, I’ll work late in my office).

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 * 53 = 49714. What is the total of all the name scores in the file?

Well, we need a converting table here, in which A, B, …, Z correspond to 1, 2, …, 26. Fortunately, there is a constant called “LETTERS” containing A, B, …, Z; if you are not aware of it, it’s OK to just type in c(“A”, “B”, …, “Z”).  Once having the converting table, the rest is very simple. Split the character string; find the corresponding number for each letter; sum the numbers up. The following is the solution code.

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
lst <- scan("names.txt", what = "", sep = ",")
lst[is.na(lst)] <- "NA"  # "NA" as a name? REALLY?!
lst <- sort(lst)
lst.len <- length(lst)
 
let2num <- data.frame(row.names = LETTERS, NUM = 1:26)  # the converting table
 
NameScore <- function(name) {
  # helper function to convert letter to its alphabetical order and sum up
  name.char <- unlist(strsplit(name, split = ""))
  score <- sum(let2num[name.char, "NUM"])
  return(score)
}
 
name.scores <- numeric(lst.len)
for (i in 1:lst.len) {
  name.scores[i] <- NameScore(lst[i])
}
result <- sum(name.scores * (1:lst.len))
cat("The result is:", result, "\n")

At last, I do have one issue on scan(). Does anybody know how to read in characters not affected by “NA”, which is a name here?

To leave a comment for the author, please follow the link and comment on his blog: Tony's bubble universe » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.