Found 10784 Articles for Python

Merge Intervals in Python

Arnab Chakraborty
Updated on 04-May-2020 05:55:55

1K+ Views

Suppose we have a collection of intervals, we have to merge all overlapping intervals. So if the intervals are like [[1, 3], [2, 6], [8, 10], [15, 18]], then the intervals after merging will be [[1, 6], [8, 10], [15, 18]]. This is because there were two intervals those are overlapping, the intervals are [1, 3] and [2, 6], these are merged to [1, 6]Let us see the steps −if interval list length is 0, then return a blank listsort the interval list using quicksort mechanismstack := an empty stack, and insert intervals[0] into the stackfor i in range 1 ... Read More

Jump Game in Python

Arnab Chakraborty
Updated on 04-May-2020 05:54:49

3K+ Views

Suppose we have an array of non-negative integers; we are initially positioned at the first index of the array. Each element in the given array represents out maximum jump length at that position. We have to determine if we are able to reach the last index or not. So if the array is like [2, 3, 1, 1, 4], then the output will be true. This is like jump one step from position 0 to 1, then three step from position 1 to the end.Let us see the steps −n := length of array A – 1for i := n ... Read More

Combination Sum in Python

Arnab Chakraborty
Updated on 04-May-2020 05:44:46

2K+ Views

Suppose we have a set of candidate numbers (all elements are unique) and a target number. We have to find all unique combinations in candidates where the candidate numbers sum to the given target. The same repeated number may be chosen from candidates unlimited number of times. So if the elements are [2, 3, 6, 7] and the target value is 7, then the possible output will be [[7], [2, 2, 3]]Let us see the steps −We will solve this in recursive manner. The recursive function is named as solve(). This takes an array to store results, one map to ... Read More

Next Permutation in Python

Arnab Chakraborty
Updated on 04-May-2020 05:42:40

3K+ Views

Suppose we want to implement the next permutation method, that method rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, this method will rearrange it as the lowest possible order (That is actually, sorted in ascending order). The replacement must be in-place and do not use any extra memory. For example, if the Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1, 2, 3 → 1, 3, 2 3, 2, 1 → 1, 2, 3 1, 1, 5 → 1, 5, 1Let us see the steps −found ... Read More

Tkinter Programming in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:34:19

2K+ Views

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps −Import the Tkinter module.Create the GUI application main window.Add one or more of the above-mentioned widgets to the GUI application.Enter the main event loop to take action against each event triggered by the user.Example#!/usr/bin/python import Tkinter top = Tkinter.Tk() # Code to add widgets will go here... top.mainloop()This would ... Read More

Parsing XML with DOM APIs in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:32:17

2K+ Views

The Document Object Model ("DOM") is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents.The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time. If you are looking at one SAX element, you have no access to another.Here is the easiest way to quickly load an XML document and to create a minidom object using the xml.dom module. The minidom object provides a simple parser method that quickly creates a DOM tree from the XML file.The sample phrase calls the ... Read More

Parsing XML with SAX APIs in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:30:33

6K+ Views

SAX is a standard interface for event-driven XML parsing. Parsing XML with SAX generally requires you to create your own ContentHandler by subclassing xml.sax.ContentHandler.Your ContentHandler handles the particular tags and attributes of your flavor(s) of XML. A ContentHandler object provides methods to handle various parsing events. Its owning parser calls ContentHandler methods as it parses the XML file.The methods startDocument and endDocument are called at the start and the end of the XML file. The method characters(text) is passed character data of the XML file via the parameter text.The ContentHandler is called at the start and end of each element. If the parser is not in namespace mode, ... Read More

Multithreaded Priority Queue in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:24:30

901 Views

The Queue module allows you to create a new queue object that can hold a specific number of items. There are following methods to control the Queue −get() − The get() removes and returns an item from the queue.put() − The put adds item to a queue.qsize() − The qsize() returns the number of items that are currently in the queue.empty() − The empty( ) returns True if queue is empty; otherwise, False.full() − the full() returns True if queue is full; otherwise, False.Example#!/usr/bin/python import Queue import threading import time exitFlag = 0 class myThread (threading.Thread):    def __init__(self, threadID, name, q): ... Read More

Synchronizing Threads in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:21:11

11K+ Views

The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock.The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread waits to acquire the lock.If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released.The release() method ... Read More

The Threading Module in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:18:04

9K+ Views

The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module discussed in the previous section.The threading module exposes all the methods of the thread module and provides some additional methods −threading.activeCount() − Returns the number of thread objects that are active.threading.currentThread() − Returns the number of thread objects in the caller's thread control.threading.enumerate() − Returns a list of all thread objects that are currently active.In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows −run()  − The run() method is the entry point ... Read More

Advertisements