Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Pavitra
Page 4 of 13
Python Program for Find the closest pair from two sorted arrays
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two arrays, we need to find the closest pair from the two sorted arraysNow let’s observe the solution in the implementation below −Example# sys module import sys # pair def print_(ar1, ar2, m, n, x): # difference diff=sys.maxsize # index l = 0 r = n-1 while(l < m and r >= 0): # closest pair if abs(ar1[l] + ar2[r] - x) < diff: res_l = l ...
Read MoreMulti-Line printing in Python
We have usually seen the print command in python printing one line of output. But if we have multiple lines to print, then in this approach multiple print commands need to be written. This can be avoided by using another technique involving the three single quotes as seen below.Exampleprint(''' Motivational Quote : Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it. ''') Running the above code gives us the following result:Motivational Quote : Sometimes later becomes never, Do it ...
Read MorePython Program for Gnome Sort
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using gnome sort.Algorithm1. Firstly we traverse the array from left to right. 2. Now, if the current element is larger or equal to the previous element then we traverse one step ahead 3. otherwise, if the current element is smaller than the previous element then swap these two elements and traverse one step back. 4. Repeat steps given above till we reach the end of the arrayNow let’s observe the solution in the ...
Read MorePython Program for Heap Sort
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of heapsort.Here we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below−Example# heapify def heapify(arr, n, i): largest = i # largest value l = 2 * i + 1 # left r = 2 * i + 2 # right # if left child exists if l < n and arr[i] ...
Read MorePython Program for Maximum height when coins are arranged in a triangle
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given N coins where we need to arrange them in form of a triangle, i.e. in the first row will have 1 coin, the second row will have 2 coins and so on, we need to display the maximum height that can be achieved by the help N coins.Now let’s observe the solution in the implementation below −Example# squareroot def squareRoot(n): # initial approximation x = n y = 1 e = 0.000001 # allowed error ...
Read MorePython Program for Min Cost Path
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a cost matrix and a position (m, n), we need to find the cost of minimum cost path to reach (m, n) from (0, 0). Each cell represents a cost to traverse from one cell to another.Now let’s observe the solution in the implementation below −Example# dynamic approach R = 3 C = 3 def minCost(cost, m, n): # initialization tc = [[0 for x in range(C)] for x in range(R)] # base case tc[0][0] = ...
Read MorePython Program for Number of stopping station problem
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given that there are 13 intermediate stations between two places A and B. We need to find the number of ways in which a train can be stopped at 2 intermediate stations, such that there are no consecutive stations?Now let’s observe the solution in the implementation below −Example# stop station def stopping_station( p, n): num = 1 dem = 1 s = p # selecting specified position while p != 1: dem *= ...
Read MorePython Program for QuickSort
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of quicksortHere we first partition the array and sort the separate partition to get the sorted array.Now let’s observe the solution in the implementation below −Example# divide function def partition(arr,low,high): i = ( low-1 ) pivot = arr[high] # pivot element for j in range(low , high): # If current element is smaller if arr[j]
Read MorePython Program for Recursive Insertion Sort
In this article, we will learn about the solution to the problem statement given below.Problem statement− We are given an array, we need to sort it using the concept of recursive insertion sort.Insertion sort works on creating a parallel array in which we manually insert the elements in the specified order.Now let’s observe the solution in the implementation below −Example# recursive way def insertionSortRecursive(arr, n): # base case if n=0 and arr[j]>last): arr[j+1] = arr[j] j = j-1 arr[j+1]=last # main arr = [1, 5, 3, 4, 8, 6, 3, 4, ...
Read MorePython Program for Stooge Sort
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using stooge sort.Algorithm1. Check if value at index 0 is greater than value at last index, then swap them. 2. sort the initial 2/3rd of the array. 3. sort the last 2/3rd of the array. 4. sort the initial 2/3rd again to confirm.Now let’s observe the solution in the implementation below −Exampledef stoogesort(arr, l, h): if l >= h: return # swap if arr[l]>arr[h]: ...
Read More