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
Python Articles
Page 815 of 852
Dictionary Methods in Python (cmp(), len(), items()…)
Dictionary in python is one of the most frequently used collection data type. It is represented by hey value pairs. Keys are indexed but values may not be. There are many python-built in functions that make using the dictionary very easy in various python programs. In this topic we will see the three in-built methods namely cmp(), len() and items().cmp()The method cmp() compares two dictionaries based on key and values. It is helpful in identifying duplicate dictionaries as well as doing a relational comparison among the dictionaries. It is a feature on only python2 and not available in python 3.Syntaxcmp(dict1, ...
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 −Example Live Demodef stoogesort(arr, l, h): if l >= h: return # swap if arr[l]>arr[h]: ...
Read MorePython Program for Sieve of Eratosthenes
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number n, we need to print all primes smaller than or equal to n. Constraint: n is a small number.Now let’s observe the solution in the implementation below −Exampledef SieveOfEratosthenes(n): # array of type boolean with True values in it prime = [True for i in range(n + 1)] p = 2 while (p * p
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 Live Demo# 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, ...
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 Live Demo# 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 Odd-Even Sort / Brick 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 brick sort.Here we have two phases: Odd and Even Phase. In the odd phase, bubble sort is performed on odd indexed elements and in the even phase, bubble sort is performed on even indexed elements.Now let’s observe the solution in the implementation below−Exampledef oddEvenSort(arr, n): # flag isSorted = 0 while isSorted == 0: isSorted = 1 temp = 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 Live Demo# stop station def stopping_station( p, n): num = 1 dem = 1 s = p # selecting specified position while p != 1: dem ...
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 Live Demo# 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 Merge 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 merge sortHere 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#merge function def merge(arr, l, m, r): n1 = m - l + 1 n2 = r- m # create arrays L = [0] * (n1) R = [0] * (n2) # Copy data to arrays ...
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 Live Demo# squareroot def squareRoot(n): # initial approximation x = n y = 1 e = 0.000001 # allowed error ...
Read More