Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Programming using Python

prutor.ai Amey Karkare


Dept. of CSE
IIT Kanpur

For any queries reach out to rahulgr@iitk.ac.in or rahulgarg@robustresults.com


or whatsapp 9910043510

1
Welcome Python Programming E&ICT IIT Kanpur
Can we search Faster?
• Yes, provided the elements in the sequence
are sorted

prutor.ai
– in either ascending or descending order

1
Programming, Recursion
About me
Amey Karkare
Dept. of CSE

prutor.ai
IIT Kanpur

http://www.cse.iitk.ac.in/~karkare

2
Welcome Programming E&ICT IIT Kanpur
Acknowledgements
➢ The images/contents are used for
teaching purpose and for fun. The

prutor.ai
copyright remains with the original
creator. If you suspect a copyright
violation, bring it to my notice and I
will remove that image/content.

3
Welcome Python Programming E&ICT IIT Kanpur
The Course
➢The course teaches you how to solve
problems using the computer.

prutor.ai
➢No prior exposure to programming is
expected.

4
Welcome Python Programming E&ICT IIT Kanpur
A Computer?

prutor.ai
Almost all electronic gadgets today are Computers.
They are everywhere! 5
Welcome Python Programming E&ICT IIT Kanpur
Why should I do this course?
• Computers are everywhere
• Understand how computers work

• prutor.ai
Write your own programs
Understand how computers can
be used to automate repetitive tasks

6
Welcome Python Programming E&ICT IIT Kanpur
Process of Programming
Problem
Description

prutor.ai
X
Solution Model of the
Code Problem

Logical
Solution
7
Welcome Python Programming E&ICT IIT Kanpur
Process of Programming
Problem
Description

Code
prutor.ai
Solution Model of the
Problem
With experience, the
Logical separation between
Solution these stages blurs
8
Welcome Python Programming E&ICT IIT Kanpur
Process of Programming: Step 1
• Define and model the problem. In real-life this is
important and complicated.
• For example, consider modelling the Indian Railways
reservation system.

prutor.ai
• If model is not proper, end result will be
unsatisfactory!

9
Welcome Python Programming E&ICT IIT Kanpur
Process of Programming: Step 2
➢Obtain a logical solution to your problem.
➢A logical solution is a finite and clear

prutor.ai
step-by-step procedure to solve your
problem.
➢Also called an Algorithm.
➢We can visualise this using a Flowchart.
➢Very important step in the programming
process.
10
Welcome Python Programming E&ICT IIT Kanpur
Algorithms in real-life

• Recipes (say, for a cake.)

prutor.ai

11
Welcome Python Programming E&ICT IIT Kanpur
Algorithms in real-life
• Assembly instructions for a make-
it-yourself kit.

prutor.ai
http://www.gocomics.com/calvinandhobbes/2009/06/02
12
Welcome Python Programming E&ICT IIT Kanpur
Process of Programming: Step 3
➢Convert the Algorithm into working code.
➢This is the stage where you require a

prutor.ai
language to communicate with the
computer
➢Syntax and Semantics of the language
➢If your model or the logical solution is
wrong, the program will also be wrong
➢Garbage In, Garbage Out
13
Welcome Python Programming E&ICT IIT Kanpur
Binary Search for Sorted Arrays
• binsearch(a, start, end, key)
–Search key between a[start]…a[end],

prutor.ai
where a is a sorted (non-decreasing)
sequence

2
Programming, Recursion
Binary Search for Sorted Sequences
if start > end:
return False
mid = (start + end)//2

prutor.ai
if a[mid]==key:
return True
if (a[mid] > key):
return binsearch(a, start, mid-1, key)
else:
return binsearch(a, mid+1, end, key)

3
Programming, Recursion
int search2(a, start, end, key) {
if start > end: return False
mid = (start + end)//2
Wait, isn’t this same as search2?
if a[mid]==key: return True
return search2(a, start, mid-1, key)
|| search2(a, mid+1, end, key)
• Lets look closely }
int binsearch(a, start, end, key) {
if start > end: return False
mid = (start + end)//2

prutor.ai
if a[mid]==key: return True
In worst case, if (a[mid] > key):
Both search2 may fire. return binsearch(a, start, mid-1, key)
But, only ONE of the two else:
binsearch will fire. return binsearch(a, mid+1, end, key)
}

4
Programming, Recursion
It matters for How does it matter?
the time taken!

prutor.ai

5
Programming, Recursion

You might also like