palindrom upd

This commit is contained in:
rasbt 2014-04-19 07:39:41 -04:00
parent 215228201d
commit a333cd075e
2 changed files with 191 additions and 8 deletions

View File

@ -0,0 +1,165 @@
{
"metadata": {
"name": "",
"signature": "sha256:710150d6bf8fe09c3a2348c765457cdea364ebe260d10f57be9349b32ef3ff50"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sebastian Raschka 04/2014\n",
"\n",
"#Timing different Implementations of palindrome functions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import re\n",
"import timeit\n",
"import string\n",
"\n",
"# All functions return True if an input string is a palindrome. Else returns False.\n",
"\n",
"\n",
"\n",
"####\n",
"#### case-insensitive ignoring punctuation characters\n",
"####\n",
"\n",
"def palindrome_short(my_str):\n",
" stripped_str = \"\".join(l.lower() for l in my_str if l.isalpha())\n",
" return stripped_str == stripped_str[::-1]\n",
"\n",
"def palindrome_regex(my_str):\n",
" return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n",
"\n",
"def palindrome_stringlib(my_str):\n",
" LOWERS = set(string.ascii_lowercase)\n",
" letters = [c for c in my_str.lower() if c in LOWERS]\n",
" return letters == letters[::-1]\n",
"\n",
"####\n",
"#### functions considering all characters (case-sensitive)\n",
"####\n",
"\n",
"def palindrome_reverse1(my_str):\n",
" return my_str == my_str[::-1]\n",
"\n",
"def palindrome_reverse2(my_str):\n",
" return my_str == ''.join(reversed(my_str))\n",
"\n",
"def palindrome_recurs(my_str):\n",
" if len(my_str) < 2:\n",
" return True\n",
" if my_str[0] != my_str[-1]:\n",
" return False\n",
" return palindrome(my_str[1:-1])\n",
"\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"test_str = \"Go hang a salami. I'm a lasagna hog.\"\n",
"\n",
"print('case-insensitive functions ignoring punctuation characters')\n",
"%timeit palindrome_short(test_str)\n",
"%timeit palindrome_regex(test_str)\n",
"%timeit palindrome_stringlib(test_str)\n",
"\n",
"print('\\n\\nfunctions considering all characters (case-sensitive)')\n",
"%timeit palindrome_reverse1(test_str)\n",
"%timeit palindrome_reverse2(test_str)\n",
"%timeit palindrome_recurs(test_str)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"case-insensitive functions ignoring punctuation characters\n",
"100000 loops, best of 3: 15.4 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 20.1 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"100000 loops, best of 3: 12.3 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"\n",
"functions considering all characters (case-sensitive)\n",
"1000000 loops, best of 3: 500 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"100000 loops, best of 3: 2.93 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000000 loops, best of 3: 505 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:842cb7ae4f520376ff820e7bcd278db91a026ac1fa159ca6e8a4b38296bc10f5" "signature": "sha256:710150d6bf8fe09c3a2348c765457cdea364ebe260d10f57be9349b32ef3ff50"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -23,6 +23,7 @@
"input": [ "input": [
"import re\n", "import re\n",
"import timeit\n", "import timeit\n",
"import string\n",
"\n", "\n",
"# All functions return True if an input string is a palindrome. Else returns False.\n", "# All functions return True if an input string is a palindrome. Else returns False.\n",
"\n", "\n",
@ -39,6 +40,11 @@
"def palindrome_regex(my_str):\n", "def palindrome_regex(my_str):\n",
" return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n", " return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n",
"\n", "\n",
"def palindrome_stringlib(my_str):\n",
" LOWERS = set(string.ascii_lowercase)\n",
" letters = [c for c in my_str.lower() if c in LOWERS]\n",
" return letters == letters[::-1]\n",
"\n",
"####\n", "####\n",
"#### functions considering all characters (case-sensitive)\n", "#### functions considering all characters (case-sensitive)\n",
"####\n", "####\n",
@ -54,12 +60,15 @@
" return True\n", " return True\n",
" if my_str[0] != my_str[-1]:\n", " if my_str[0] != my_str[-1]:\n",
" return False\n", " return False\n",
" return palindrome(my_str[1:-1])\n" " return palindrome(my_str[1:-1])\n",
"\n",
"\n",
"\n"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 6 "prompt_number": 1
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -70,6 +79,7 @@
"print('case-insensitive functions ignoring punctuation characters')\n", "print('case-insensitive functions ignoring punctuation characters')\n",
"%timeit palindrome_short(test_str)\n", "%timeit palindrome_short(test_str)\n",
"%timeit palindrome_regex(test_str)\n", "%timeit palindrome_regex(test_str)\n",
"%timeit palindrome_stringlib(test_str)\n",
"\n", "\n",
"print('\\n\\nfunctions considering all characters (case-sensitive)')\n", "print('\\n\\nfunctions considering all characters (case-sensitive)')\n",
"%timeit palindrome_reverse1(test_str)\n", "%timeit palindrome_reverse1(test_str)\n",
@ -92,7 +102,15 @@
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"\n", "\n",
"10000 loops, best of 3: 20.3 \u00b5s per loop" "10000 loops, best of 3: 20.1 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"100000 loops, best of 3: 12.3 \u00b5s per loop"
] ]
}, },
{ {
@ -103,7 +121,7 @@
"\n", "\n",
"\n", "\n",
"functions considering all characters (case-sensitive)\n", "functions considering all characters (case-sensitive)\n",
"1000000 loops, best of 3: 508 ns per loop" "1000000 loops, best of 3: 500 ns per loop"
] ]
}, },
{ {
@ -111,7 +129,7 @@
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"\n", "\n",
"100000 loops, best of 3: 3 \u00b5s per loop" "100000 loops, best of 3: 2.93 \u00b5s per loop"
] ]
}, },
{ {
@ -119,7 +137,7 @@
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"\n", "\n",
"1000000 loops, best of 3: 509 ns per loop" "1000000 loops, best of 3: 505 ns per loop"
] ]
}, },
{ {
@ -130,7 +148,7 @@
] ]
} }
], ],
"prompt_number": 7 "prompt_number": 2
}, },
{ {
"cell_type": "code", "cell_type": "code",