- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10805 Articles for Python
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
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
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
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
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
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
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
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
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
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
To Continue Learning Please Login
Login with Google