Python Articles

Page 831 of 855

Python Program for Egg Dropping Puzzle

Pavitra
Pavitra
Updated on 11-Mar-2026 384 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# 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 Extended Euclidean algorithms

Pavitra
Pavitra
Updated on 11-Mar-2026 2K+ 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# extended Euclidean Algorithm def gcdExtended(a, b, x, y):   ...

Read More

Python Program for Find the closest pair from two sorted arrays

Pavitra
Pavitra
Updated on 11-Mar-2026 295 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# 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 More

Python Program for Gnome Sort

Pavitra
Pavitra
Updated on 11-Mar-2026 397 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 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 More

Single Number in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have an array A. In this array there are many numbers that occur twice. Only one element can be found a single time. We have to find that element from that array. Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there is each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res = res XOR ereturn resExampleLet us see ...

Read More

Python Program for Heap Sort

Pavitra
Pavitra
Updated on 11-Mar-2026 2K+ 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 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 More

Python Program for Maximum height when coins are arranged in a triangle

Pavitra
Pavitra
Updated on 11-Mar-2026 271 Views

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 More

Python Program for Min Cost Path

Pavitra
Pavitra
Updated on 11-Mar-2026 852 Views

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 More

Python Program for Number of stopping station problem

Pavitra
Pavitra
Updated on 11-Mar-2026 369 Views

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 More

Reverse String in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have an array of characters. We have to reverse the string without using any additional space. So if the string is like [‘H’, ‘E’, ‘L’, ‘L’, ‘O’], the output will be [‘O’, ‘L’, ‘L’, ‘E’, ‘H’]To solve this, we will follow these steps −Take two pointers to start = 0 and end = length of the string – 1swap first and last charactersincrease start by 1 and decrease-end by 1ExampleLet us see the following implementation to get a better understanding −class Solution(object):    def reverseString(self, s):       """       :type s: List[str]     ...

Read More
Showing 8301–8310 of 8,546 articles
« Prev 1 829 830 831 832 833 855 Next »
Advertisements