euler/ipython/EulerProblem022.ipynb

127 lines
3.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 22\n",
"\n",
"Using names.txt (saved as EulerProblem022.txt in the same directory as this notebook), 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.\n",
"\n",
"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.\n",
"\n",
"What is the total of all the name scores in the file?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, this should be straight forward."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with open('EulerProblem022.txt', 'r') as f:\n",
" names = f.read().split(',')\n",
"\n",
"def get_score_for_name(name):\n",
" return sum([ord(c) - ord('A') + 1 for c in name if not c == '\"'])\n",
"\n",
"assert(get_score_for_name('COLIN') == 53)\n",
"names.sort()\n",
"s = sum([(i + 1) * get_score_for_name(name) for i, name in enumerate(names)])\n",
"assert(s == 871198282)\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I think nothing to explain here. The only question is what was if we hadn't the Python sort function to sort into alphabetical order, then we would have to write our own compare function and use it with what ever sorting algorithm."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Arnold before Felix\n",
"Felix before Felixb\n",
"Felix before Felixb\n",
"Felix is Felix\n"
]
}
],
"source": [
"def compare(a, b):\n",
" try:\n",
" for i in range(len(a)):\n",
" if a[i] < b[i]:\n",
" return '{} before {}'.format(a, b)\n",
" elif a[i] > b[i]:\n",
" return '{} before {}'.format(b, a)\n",
" except IndexError:\n",
" pass\n",
" if len(a) < len(b):\n",
" return '{} before {}'.format(a, b)\n",
" elif len(a) > len(b):\n",
" return '{} before {}'.format(b, a)\n",
" else:\n",
" return '{} is {}'.format(b, a)\n",
"\n",
"print(compare('Felix', 'Arnold'))\n",
"print(compare('Felix', 'Felixb'))\n",
"print(compare('Felixb', 'Felix'))\n",
"print(compare('Felix', 'Felix'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obviously, the algorithm would return True/False or 0/1/-1 for real sorting."
]
}
],
"metadata": {
"completion_date": "Fri, 5 Sep 2014, 15:24",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
},
"tags": [
"sorting",
"lexicographical order"
]
},
"nbformat": 4,
"nbformat_minor": 0
}