Skip to content

Commit

Permalink
added algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestyalumni committed Oct 2, 2017
1 parent e41e844 commit 91db157
Show file tree
Hide file tree
Showing 13 changed files with 5,236 additions and 0 deletions.
474 changes: 474 additions & 0 deletions crack/BigO.ipynb

Large diffs are not rendered by default.

652 changes: 652 additions & 0 deletions crack/DataStruct.ipynb

Large diffs are not rendered by default.

175 changes: 175 additions & 0 deletions crack/Recursion.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"cf. [4.3. Calculating the Sum of a List of Numbers](http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsCalculatingtheSumofaListofNumbers.html)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25\n"
]
}
],
"source": [
"def listsum(numList):\n",
" theSum = 0\n",
" for i in numList:\n",
" theSum = theSum + i\n",
" return theSum\n",
"\n",
"print(listsum([1,3,5,7,9]))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25\n"
]
}
],
"source": [
"def listsum(numList):\n",
" if len(numList) == 1: # base case\n",
" return numList[0]\n",
" else:\n",
" return numList[0] + listsum(numList[1:]) # change its state and move toward base case with [1:]\n",
"\n",
"print(listsum([1,3,5,7,9]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[4.5. Converting an Integer to a String in Any Base](http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsConvertinganIntegertoaStringinAnyBase.html)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# base case - n < base\n",
"# n / base (division by the base will move us toward base case)\n",
"# call recursion on n/base \n",
"\n",
"def toStr(n,base):\n",
" convertString = \"0123456789ABCDEF\"\n",
" if n < base:\n",
" return convertString[n]\n",
" else:\n",
" return toStr(n // base, base) + convertString[n % base]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5AD\n"
]
}
],
"source": [
"print(toStr(1453,16))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def reverseStr(s):\n",
" if len(s) == 1:\n",
" return s\n",
" else:\n",
" return reverseStr(s[1:]) + s[0]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'olleh'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reverseStr(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit 91db157

Please sign in to comment.