Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "830b82ae-eee6-4258-83ed-d28379a8a478", "metadata": {}, "source": [ "### Q22\n", "What is the total of all the name scores in the [file](/notebook/raw/q22_data.txt)?" ] }, { "cell_type": "code", "execution_count": 1, "id": "e92c7d68-7d67-42d8-b680-0bbb4186817f", "metadata": {}, "outputs": [], "source": [ "raw_names_list = open(\"q22_data.txt\",\"r\").read()" ] }, { "cell_type": "code", "execution_count": 2, "id": "5593e1a9-8016-4907-95db-acbaa9eea4fe", "metadata": {}, "outputs": [], "source": [ "def char_score(char):\n", " return ord(char.upper())-64\n", "\n", "def total_names_scores(raw_names_list):\n", " return sum([ (i+1)*sum([char_score(c) for c in name]) \n", " for i,name in enumerate(sorted(\n", " raw_names_list.replace('\"',\"\").split(\",\")\n", " ))])\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "27c1ab37-cdf0-452f-93e0-d6689aed3145", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 5.45 ms, sys: 1.21 ms, total: 6.66 ms\n", "Wall time: 6.45 ms\n" ] }, { "data": { "text/plain": [ "871198282" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "total_names_scores(raw_names_list)" ] }, { "cell_type": "code", "execution_count": null, "id": "037b11a4-6710-43c4-8248-6949ff9917d9", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q21
Next Notebook:
Project Euler Q23
Loading