Project Euler — problem 22

[This article was first published on Tony's bubble universe » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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
lst <- scan("names.txt", what = "", sep = ",")
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"])
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 their blog: Tony's bubble universe » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)