Found 10783 Articles for Python

Find a positive number M such that gcd(N^M,N&M) is maximum in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:32:45

70 Views

Suppose we have a number N, we have to find a positive number M such that gcd(N^M, N&M) is as large as possible and m < n. We will also return the largest gcd thus obtained.So, if the input is like 20, then the output will be 31To solve this, we will follow these steps −if bit_count(n) is same as 0, thenfor i in range 2 to int(square root of (n)) + 1, doif n mod i is same as 0, thenreturn int(n / i)otherwise, val := 0p :=dupn := nwhile n is non-zero, doif (n AND 1) is same ... Read More

Final state of the string after modification in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:05:57

88 Views

Suppose we have a string S. The length is n. These n boxes adjacent to each other, a character R at position i represents that i-th box is being pushed towards right. similarly, L at position i represents that i-th box is being pushed towards left, a dot '.' indicates an empty space. Starting from initial configuration, at every time unit, a box being pushed to the right side is able to push next box to right, same action can be applied for the left side also. We have to find the final positions of all boxes when no more ... Read More

Python - Filtering data with Pandas .query() method

Pradeep Elance
Updated on 22-Jul-2020 08:41:05

2K+ Views

Pandas is a very widely used python library for data cleansing, data analysis etc. In this article we will see how we can use the query method to fetch specific data from a given data set. We can have both single and multiple conditions inside a query.Reading the dataLet’s first read the data into a pandas data frame using the pandas library. The below program just does that.Exampleimport pandas as pd # Reading data frame from csv file data = pd.read_csv("D:\heart.csv") print(data)OutputRunning the above code gives us the following result −Query with single conditionNext we see how we ... Read More

Python - Filter out integers from float numpy array

Pradeep Elance
Updated on 22-Jul-2020 08:36:16

383 Views

As part of data cleansing activities, we may sometimes need to take out the integers present in a list. In this article we will have an array containing both floats and integers. We will remove the integers from the array and print out the floats.With astypeThe astype function will be used to find if an element from the array is an integer or not. Accordingly we will decide to keep or remove the element from the array and store it in the result set.Example Live Demoimport numpy as np # initialising array A_array = np.array([3.2, 5.5, 2.0, 4.1, 5]) ... Read More

Python - Filter dictionary key based on the values in selective list

Pradeep Elance
Updated on 22-Jul-2020 08:34:33

1K+ Views

Sometimes in a Python dictionary we may need to to filter out certain keys of the dictionary based on certain criteria. In this article we will see how to filter out keys from Python dictionary.With for and inIn this approach we put the values of the keys to be filtered in a list. Then iterate through each element of the list and check for its presence in the given dictionary. We create a resulting dictionary containing these values which are found in the dictionary.Example Live DemodictA= {'Mon':'Phy', 'Tue':'chem', 'Wed':'Math', 'Thu':'Bio'} key_list = ['Tue', 'Thu'] print("Given Dictionary:", dictA) print("Keys for filter:", ... Read More

Python - fabs() vs abs()

Pradeep Elance
Updated on 22-Jul-2020 08:32:37

1K+ Views

Both abs() and fabs() represent the mathematical functions which give us the absolute value of numbers. But there is a subtle difference between both of them which we can explore in the exmaples below.ExampleThe abs() functions returns the absolute value as an integer or floating point value depending on what value was supplied dot it. But the fabs) function will always return the value as floating point irrespective of whether an integer or a floating point was supplied to it as a parameter. Live Demoimport math n = -23 print(abs(n)) print(math.fabs(n)) n = 21.4 print(abs(n)) print(math.fabs(n)) n = ... Read More

Python - end parameter in print()

Pradeep Elance
Updated on 22-Jul-2020 08:30:56

777 Views

The print() function in python always creates a newline. But there is also a parameter for this function which can put other characters instead of new line at the end. In this article we will explore various options for this parameter.ExampleIn the below example we see various ways we can assign values to the end parameter and see the result from it. Live Demoprint("Welcome to ") print("Tutorialspoint") print("Welcome to ", end = ' ') print("Tutorialspoint") print("emailid", end='@') print("tutorialspoint.com")OutputRunning the above code gives us the following result −Welcome to Tutorialspoint Welcome to Tutorialspoint emailid@tutorialspoint.comRead More

Python - Difference in keys of two dictionaries

Pradeep Elance
Updated on 22-Jul-2020 08:28:31

2K+ Views

Two python dictionaries may contain some common keys between them. In this article we will find how to get the difference in the keys present in two given dictionaries.With setHere we take two dictionaries and apply set function to them. Then we subtract the two sets to get the difference. We do it both ways, by subtracting second dictionary from first and next subtracting first dictionary form second. Those keys which are not common are listed in the result set.Example Live DemodictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'} print("1st Distionary:", dictA) dictB = {'3': 'Wed', '4': 'Thu', '5':'Fri'} print("1st Distionary:", ... Read More

Python - Create a dictionary using list with none values

Pradeep Elance
Updated on 22-Jul-2020 08:23:09

523 Views

Suppose you are given a list but we want to convert it to dictionary. Dictionary elements hold two values are called key value pair, we will use in case of value. The elements of the list become keys and non will remain a placeholder.With dictThe dict() constructor creates a dictionary in Python. So we will use it to create a dictionary. The fromkeys method is used to create the dictionary elements.Example Live DemolistA = ["Mon", "Tue", "Wed", "Thu", "Fri"] print("Given list: ", listA) res = dict.fromkeys(listA) # New List print("The list of lists:", res)OutputRunning the above code gives us ... Read More

Python - Convert given list into nested list

Pradeep Elance
Updated on 22-Jul-2020 08:19:44

958 Views

There may be a situation when we need to convert the elements in the list into a list in itself. In other words, create a list which is nested as its elements are also lists.Using iterationThis is the novel approach in which we take each element of the list and convert it to a format of lists. We use temporary list to achieve this. Finally all these elements which are converted to lists are group together to create the required list of lists.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] print("Given list:", listA) new_list = [] # Creating ... Read More

Advertisements