Created
February 27, 2014 02:19
-
-
Save jmMAGALLANES/9243039 to your computer and use it in GitHub Desktop.
a solution to an exercise!
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
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"<font color='red'> The calculator </font>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<font color='darkblue'>A possible solution to the exercise. You can copy and paste this code, and you should **write comments** as you understand what it is doing.</font>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def sumab (a,b):\n", | |
" res = a\n", | |
" for i in range(b):\n", | |
" res+=1\n", | |
" return res\n", | |
"\n", | |
"def subab (a,b):\n", | |
" res = a\n", | |
" for i in range(b):\n", | |
" res-=1\n", | |
" return res\n", | |
"\n", | |
"def multab(a,b):\n", | |
" res=0\n", | |
" for i in range(b):\n", | |
" res=sumab(res,a)\n", | |
" return res\n", | |
"\n", | |
"def divab(a,b):\n", | |
" \n", | |
" res=a\n", | |
" count=1\n", | |
" go=True\n", | |
" while res>0 or go:\n", | |
" res=subab(res,b)\n", | |
" if res<b:\n", | |
" go=False\n", | |
" break\n", | |
" else: \n", | |
" count+=1\n", | |
" return count\n", | |
"\n", | |
"def calculator(num1,num2,op):\n", | |
" if op in ['+']:\n", | |
" message=\"%g %s %g equals %g\"%(num1,op,num2,sumab(num1,num2) )\n", | |
" return message\n", | |
" if op in ['-']:\n", | |
" message=\"%g %s %g equals %g\"%(num1,op,num2,subab(num1,num2) )\n", | |
" return message\n", | |
" if op in ['x']:\n", | |
" message=\"%g %s %g equals %g\"%(num1,op,num2,multab(num1,num2) )\n", | |
" return message\n", | |
" if op in ['/']:\n", | |
" if num2==0:\n", | |
" return \"b is ZERO...aborting\" \n", | |
" elif num1<num2:\n", | |
" return \"%g %s %g equals %g\"%(num1,op,num2,0 )\n", | |
" elif num1==num2:\n", | |
" return 1\n", | |
" else:\n", | |
" message=\"%g %s %g equals %g\"%(num1,op,num2,divab(num1,num2) )\n", | |
" return message" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print calculator(10,33,'+')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10 + 33 equals 43\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print calculator(20,15,'-')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"20 - 15 equals 5\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print calculator(15,12,'x')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"15 x 12 equals 180\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print calculator(100,220,'/')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"100 / 220 equals 0\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print calculator(100,30,'/')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"100 / 30 equals 3\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print calculator(1000,25,'/')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1000 / 25 equals 40\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment