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 3 of 13
Python Program to find whether a no is power of two
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number, we need to check that the number is a power of two or not.We can solve this using two approaches as discussed below.Approach 1: Taking the log of the given number on base 2 to get the powerExample# power of 2 def find(n): if (n == 0): return False while (n != 1): if (n % 2 != 0): return False n = n ...
Read MorePython program to get all subsets of a given size of a set
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set, we need to list all the subsets of size nWe have three approaches to solve the problem −Using itertools.combinations() methodExample# itertools module import itertools def findsubsets(s, n): return list(itertools.combinations(s, n)) #main s = {1, 2, 3, 4, 5} n = 4 print(findsubsets(s, n))Output[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]Using map() and combination() methodExample# itertools module from itertools import combinations def findsubsets(s, n): return list(map(set, ...
Read MorePython program to insert an element into sorted list
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 insert an element in a list without changing sorted orderThere are two approaches as discussed below−Approach 1: The brute-force methodExampledef insert(list_, n): # search for i in range(len(list_)): if list_[i] > n: index = i break # Insertion list_ = list_[:i] + [n] + list_[i:] return list_ # Driver function list_ = ['t', 'u', 't', 'o', 'r'] n = 'e' ...
Read MorePython program to interchange first and last elements in a list
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 swap the last element with the first element.There are 4 approaches to solve the problem as discussed below−Approach 1 − The brute-force approachExampledef swapLast(List): size = len(List) # Swap operation temp = List[0] List[0] = List[size - 1] List[size - 1] = temp return List # Driver code List = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'] print(swapLast(List))Output['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']Approach 2 − The brute-force ...
Read MorePython program to print all Prime numbers in an Interval
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an interval we need to compute all the prime numbers in a given rangeHere we will be discussing a brute-force approach to get the solution i.e. the basic definition of a prime number. Prime numbers are the number which has 1 and itself as a factor and rests all the numbers are not its factors.Each time the condition of a prime number is evaluated to be true computation is performed.Now let’s observe the concept in the implementation below−Examplestart = 1 end ...
Read MorePython Program for array rotation
In this article, we will learn about the solution to the problem statement given below.Problem statement − Given a text and a pattern, we need to print all occurrences of pattern and its permutations (or anagrams) in text.Now let’s observe the solution in the implementation below −Example# maximum value MAX = 300 # compare def compare(arr1, arr2): for i in range(MAX): if arr1[i] != arr2[i]: return False return True # search def search(pat, txt): M = len(pat) N = len(txt) # countP pattern account # countTW text ...
Read MorePython Program for Counting 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 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 −Exampledef countSort(arr): # The output character array that will have sorted arr output = ...
Read MorePython Program for Cycle 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 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 −Exampledef 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 range(cycleStart ...
Read MorePython Program for Egg Dropping Puzzle
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 MorePython Program for Extended Euclidean algorithms
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