<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/euler-problem-22-names-scores/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-22-names-scores/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="http://picascii.com/"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/02/rlogoascii.png" data-orig-size="374,214" data-image-title="rlogoascii" class="wp-image-403" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="403" data-permalink="http://r.prevos.net/euler-problem-22-names-scores/rlogoascii/" alt="Euler Problem 22" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/rlogoascii-300x172.png?resize=300%2C172" data-large-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/02/rlogoascii.png" data-image-meta="{" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/rlogoascii-300x172.png?resize=300%2C172"></a>
<p class="wp-caption-text">R logo in ASCII art by picascii.com</p>
</div>
<p>Euler problem 22 is another trivial one that takes us to the realm of <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> codes. ASCII is a method to convert symbols into numbers, originally invented for telegraphs.</p>
<p>Back in the 8-bit days, <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/ASCII_art">ASCII art</a> was a method to create images without using lots of memory. Each image consists of a collection of text characters that give the illusion of an image. Euler problem 22 is, unfortunately, a bit less poetic.</p>
<h2>Euler Problem 22 Definition</h2>
<p>Using <a target="_blank" rel="nofollow" href="https://projecteuler.net/project/resources/p022_names.txt">names.txt</a>, 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.</p>
<p>For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938<sup>th</sup> name in the list. So, COLIN would obtain a score of 938 × 53 = 49,714.</p>
<p><a target="_blank" rel="nofollow" href="https://projecteuler.net/problem=22">What is the total of all the name scores in the file?</a></p>
<h2>Solution</h2>
<p>This code reads and cleans the file and sorts the names alphabetically. The <a target="_blank" rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/rawConversion.html">charToRaw</a> function determines the numerical value of each character in each name. This output of this function is the hex <a target="_blank" rel="nofollow" href="http://ascii.cl/">ASCII code</a> for each character. The letter A is number 65, so we subtract 64 from each value to get the sum total.</p>
<pre>
# ETL: reads the file and converts it to an ordered vector.
names &lt;- readLines(&quot;https://projecteuler.net/project/resources/p022_names.txt&quot;, warn = F)
names &lt;- unlist(strsplit(names, &quot;,&quot;))
names &lt;- gsub(&quot;[[:punct:]]&quot;, &quot;&quot;, names)
names &lt;- sort(names)

# Total Name scores
answer &lt;- 0
for (i in names) {
    value &lt;- sum(sapply(unlist(strsplit(i, &quot;&quot;)), function(x) as.numeric(charToRaw(x)) - 64))
    value &lt;- value * which(names==i)
    answer &lt;- answer + value
}
print(answer)
</pre>
<p>We can have a bit more fun with this problem by comparing this list with the <a target="_blank" rel="nofollow" href="https://www.babycenter.com/top-baby-names-2016.htm">most popular baby names in 2016</a>. The first section of the code extracts the list of popular names from the website. The rest of the code counts the number of matches between the lists.</p>
<pre>
# Most popular baby names
library(rvest)
url &lt;- &quot;https://www.babycenter.com/top-baby-names-2016.htm&quot;
babynames &lt;- url %>%
    read_html() %>%
    html_nodes(xpath = '//*[@id=&quot;babyNameList&quot;]/table') %>%
    html_table()
babynames &lt;- babynames[[1]]

# Convert Project Euler list and test for matches
proper=function(x) paste0(toupper(substr(x, 1, 1)), tolower(substring(x, 2)))
names &lt;- proper(names)

sum(babynames$GIRLS %in% names)
sum(babynames$BOYS %in% names)
</pre>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-22-names-scores/">Euler Problem 22 : Names Scores</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">The Devil is in the Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/euler-problem-22-names-scores/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Z86yjwn714Q"></a></div>