summaryrefslogtreecommitdiff
path: root/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb')
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb144
1 files changed, 144 insertions, 0 deletions
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb
new file mode 100755
index 00000000..251e4767
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb
@@ -0,0 +1,144 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 03:ALGORITHMS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Found number 19 at the position 12\n"
+ ]
+ }
+ ],
+ "source": [
+ "def binarysearch(a,num): #function definition with its parameters 'a' is the inputlist\n",
+ " #and 'num' number to be found\n",
+ "\n",
+ " first=0 #initially the first position is zero\n",
+ " last=len(a)-1 #initially the last position is the total length of the inputlist-1\n",
+ " found=False #boolean value to indicate if the number to be searched is found or not.\n",
+ "\n",
+ " while first<=last and not found:\n",
+ " midpoint=(first+last)//2 #dividing the inputlist into two halves and comparing the number to be found with the midpoint.\n",
+ "\n",
+ " if a[midpoint]==num: #If the number to be found is equal to the midpoint returns the position.\n",
+ " found=True\n",
+ " else:\n",
+ " if num<a[midpoint]: #if the number to be found is less than the midpoint\n",
+ " #then the first half of the divided input list is taken for further computation.\n",
+ "\n",
+ " last=midpoint-1 #by assigning the last number of the first half(number before the midpoint) to the variable last.\n",
+ "\n",
+ "\n",
+ " else:\n",
+ " first=midpoint+1 #if the number to be found is greater than the midpoint\n",
+ " #then the second half of the divided input list is taken for further computation.\n",
+ " #by assigning the first number of the second half(number following the midpoint) to the variable first.\n",
+ "\n",
+ " return midpoint #returns the position of the number found in the list. \n",
+ "\n",
+ "\n",
+ "\n",
+ "numlist=[1, 23, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22] #List of inputs\n",
+ "print \"Found number 19 at the position\",(binarysearch(numlist, 19)) #Printing the position of the number to be found by a function call.\n",
+ " #The function binarysearch is called along with its parameters, inputlist\n",
+ " #and the number to be found.\n",
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 03:ALGORITHMS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 05: Page 198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[2, 3, 4, 1, 5]\n",
+ "[1, 2, 3, 4, 5]\n",
+ "[1, 2, 3, 4, 5]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To perform insertionsort\n",
+ "def sort_insertion(inputlist):\n",
+ "\n",
+ " for i in range(1,len(inputlist)):\n",
+ "\n",
+ " val_current = inputlist[i]\n",
+ " pos = i \n",
+ " \n",
+ " # check backwards through sorted list for proper pos of val_current\n",
+ " while((pos > 0) and (inputlist[pos-1] > val_current)):\n",
+ " inputlist[pos] = inputlist[pos-1]\n",
+ " pos = pos-1\n",
+ " \n",
+ " if pos != i:\n",
+ " inputlist[pos] = val_current \n",
+ " print(inputlist)\n",
+ " return inputlist\n",
+ "inputlist = [3,2,4,1,5]\n",
+ "print sort_insertion(inputlist)\n"
+ ]
+ }
+ ],
+ "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.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}