Found 10784 Articles for Python

Fast XML parsing using Expat in Python

Pradeep Elance
Updated on 04-Feb-2020 05:30:44

923 Views

Python allows XML data to be read and processed through its inbuilt module called expat. It is a non-validating XML parser. it creates an XML parser object and captures the attributes of its objects into various handler functions. In the below example we will see how the various handler functions can help us read the XML file as well as give the attribute values as the output data. This generated data can be used for the processing.Exampleimport xml.parsers.expat # Capture the first element def first_element(tag, attrs):    print ('first element:', tag, attrs) # Capture the last element def last_element(tag):   ... Read More

Analyzing Census Data in Python

Pradeep Elance
Updated on 04-Feb-2020 05:24:50

578 Views

Census is about recording information about a given population in a systematic manner. The data captured includes various category of information like – demographic, economic, habitation details etc. This ultimately helps the government in understanding the current scenario as well as Planning for the future. In this article we will see how to leverage python to analyze the census data for Indian population. We will look at various demographic and economic aspects. Then plot charge which will project the analysis in a graphical manner. The source that is collected from kaggle. It is located  here.Organizing the DataIn the below program ... Read More

Maximum Product Subarray in Python

Arnab Chakraborty
Updated on 04-May-2020 08:51:03

1K+ Views

Suppose we have an integer array called nums, we have to find the contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [2, 3, -2, 4], the output will be 6, as contiguous subarray [2, 3] has max product.To solve this, we will follow these steps −max_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0max_list[0] := nums[0] and min_list[0] := nums[0]for i in range 1 to length of numsmax_list[i] = max of max_list[i - 1]*nums[i], min_list[i - 1]*nums[i] and ... Read More

Construct Binary Tree from Inorder and Postorder Traversal in Python

Arnab Chakraborty
Updated on 04-May-2020 06:32:22

173 Views

Suppose we have the inorder and postorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the postorder and inorder sequences are [9, 15, 7, 20, 3] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps -Suppose the method is called buildTree with preorder and inorder listsroot := last node from the postorder, and delete first node from postorderroot_index := index of root.val from the inorder listleft or root := buildTree(subset of inorder from root_index + 1 to end, postorder)right or root := buildTree(subset of ... Read More

Construct Binary Tree from Preorder and Inorder Traversal in Python

Arnab Chakraborty
Updated on 04-May-2020 06:27:14

930 Views

Suppose we have the inorder and preorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the preorder and inorder sequences are [3, 9, 20, 15, 7] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps −Suppose the method is called buildTree with preorder and inorder listsroot := first node from the preorder, and delete first node from preorderroot_index := index of root.val from the inorder listleft or root := buildTree(preorder, subset of inorder from 0 to root_index)right or root := buildTree(preorder, subset of inorder ... Read More

Word Search in Python

Arnab Chakraborty
Updated on 04-May-2020 06:16:55

4K+ Views

Suppose we have a 2D board and a word, we have to find if the word is present in the grid or not. The words can be made from letters of sequentially adjacent cell, "adjacent" cells are those horizontally or vertically neighboring cells. We should not use the same letter cell more than once. So if the matrix is like −ABCESFCSADEFGiven words are say “ABCCED”, the answer will be true, for word “SEE”, it will be true, but for “ABCB” if will be false.Let us see the steps −We will solve this using recursive approach. So if the recursive method ... Read More

Subsets in Python

Arnab Chakraborty
Updated on 04-May-2020 06:14:43

998 Views

Suppose we have a set of numbers; we have to generate all possible subsets of that set. This is also known as power set. So if the set is like [1, 2, 3], then the power set will be [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]Let us see the steps −We will solve this using recursive approach. So if the recursive method name is called solve(), and this takes the set of numbers (nums), temporary set (temp), res and indexThe solve() function will work like below −if index = length of nums, then create ... Read More

Set Matrix Zeroes in Python

Arnab Chakraborty
Updated on 04-May-2020 06:09:38

1K+ Views

Consider we have a matrix, in that matrix if one element is 0, then make the entire row and column of that matrix to 0. The conversion will be in-place. So if the matrix is −101111111Then the output will be −000101101Let us see the steps −n := number of rows, m := number of columns, set flag := falseif mat[0, 0] = 0, then set flag := trueset row := false, and col := falsefor i in range 1 to nif mat[i, 0] = 0, then set col := True and break the loopfor i in range 1 to mif ... Read More

Simplify Path in Python

Arnab Chakraborty
Updated on 04-May-2020 06:08:30

885 Views

Suppose we have an absolute path for a file (Like Unix File system), we have to simplify it. Or in other words, we have to convert it to the canonical path. In the UNIX-style file system, a period ‘.’ refers to the current directory. And a double period ‘..’ moves the directory up a level (Parent directory). The properties of canonical paths are as follows.Path must always begin with a slash /There must be only a single slash / between two directory names.Last directory name (if it exists) must not end with a trailing /.Canonical path must be the shortest ... Read More

Spiral Matrix II in Python

Arnab Chakraborty
Updated on 04-May-2020 05:56:58

2K+ Views

Suppose we have a positive integer n, we have to generate a square matrix with n2 elements in spiral order. So if n = 5, then the matrix will be −12341213145111615610987Let us see the steps −set (row1, col1) := (0, 0) and (row2, col2) := (n, n), and create one matrix called res, then fill it with 0s, and set num := 1while num n2, then breakfor i in range row1 + 1 to row2, res[i, col2-1] = num, incase num by 1if num > n2, then breakfor i in range col2 – 2 down to col1 – 1, ... Read More

Advertisements