0% found this document useful (0 votes)
36 views12 pages

Python Material Chapter-3-2024

Uploaded by

trushalkjk9227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views12 pages

Python Material Chapter-3-2024

Uploaded by

trushalkjk9227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.

(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

CHAPTER-3
Plotting Using PyLab

 Plotting using PyLab


 Plotting Mortgages
 Extended Example
 Fibonacci Sequence Revisited
 Dynamic Programming
 0/1 Knapsack Algorithm
 Dynamic Programming with Divide and
Conquer

79
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

Q-1 Explain How to plotting using PyLab.

Detail :-
PyLab is a module inside MATPLOTLIB library.
MATPLOTLIB was developed by John . D. Hunter in 2003.
MATPLOTLIB has it’s roots in MATLAB which need to decide PyLab.
MATLAB support many built – in function for users to develop the code.
It become easy for MATLAB user who don’t want to use import statement.
PyLab having number of functions and classes for generating the drawings.
MATPLOTLIB in python having 1000 lines of code to create quality graphics.

 MATPLOTLIB :-

MATPLOTLIB is free python library for generating plots in 1D ,


2D Graphics.
MATPLOTLIB is easy to use interface for PyLab modules.

Example :-
80
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

Import matplotlib.pyplot as plt


Import numpy as np
X = np.linspace(0,10,100)
Plt.plot(x , x.Label = “linear”)
Plt.legend()
Plt.show()

1 Word Question – Answer

SR.NO QUESTION ANSWER


.
1 MATPLOTLIB was developed by John . D. Hunter
in 2003.
2 is a module inside PyLab
MATPLOTLIB library.
3 _is free python library for MATPLOTLIB
generating plots in 1D , 2D Graphics.
4 support many built – in MATLAB
function for users to develop the code.

Q-2 Explain Plotting Mortgage in brief.

81
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

Detail :-
 Mortgage is a simple calculation to find out or understand true cost of any
loan or interest.
 To install mortgage you have to write following command at CMD prompt.

o Pip install mortgage

 The above package can provide easy way to compare different mortgages.
 It you are consider in mortgage loan , you should understand all the dtails
about how principal and interest will be calculated.
 It will be also easy to find out monthly payment system for mortgage loan
which include following :
o HOA :- [ Home Owner Association fee ]
o PMI :- [Private Mortgage Insurance fee ]
o Home Owner Insurance
o Taxes

 Here are some common tricks to find out mortgage loan calculation :-
1. Provide sales price of house.
2. Enter down payment.

82
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

3.Calculate loan amount.


4.Enter loan terms in years.
5.Enter interest rate.
6.Calculate monthly payment.
7.Calculate final balance and monthly interest.
Example :-

Calculate simple mortgage


From mortgage import loan
l = Loan(p=2,00,000 , i=0.06 , term = 30)
l.summarize
>>>original balance : 2,00,000
>>>interest rate : 0.06%
>>>terms : 30 years
>>>monthly payment :

1 Word Question – Answer

sr.
no
. is a simple calculation
1 to find Mortgage
out or understand true cost of any loan or
interest.
To install mortgage you have to write
Pip install mortgage
command.

Q-3 Explain Fibonacci sequence revisited with example.

83
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Detail :-
 One of the most common type of math based technical challenges are ones
that deal with Fibonacci sequence.
 Each new term in the Fibonacci sequence is generated by adding the
previous two terms.
 For example , starting with 1 and 2 ,the first 10 numbers in the sequence
would be :

o 1,2,3,5,8,13,21,34,55,89

 One of the favourite challenge that deals with Fibonacci sequence is one that
asks for index value of some high number in the sequence.
 It might be good idea to record the value returned by the first call , and then
look it up rather than compute it each time it is needed.This is called
“memorization”.
 “Memorization” is key idea behind any dynamic programming.

 Normally , easy way to go about doing something like would be to put all
the numbers in array and then cycle them with for loop.
 First it requires two different functions , one function to generate Fibonacci
sequence and second function to cycle through all the numbers we have
generated.
 Let’s see ,implementation of Fibonacci sequence by following figure.

84
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot – 360001

As above figure , look at tree of calls associated with the invocation fib(6).
Notice that we are computing same values over & over again.

oExample :-
Def
fib_seq(n)
A=0
B=1
If n==1:
Print(a)
Elif n==2:
Print(a,b)
Else:
Print(a,b,end=” “)
For I in range(n-2):
c=a+b
a=b
b=c
print(c , end=” “)
#calling
fib_seq(10)

1 Word Question – Answer

SR.NO QUESTION ANSWER


.
1 Each new term in the Fibonacci sequence is two
generated by adding the previous _
terms.
2 is key idea behind any dynamic Memorization
programming.

85
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

Q-4 Explain 0/1 knapsack algorithm.

Detail :-
 In 0-1 knapsack , items can not be broken.
 It means if 1 than cover all the elements and if 0 than leave all the
elements.
 This is the main reason behind calling it as 0-1 knapsack.
 In case of 0-1 knapsack , the value of xi can be either 0 or 1 where other
elements remains same.
 Let’s consider the capacity of the knapsack is w=25 and the items as shown
in the following table.

86
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

ITEM A B C D
PROFIT 24 18 18 10
WEIGHT 24 10 10 7
 Here , profit per unit weight = (pi/wi).
 First item A will be selected as it will contribute maximum profit among
all the elements.
 After selecting item A , no more item will be selected.
 Here , for this given set of items total profit is 24.
 The optimal solution can be achieved by selecting items , B and C , where
total profit is 18+18=36.
 In this example , the items are selected based on ratio (pi/wi).
 Let’s us consider capacity of knapsack is w=60 and the items are as shown
in following table.

ITEM A B C
PRICE 100 280 120
WEIG 10 40 20
HT
RATIO 10 7 8

 First item A is selected , then next item B is select.


 Here , total profit is 100 + 280 =380.
 The optimal solution of this instance can be achieved by selecting items B
and C , where total profit is 280 + 120 = 400.
 0/1 knapsack alogorithm takes following inputs :
o The maximum weight = W.
o The number of items = n.
o The two sequences ….
 Value V = <v1,v2,……,vn>
 Weight W = <w1,w2,…..,wn>

87
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

1 Word Question – Answer

SR.NO QUESTION ANSWER


.
1 In , items can not be broken. 0-1 knapsack
2 In 0-1 knapsake algorithm, means 1
cover all the elements and means 0
leave all the elements.

Q-5 Explain dynamic programming with divide & conquer algorithm.


88
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001

DIVIDE & CONQUER ALGORITHM


Detail :-
 Devide & conquer is the process of breaking down problem into smaller
parts.
 Break a problem into subprograms that are similar with original problems.
 Recursively , solves the sub problems , and finally combines the solutions to
the sub programs to solve the original problem.
 Divide & conquer is an alogorithm paradigm.
 A typical divide & conquer algorithm solve a problem using following
3steps:
o DIVIDE (Break) :-
 It breaks the given problem into sub – problems of same type.
 Ths step involves breaking the problem into smaller sub –
problems.
 At this stage , sub- problem should represent a part of original
problem.

o CONQUER(Solve) :-
 It include process of recursively solve the sub – problems.
 This step receives a lot of smaller sub – problem to be solved.
 Generally , at this level , the problems are considered as
“solved”.

o COMBINE (Merge):-
 It combines the appropriate answers as well as results.
 When smaller sub – problems are solved , this stage recursively
combines them until they found solution of original problem.

89
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
o Example :-
def bsearch(list ,val):
list_size = len(list) -1
idx0 = 0
idxn = list_size #find middle most value
While idx0 < = idxn :
Midval = (idx0 _ idxn)/2
If list[midval] == val:
Return midval
#compare value – middle most value
If val > list[midval]:
Idx0 = midval +1
Else:
Idxn = midval – 1
If idx0 > idxn:
Return none
#calling
l1 = [55,44,2,4,9,8]
Print (bsearch(l1,4)
 A classic example of divide & conquer is merge sort which demonstrated
below.
 In merge sort , we divide array into two halves & sort the two halves
recursively & then finally merge the sored halves.

90

You might also like