Last active
June 6, 2018 13:49
-
-
Save Macorreag/89c34762aceea7acea00f3ac8135d7c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Pancakes Taller 0\n", | |
"\n", | |
"\n", | |
"[Taller](http://disi.unal.edu.co/~algorithms/pancakes/pancakes.html)\n", | |
"* [Miller Correa](https://www.github.com/macorreag) \n", | |
"* [Brayan Hurtado](https://www.github.com/Bryan9712) \n", | |
"* [Miguel Medellin](https://www.github.com/chranium) \n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1 Complete the flips and number of flips for the 4 and 5 element permutations in the files\n", | |
"\n", | |
"[XLS with analisis](https://docs.google.com/spreadsheets/d/1sXofQAiNDFXgDBhPvJ2Rx2e0VRTP6M5Gicai2AnYtsg/edit?usp=sharing)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Write a Phyton program for one spatula flips that recives the size of the permutation n numbers and control parameter c,\n", | |
"if the paramyter c is 0 writes Pn; if the paramtere is c is 1 writes Pn and all the permutations (in xls and xml) that\n", | |
"require Pn flips ant the corresponding flips; and if the parameter c is 2 writes Pn and all the permutations (in xls and xml) ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Original\n", | |
"************************\n", | |
" ---3---\n", | |
" ------6------\n", | |
" --2--\n", | |
" 0\n", | |
"-------7-------\n", | |
" -----5-----\n", | |
" ----4----\n", | |
"************************\n", | |
"************************\n", | |
"Step 1\n", | |
"************************\n", | |
"-------7-------\n", | |
" 0\n", | |
" --2--\n", | |
" ------6------\n", | |
" ---3---\n", | |
" -----5-----\n", | |
" ----4----\n", | |
"************************\n", | |
"************************\n", | |
"Step 2\n", | |
"************************\n", | |
" ----4----\n", | |
" -----5-----\n", | |
" ---3---\n", | |
" ------6------\n", | |
" --2--\n", | |
" 0\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Step 3\n", | |
"************************\n", | |
" ------6------\n", | |
" ---3---\n", | |
" -----5-----\n", | |
" ----4----\n", | |
" --2--\n", | |
" 0\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Step 4\n", | |
"************************\n", | |
" 0\n", | |
" --2--\n", | |
" ----4----\n", | |
" -----5-----\n", | |
" ---3---\n", | |
" ------6------\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Step 5\n", | |
"************************\n", | |
" -----5-----\n", | |
" ----4----\n", | |
" --2--\n", | |
" 0\n", | |
" ---3---\n", | |
" ------6------\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Step 6\n", | |
"************************\n", | |
" ---3---\n", | |
" 0\n", | |
" --2--\n", | |
" ----4----\n", | |
" -----5-----\n", | |
" ------6------\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Step 7\n", | |
"************************\n", | |
" --2--\n", | |
" 0\n", | |
" ---3---\n", | |
" ----4----\n", | |
" -----5-----\n", | |
" ------6------\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Step 8\n", | |
"************************\n", | |
" 0\n", | |
" --2--\n", | |
" ---3---\n", | |
" ----4----\n", | |
" -----5-----\n", | |
" ------6------\n", | |
"-------7-------\n", | |
"************************\n", | |
"************************\n", | |
"Total Steps: 8\n" | |
] | |
} | |
], | |
"source": [ | |
"def printCakes(cakes):\n", | |
" total = len(cakes)\n", | |
" for cake in cakes:\n", | |
" for i in range(total-cake):\n", | |
" print(' ',end='')\n", | |
" for i in range(cake):\n", | |
" print('-',end='')\n", | |
" print(cake,end='')\n", | |
" for i in range(cake):\n", | |
" print('-',end='')\n", | |
" print()\n", | |
" printLine()\n", | |
" \n", | |
" printLine()\n", | |
" \n", | |
"def flipCakes(cakes,position):\n", | |
" top_cakes = cakes[0:position+1]\n", | |
" top_cakes.reverse()\n", | |
" bottom_cakes = cakes[position+1:]\n", | |
" return top_cakes+bottom_cakes\n", | |
" \n", | |
"def printLine():\n", | |
" print('************************')\n", | |
" \n", | |
"def arrangeCakes(cakes):\n", | |
" largest = len(cakes)\n", | |
" count = 0\n", | |
" while largest != 1:\n", | |
" if(largest != (cakes.index(largest)+1)):\n", | |
" #if the next largest pancake to position is not on the top, flip to get it there\n", | |
" if(cakes.index(largest)!= 0):\n", | |
" cakes = flipCakes(cakes, cakes.index(largest))\n", | |
" count += 1\n", | |
" print('Step',count)\n", | |
" printLine()\n", | |
" printCakes(cakes)\n", | |
" \n", | |
" #once the next largest pancake to position is one top, flip\n", | |
" cakes = flipCakes(cakes,largest -1)\n", | |
" count += 1\n", | |
" largest -= 1\n", | |
" print('Step',count)\n", | |
" printLine()\n", | |
" printCakes(cakes)\n", | |
" else:\n", | |
" largest -= 1\n", | |
" return count\n", | |
" \n", | |
"#Example Data\n", | |
"my_cakes = [3,6,2,0,7,5,4]\n", | |
"\n", | |
"print('Original')\n", | |
"printLine()\n", | |
"printCakes(my_cakes)\n", | |
" \n", | |
"count = arrangeCakes(my_cakes) \n", | |
" \n", | |
"print('Total Steps:',count)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"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.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment