Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings

Illustrative Python Programs

Control Flow, Functions, Strings

Following is a Python program that is used for obtaining the square root of a given number

Illustrative Programs

AU : Dec.-19, Marks 8

 

1. quare Root

Following is a Python program that is used for obtaining the square root of a given  number

Python Program

print("Enter the number: ")

num = float(input())

sqrt_num = num ** 0.5

print("The square root of ",num," is ",sqrt_num)

Output


 

2. GCD

The GCD is a largest integer that can exactly divide both numbers without a remainder.

The easiest and fastest process consists in decomposing each one of the numbers in products of prime factors, this is, and we successively divide each one of the numbers by prime numbers till we reach a quotient that equals 1.

For example -

96 = 2 × 2 × 2 × 2 × 2 × 3

36 = 2 × 2  3 × 3

GCD = 2 × 2 × 3

= 12

Hence GCD of 96 and 36 is 12.

Iterative Python Program

print("Enter first number: ")

a = int(input())

print("Enter second number: ")

b = int(input())

rem = a%b

while rem! = 0:

a = b

b = rem

rem = a%b

print("gcd of given numbers is: ",b)

Output


Recursive Python Program

def gcd(a,b):

c = b

b = a%b

if b = = 0:

return c

else:

return gcd(cb)

print(gcd(96,36))

Output

12

>>> 

Cano

 

3. Exponentiation

Python Program

def expo(base,degree):

result = 1

i = 1

while I < = degree:

result = base * result

i + = 1

print("Result is ",result)

expo(2,3)

Output

Result is 8

>>> 

 

4. Sum of Numbers

For sum of numbers we have to store the numbers in an array. And by traversing the elements of array, each number is added with each other. The resultant sum is then printed.

For example

Consider 5 numbers stored in an array as follows -


Sum = (10 + 20 + 30 + 40 + 50) = 150

Python Program

print("Enter total number of elements in array")

n = int(input())

i = 0

sum = 0

a = [i for i in range(n)] #Creation of array

for i in range(0,n):

print("Enter the element: ")

a[i] = int(input())

print("The elements in array are ...")

for i in range(0,n):

print(a[i])

for i in range(0,n):

sum = sum + a[i]

print("The sum of all elements in array",sum)

Output


 

5. Linear Search

In linear search method, the key element is compared against every element of the array. If the key element matches with the array element then we declare element is found otherwise the element is declared as not found.

Python Program

print("Enter total number of elements in array")

n = int(input())

i = 0

a = [i for i in range(n)] #Creation of array

 

for i in range(0,n):

print("Enter the element: ")

a[i] = int(input())

print("The elements in array are ...").

for i in range(0,n):

print(a[i])

print("Enter the key element to be searched ")

key = int(input())

for i in range(0,n):

if a[i] = = key:

found = True

break

else:

found = False

if found = = True:

print("The element is found")

else:

print("The element is not found")

Output


 

6.  Binary Search

The binary search is an efficient searching technique.

Python Program

def binary_search(a,n,key):

low = 0

high = n

 

while(low < = high):

mid = int((low+high)/2)

if(key == a[mid]):

return mid

elif(key< a[mid]):

high = mid - 1

else:

low = mid + 1

return -1

n = int(input("Enter the size of the list: "))

a = [i for i in range(n+1)] #Creation of array

 

for i in rnge(0,n):

print("Enter the element: ")

a[i] = int(input())

print(“Enter the element to be searched: ")

k = int(input())

position = binary_search(a,n,k)

if(position ! = -1):

print("Entered number {} is present at position: {}".format(k,position))

Cered number {} is not present in the list".format(k))

Output(Run1)

Enter the size of the list: 5

Enter the element:

10

Enter the element:

20

Enter the element:

30

Enter the element:

40

Enter the element:

50

Enter the element to be searched:

40

Entered number 40 is present at position: 3

>>> 

Output(Run2)

Enter the size of the list: 5

Enter the element:

10

Enter the element:

20

Enter the element:

30

Enter the element:

40

Enter the element:

50

Enter the element to be searched:

90

Enter the number 90 is not present in the list

>>> 

Explanation on Binary search method

The prerequisite for this searching technique is that the arry should be sorted.

Example :

Aa mentioned earlier the necessity of this method is that all the elements should be sorted. So let us take an arry of sorted elements.


Step 1: Now the key element which is to be searched is = 99 key = 99.

Step 2: Find the middle element of the array. Compare it with the key

if middle < key

i.e. if 42 < 99


if 42 < 99 search the sublist 2.


Here middle element is 99 and key is also 99. Hence we declare that the element is found and it is at index 6.


Review Question

1. Write a python code to perform binary search. Trace it with example of your choice.

AU : Dec.-19, Marks 8

 

Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Control Flow, Functions, Strings - Illustrative Python Programs