Found 10476 Articles for Python

Check if a queue can be sorted into another queue using a stack in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:02:15

332 Views

Suppose we have a Queue with first n natural numbers (unsorted). We have to check whether the given Queue elements can be sorted in non-decreasing sequence in another Queue by using a stack. We can use following operations to solve this problem −Push or pop elements from stackDelete element from given Queue.Insert element in the other Queue.So, if the input is like Que = [6, 1, 2, 3, 4, 5], then the output will be True as we can pop 6 from Que, then push it into stack. Now pop all remaining elements from Que to another queue, then pop ... Read More

Check if a Queen can attack a given cell on chessboard in Python

Arnab Chakraborty
Updated on 29-Dec-2020 13:00:31

724 Views

Suppose we have two coordinates on a chessboard for queen and opponent. These points are Q and O respectively. We have to check whether the queen can attack the opponent or not. As we know that the queen can attack in the same row, same column and diagonally.So, if the input is like Q = (1, 1) O = (4, 4), then the output will be True as Q can go (4, 4) diagonally.To solve this, we will follow these steps −if x of Q is same as x of O, thenreturn Trueif y of Q is same as y ... Read More

Check if a prime number can be expressed as sum of two Prime Numbers in Python

Arnab Chakraborty
Updated on 29-Dec-2020 12:58:49

917 Views

Suppose we have a prime number n. we have to check whether we can express n as x + y where x and y are also two prime numbers.So, if the input is like n = 19, then the output will be True as we can express it like 19 = 17 + 2To solve this, we will follow these steps −Define a function isPrime() . This will take numberif number

Check if a point lies on or inside a rectangle in Python

Arnab Chakraborty
Updated on 29-Dec-2020 12:57:09

6K+ Views

Suppose we have a rectangle represented by two points bottom-left and top-right corner points. We have to check whether a given point (x, y) is present inside this rectangle or not.So, if the input is like bottom_left = (1, 1), top_right = (8, 5), point = (5, 4), then the output will be TrueTo solve this, we will follow these steps −Define a function solve() . This will take bl, tr, pif x of p > x of bl and x of p < x of tr and y of p > y of bl and y of p < ... Read More

Python - Add Label to kivy window

Pradeep Elance
Updated on 28-Dec-2020 11:25:51

685 Views

Kivy is an pen source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is used to develop the Android application, as well as Desktops applications. In this article we will see how to add labels to the windows created through Kivy.Creating LabelIn the below example we create a window and give it a custom label by using Label function available in uix.lable module. Once we run the app with this code we get the new window showing the custom label.Examplefrom kivy.app import App from kivy.uix.label import Label class ... Read More

Python - Combine two lists by maintaining duplicates in first list

Pradeep Elance
Updated on 28-Dec-2020 11:22:16

411 Views

In data analysis using python we may come across situation when two lists need to be merged. But it may be a challenge to handle the duplicate elements present in those lists. In this article we will see how to combine two lists by maintain all the elements form the first list and only the unique elements from the second list.Using extendIn this approach we take the first list and create a result list. Then we design a for loop to check for the presence of element of the first list in the second list and if the element is ... Read More

Python - Convert 1D list to 2D list of variable length

Pradeep Elance
Updated on 28-Dec-2020 11:20:56

3K+ Views

A list in python is normally a 1D list where the elements are listed one after another. But in a 2D list we have list nested inside the outer list. In this article we will see how to create a 2D list from a given 1D list. We also supply the values for the number of elements inside the 2D list to the program.Using append and indexIn this approach we will create a for loop to loop through each element in the 2D list and use it as an index for the new list to be created. We keep incrementing ... Read More

Python - Convert a list of lists into tree-like dict

Pradeep Elance
Updated on 28-Dec-2020 11:19:45

1K+ Views

Given a nested list we want to convert it to a dictionary whose elements can be considered as part of a tree data structure. In this article we will see the two approaches to convert a nested list into to add dictionary whose elements represent a tree like data structure.Using SlicingWe reverse the items in the list aby slicing and then check if the item is present in the list. If it is not present we ignore it else we add it to the tree.Exampledef CreateTree(lst):    new_tree = {}    for list_item in lst:       currTree = ... Read More

Python - Convert flattened dictionary into nested dictionary

Pradeep Elance
Updated on 28-Dec-2020 11:18:13

709 Views

Python dictionaries have keys and values. If we have two or more dictionaries to be merged a nested dictionary, then we can take the below approaches. Here year the dictionaries are given along with the new keys that will become a key in the nested dictionary.Assigning keysIn this approach we will create a new empty dictionary. Then assigned the given dictionaries to each new key. The resulting dictionary will be a nested dictionary with the keys assigned.ExampledictA = {'Sun': 1, 'Mon': 2} dictB = {'Tue': 3, 'Sun': 5} # Given Dictionaries print("DictA : ", dictA) print("DictB: ", dictB) ... Read More

Python - Combine two dictionary adding values for common keys

Pradeep Elance
Updated on 28-Dec-2020 11:17:05

2K+ Views

When analyzing data with python we come across situations when we have to merge two dictionaries in such a way that we add the values of those elements whose keys have equal values. In this article we will see such two dictionaries getting added.With For loop and | OperatorIn this approach we design a for loop to check for the presence of the value of the key in both the dictionaries and then add them. Finally we merge the two dictionaries using the | operator available for the dictionaries.ExampledictA = {'Mon': 23, 'Tue': 11, 'Sun': 6} dictB = {'Wed': 10, ... Read More

Advertisements