Found 10805 Articles for Python

Python Program for Find the closest pair from two sorted arrays

Pavitra
Updated on 20-Dec-2019 06:10:25

133 Views

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 Live Demo# 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 = ... Read More

Python Program for Extended Euclidean algorithms

Pavitra
Updated on 20-Dec-2019 06:07:43

947 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Given two numbers we need to calculate gcd of those two numbers and display them.GCD Greatest Common Divisor of two numbers is the largest number that can divide both of them. Here we follow the euclidean approach to compute the gcd i.e. to repeatedly divide the numbers and stop when the remainder becomes zero. Here we extend the algorithm based on previous values obtained in recursion.Now let’s observe the solution in the implementation below −Example Live Demo# extended Euclidean Algorithm def gcdExtended(a, b, x, y): ... Read More

Python Program for Egg Dropping Puzzle

Pavitra
Updated on 20-Dec-2019 06:03:11

201 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Suppose that we want to know which stories in a 40-story building are safe to drop the eggs from, and which of those will cause the eggs to get damaged on landing with the help of eggs. We need to display a minimum number of trails to check the stories.Now let’s observe the solution in the implementation below −Example Live Demo# dynamic programming INT_MAX = 32767 # to get minimum trials def eggDrop(n, k):    # intialization    eggFloor = [[0 for x in range(k ... Read More

Python Program for Detect Cycle in a Directed Graph

Pavitra
Updated on 20-Dec-2019 06:00:01

835 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a directed graph, we need to check whether the graph contains a cycle or not. The output should be true if the given graph contains at least one cycle, otherwise false.Now let’s observe the solution in the implementation below −Example Live Demo# collections module from collections import defaultdict # class for creation of graphs class Graph():    # constructor    def __init__(self, vertices):       self.graph = defaultdict(list)       self.V = vertices    def addEdge(self, u, v):   ... Read More

Python Program for Cycle Sort

Pavitra
Updated on 20-Dec-2019 05:56:13

199 Views

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 cycle sort.It is an in-place algorithm and swapping takes place by the formation of cycles.Now let’s observe the solution in the implementation below −Example Live Demodef cycleSort(array):    writes = 0    # cycles to be rotated    for cycleStart in range(0, len(array) - 1):       item = array[cycleStart]       #position to place the item       pos = cycleStart       for i in ... Read More

Python Program for Cutting a Rod

Pavitra
Updated on 20-Dec-2019 05:49:45

675 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a rod of length n and an array of prices that contains prices of all pieces of the size which are smaller than n. We need to determine the maximum value obtainable by cutting up the rod and selling its pieces.We will be using a dynamic programming approach to solve the problem.Now let’s observe the solution in the implementation below−Example Live Demo# A Dynamic Programming solution for Rod cutting problem INT_MIN = -32767 # cut function def cutRod(price, n):    val = ... Read More

Python Program for Counting Sort

Pavitra
Updated on 20-Dec-2019 05:45:41

116 Views

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 the array using the concept of counting sort.Counting sort is a technique in which we work on keys between a specific range. It involves counting the number of objects which have distinct key & values. Finally, we do arithmetic calculations to obtain the position of each object and display the output.Now let’s observe the solution in the implementation below −Example Live Demodef countSort(arr):    # The output character array that will have sorted arr    output ... Read More

Python Program for Cocktail Sort

Pavitra
Updated on 20-Dec-2019 05:38:18

283 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to perform a bitonic sort on the given list and display the listCocktail Sort − Here sort takes place like bubble sort where iteration takes place in both directions.AlgorithmFirstly array is traversed from left to right. During traversal, adjacent items are compared and based on the conditions, the values are swapped. By this, the largest number will be at the end of the array.Now the array is traversed in the opposite direction and based on the conditions, elements ... Read More

Python Program for BogoSort or Permutation Sort

Pavitra
Updated on 20-Dec-2019 05:33:17

156 Views

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 permutation sort.BogoSort also is known as permutation sort, is based on generating and testing paradigms.Now let’s observe the solution in the implementation below−Example Live Demo# random module import random # Sort def bogoSort(a):    n = len(a)    while (is_sorted(a)== False):       shuffle(a) # check def is_sorted(a):    n = len(a)    for i in range(0, n-1):       if (a[i] > a[i+1] ):       ... Read More

Python Program for Binary Insertion Sort

Pavitra
Updated on 20-Dec-2019 05:28:47

1K+ Views

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 binary insertion sort.Here as the name suggests, we use the concept of the binary search along with the insertion sort algorithm.Now let’s observe the solution in the implementation below −Example Live Demo# sort def insertion_sort(arr):    for i in range(1, len(arr)):       temp = arr[i]       pos = binary_search(arr, temp, 0, i) + 1       for k in range(i, pos, -1):       ... Read More

Advertisements