Skip to content

Commit

Permalink
rod cutting and memoization is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
nurfnick committed Oct 26, 2023
1 parent 515baaa commit 671ad22
Showing 1 changed file with 92 additions and 27 deletions.
119 changes: 92 additions & 27 deletions AstrosSuck.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPtE9KzkDB5CD9eVNkgZRAz",
"authorship_tag": "ABX9TyOAmJRrll3WB5UfByEwYzIO",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -398,7 +398,7 @@
"metadata": {
"id": "GLxFdoVBWNP1"
},
"execution_count": 6,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -413,7 +413,7 @@
"id": "WfbetcEjeEPG",
"outputId": "03a44a55-7cdc-4eb9-9994-80017b62e986"
},
"execution_count": 7,
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -441,67 +441,132 @@
{
"cell_type": "code",
"source": [
"p = [1,5,8,9,10]"
"p = [0,1,5,8,9,10,17,17,20,24,30]"
],
"metadata": {
"id": "eoVRbaHkeKIG"
},
"execution_count": 17,
"execution_count": 51,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def RodCutting(p,l):\n",
" if l<=0:\n",
" if l==0:\n",
" return 0\n",
" else:\n",
" return max((p[i]+RodCutting(p,l-i) for i in range(l)))"
" return max(p[i]+RodCutting(p,l-i) for i in range(1,l+1))"
],
"metadata": {
"id": "Jd07MLgTiJ5H"
},
"execution_count": 20,
"execution_count": 52,
"outputs": []
},
{
"cell_type": "code",
"source": [
"RodCutting(p,5)"
"RodCutting(p,10)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 322
"base_uri": "https://localhost:8080/"
},
"id": "wQuKkSy7j9TI",
"outputId": "5b6d7026-e6b4-4946-984f-7335fa9325df"
"outputId": "dbb7353d-c007-4ad7-87ce-892ff602ba2e"
},
"execution_count": 21,
"execution_count": 53,
"outputs": [
{
"output_type": "error",
"ename": "RecursionError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-21-2d4e6ac37040>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mRodCutting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-20-78fc1a0e3b6e>\u001b[0m in \u001b[0;36mRodCutting\u001b[0;34m(p, l)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mRodCutting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-20-78fc1a0e3b6e>\u001b[0m in \u001b[0;36m<genexpr>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mRodCutting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"... last 2 frames repeated, from the frame below ...\n",
"\u001b[0;32m<ipython-input-20-78fc1a0e3b6e>\u001b[0m in \u001b[0;36mRodCutting\u001b[0;34m(p, l)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mRodCutting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mRecursionError\u001b[0m: maximum recursion depth exceeded"
]
"output_type": "execute_result",
"data": {
"text/plain": [
"30"
]
},
"metadata": {},
"execution_count": 53
}
]
},
{
"cell_type": "code",
"source": [],
"source": [
"r = [0]\n",
"\n",
"def RodCutMemo(p,r,l):\n",
" if len(r)>l:\n",
" return r[l]\n",
" else:\n",
" for j in range(len(r),l+1):\n",
" r.append(max(p[i]+RodCutMemo(p,r,j-i) for i in range(1,j+1)))\n",
" return r[l]"
],
"metadata": {
"id": "CBCpbRrSkAc2"
},
"execution_count": 101,
"outputs": []
},
{
"cell_type": "code",
"source": [
"RodCutMemo(p,r,5)"
],
"metadata": {
"id": "rD8bMhVhuCxz",
"outputId": "9bb55847-f7bc-4c4b-e288-4b6123d3f757",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 102,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"13"
]
},
"metadata": {},
"execution_count": 102
}
]
},
{
"cell_type": "code",
"source": [
"r"
],
"metadata": {
"id": "BxoHkCL4uGy8",
"outputId": "64d83c24-6952-4f4f-95ab-44835f851e2b",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 103,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[0, 1, 5, 8, 10, 13]"
]
},
"metadata": {},
"execution_count": 103
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "kGtsB8ifxllG"
},
"execution_count": null,
"outputs": []
}
Expand Down

0 comments on commit 671ad22

Please sign in to comment.