Found 10476 Articles for Python

Program to check final answer by performing given stack operations in Python

Arnab Chakraborty
Updated on 14-Oct-2021 12:31:53

816 Views

Suppose we have a list of string called ops where each element is any one of these operations like below −A non-negative integer value that will be pushed into a stack"POP" to delete top most element from the stack"DUP" to insert top element again into the stack, to make it duplicate"+" to pop out the top two elements and push the sum value"-" to pop out the top two elements and push the result of (top element - element just below top)So we have to find the top mot element in the stack after applying all of these operations. If ... Read More

Program to count how many swimmers will win the final match in Python

Arnab Chakraborty
Updated on 14-Oct-2021 12:01:04

372 Views

Suppose we have a list of numbers called nums whose length is n. The elements present in this list are representing the current score of swimmers in a competition. For the final match the first place winner for this current round will get n scores, the second place winner will get n-1 points and so on. We have to check the number of swimmers that can still win the competition in the final round after the current round. If there is a tie for the first in points, that will also be counted as winning.So, if the input is like ... Read More

Program to check heap is forming max heap or not in Python

Arnab Chakraborty
Updated on 14-Oct-2021 11:54:51

1K+ Views

Suppose we have a list representing a heap tree. As we know heap is a complete binary tree. We have to check whether the elements are forming max heap or not. As we know for max heap every element is larger than both of its children.So, if the input is like nums = [8, 6, 4, 2, 0, 3], then the output will be True because, all elements are larger than their children.To solve this, we will follow these steps −n := size of numsfor i in range 0 to n - 1, dom := i * 2num := nums[i]if ... Read More

Program to check all listed delivery operations are valid or not in Python

Arnab Chakraborty
Updated on 14-Oct-2021 11:45:32

178 Views

Suppose we have a list of strings called orders. Each element in the orders list starts with either "P" or "D". The "P" indicates that is picked up, and "D" means "delivery". And these letters are followed by the order id number. For example, "P6" indicates pick up order 6. We have to check whether orders list is valid or not based on these rules −We cannot delivery an order before pickupEvery pickup must be deliveredAn order which is already been picked up and also delivered cannot be picked up or delivered againSo, if the input is like orders = ... Read More

Program to find buildings from where sea can be visible in Python

Arnab Chakraborty
Updated on 14-Oct-2021 11:42:09

399 Views

Suppose we have a list of heights of different buildings. A building with heights value heights[i] can see the ocean when every building on its right are shorter than that building. We have to find the building indices from where we can see the ocean, in ascending order.So, if the input is like heights = [8, 12, 12, 9, 10, 6], then the output will be [2, 4, 5] because we can see the ocean from building heights 12 at index 2, from building height 10 at index 10 and from last building at index 5.To solve this, we will ... Read More

Program to find number of unique people from list of contact mail ids in Python

Arnab Chakraborty
Updated on 14-Oct-2021 11:37:19

214 Views

Suppose we have a list of mail ids in a constants list. So for each row there may be more than one mail id of same person. The contact i is considered as duplicate when there's any j, where j < i such that contact j shares a common email with i. So we have to find the number of unique people in contacts.So, if the input is like contacts = [["alex@gmail.com", "alex@yahoo.com"], ["alex_25@yahoo.com", "alex@gmail.com"], ["bob15@gmail.com"] ], then the output will be 2, as the first and second contacts are sharing same mail ids, so they are same person, so ... Read More

Python Pandas - Get the maximum value from Ordered CategoricalIndex

AmitDiwan
Updated on 14-Oct-2021 11:12:51

417 Views

To get the maximum value from Ordered CategoricalIndex, use the catIndex.max() method in Pandas.At first, import the required libraries −import pandas as pdSet the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −catIndex = pd.CategoricalIndex(    ["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"] )Display the Categorical Index −print("Categorical Index...", catIndex)Get the max value −print("Maximum value from CategoricalIndex...", catIndex.max())ExampleFollowing is the code −import pandas as pd # CategoricalIndex can only take on a limited, and usually fixed, number of possible values. # Set the categories ... Read More

Program to check words can be found in matrix character board or not in Python

Arnab Chakraborty
Updated on 14-Oct-2021 11:31:36

562 Views

Suppose we have a matrix character board. Where each cell is holding a character. We also have a string called target, we have to check whether the target can be found in the matrix by going left-to-right, or up-to-down unidirectional way, or not.So, if the input is likeantsspinlapsWord = “tip”then the output will be True, you can see the third column (top to bottom) is forming "tip".To solve this, we will follow these steps −for each i in board, doi := make word from characters present in iif word is present in i, thenreturn Truei := 0while i < row ... Read More

Python Pandas - Get the minimum value from Ordered CategoricalIndex

AmitDiwan
Updated on 14-Oct-2021 11:10:28

145 Views

To get the minimum value from Ordered CategoricalIndex, use the catIndex.min() method in Pandas. At first, import the required libraries −import pandas as pdSet the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −catIndex = pd.CategoricalIndex(    ["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"] )Display the Categorical Index −print("Categorical Index...", catIndex)Get the min value −print("Minimum value from CategoricalIndex...", catIndex.min())ExampleFollowing is the code −import pandas as pd # CategoricalIndex is the Index based on an underlying Categorical # Set the categories for the categorical using ... Read More

Python Pandas - Create an Index based on an underlying Categorical

AmitDiwan
Updated on 14-Oct-2021 11:08:53

528 Views

To create an Index based on an underlying Categorical, use the pandas.CategoricalIndex() method.At first, import the required libraries −import pandas as pdCategoricalIndex is the Index based on an underlying Categorical. CategoricalIndex can only take on a limited, and usually fixed, number of possible values. Set the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −catIndex = pd.CategoricalIndex(    ["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"] )Display the Categorical Index −print("Categorical Index...", catIndex)Get the categories −print("DisplayingCategories from CategoricalIndex...", catIndex.categories)ExampleFollowing is the code −import pandas as pd ... Read More

Advertisements