Python Articles

Page 115 of 852

Find n-th lexicographically permutation of a strings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 337 Views

Suppose we have a string whose length is m, and this string is containing only lowercase letters, we have to find the n-th permutation of string lexicographically.So, if the input is like string = "pqr", n = 3, then the output will be "qpr" as all permutations are [pqr, prq, qpr, qrp, rpq, rqp], they are in sorted order.To solve this, we will follow these steps −MAX_CHAR := 26MAX_FACT := 20factorials := an array of size MAX_FACTfactorials[0] := 1for i in range 1 to MAX_FACT, dofactorials[i] := factorials[i - 1] * isize := size of stringoccurrence := an array of ...

Read More

Find nth term of a given recurrence relation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 729 Views

Suppose we have a sequence of numbers called bn, this is represented using a recurrence relation like b1=1 and bn+1/bn=2n . We have to find the value of log2(bn) for a given n.So, if the input is like 6, then the output will be 5 as log2(bn) = (n * (n - 1)) / 2 = (6*(6-1))/2 = 15We can solve this problem by solving this relation as follows −bn+1/bn = 2nbn/bn-1 = 2n-1…b2/b1 = 21 , If we multiply all of above, we can get(bn+1/bn).(bn/bn-1)……(b2/b1) = 2n + (n-1)+……….+1So, bn+1/b1 = 2n(n+1)/2As 1 + 2 + 3 + ………. ...

Read More

Find original numbers from gcd() every pair in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 287 Views

Suppose we have an array A where GCD of every possible pair of elements of another array is given, we have to find the original numbers which are used to compute the given GCD array.So, if the input is like A = [6, 1, 1, 13], then the output will be [13, 6] as gcd(13, 13) is 13, gcd(13, 6) is 1, gcd(6, 13) is 1, gcd(6, 6) is 6To solve this, we will follow these steps −n := size of Asort array A in descending orderoccurrence := an array of size A[0] and fill with 0for i in range ...

Read More

Find pairs with given product in a sorted Doubly Linked List in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 197 Views

Suppose we have a sorted doubly linked list of unique positive numbers; we have to find pairs in the doubly linked list whose product is same as a given value x. We have to keep in mind that, this will be solved without consuming any extra space.So, if the input is like L = 1 ⇔ 2 ⇔ 4 ⇔ 5 ⇔ 6 ⇔ 8 ⇔ 9 and x = 8, then the output will be (1, 8), (2, 4)To solve this, we will follow these steps −curr := head, nxt := headwhile nxt.next is not None is non-zero, donxt ...

Read More

Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 184 Views

Suppose we have a number N, we have to find all factors of N and return the product of four factors of N such that: The sum of the four factors is same as N. The product of the four factors is maximum. All four factors can be equal to each other to maximize the product.So, if the input is like N = 60, then the output will be All the factors are -> 1 2 3 4 5 6 10 12 15 20 30 60 and product is 50625, as we 15 has been selected four times to make ...

Read More

Creating a Pandas dataframe column based on a given condition in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 345 Views

Pandas creates data frames to process the data in a python program. In this article we will see how we can add a new column to an existing dataframe based on certain conditions.The Given Data FrameBelow is the given pandas DataFrame to which we will add the additional columns. It describes the Days and Subjects of an examination.Exampleimport pandas as pd # Lists for Exam subjects and Days Days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Sub = ['Chemisry', 'Physics', 'Maths', 'English', 'Biology'] # Dictionary for Exam Schedule Exam_Subjects = {'Exam Day': Days,             ...

Read More

Create a Pandas Dataframe from a dict of equal length lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 351 Views

The Dataframe in pandas can be created using various options. One of the option is to take a dictionary and convert it to a Dataframe. In this article we will see how to take three lists of equal length and convert them to a pandas dataframe using a python dictionary.Uisng Lists and DictionaryIn this approach we have the lists declared individually. Then each of them is used as a value for the appropriate key inside a dictionary definition. Finally the pandas method called pd.Dataframe is applied to the dictionary.Exampleimport pandas as pd # Lists for Exam schedule Days = ...

Read More

Creating DataFrame from dict of narray-lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 668 Views

Pandas is a very widely used python library for data processing and data analysis. In this article we will see how we we can create pandas dataframe from given python dictionaries and lists.From dictionary with listsDictionaries are key value pairs. If we take a python dictionary which has key and a list as a value then we can directly use the DataFrame method on the given dictionary to create the pandas data frame.Exampleimport pandas as pd # Dictionary for Exam Schedule Exam_Schedule = { 'Exam Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'Exam Subject': ['Chemisry', 'Physics', 'Maths', 'English', 'Biology'], 'Exam ...

Read More

Custom length Matrix in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 225 Views

Sometimes when creating a matrix using python we may need to control how many times a given element is repeated in the resulting matrix. In this articled we will see how to create a matrix with required number of elements when the elements are given as a list.Using zipWe declare a list with elements to be used in the matrix. Then we declare another list which will hold the number of occurrences of the element in the matrix. Using the zip function we can create the resulting matrix which will involve a for loop to organize the elements.ExamplelistA = ['m', ...

Read More

Custom Multiplication in list of lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

Multiplying two lists in python can be a necessity in many data analysis calculations. In this article we will see how to multiply the elements of a list of lists also called a nested list with another list.Using LoopsIn this approach we design tow for loops, one inside another. The outer loop keeps track of number of elements in the list and the inner loop keeps track of each element inside the nested list. We use the * operator to multiply the elements of the second list with respective elements of the nested list.ExamplelistA = [[2, 11, 5], [3, 2, ...

Read More
Showing 1141–1150 of 8,519 articles
« Prev 1 113 114 115 116 117 852 Next »
Advertisements