Found 10784 Articles for Python

Product of Array Except Self in Python

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

1K+ Views

Suppose we have an array called nums of n integers where n > 1. We have to find an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. So if the input array is [1, 2, 3, 4], then the output will be [24, 12, 8, 6]. We have to solve this without using division operator.To solve this, we will follow these steps −right_mul := an array of size same as nums, fill it with 0last element of right_mul = last element of numsfor i in range 1 to length of ... Read More

Lowest Common Ancestor of a Binary Tree in Python

Arnab Chakraborty
Updated on 04-May-2020 09:05:27

902 Views

Suppose we have a binary tree. we have to find the Lowest common ancestor nodes of two given nodes. The LCA of two nodes p and q is actually as the lowest node in tree that has both p and q as decedent. So if the binary tree is like [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]. The tree will be like −Here LCA of 5 and 1 is 3To solve this, we will follow these steps −If the tree is empty, then return nullif p and q both are same as root, then return rootleft ... Read More

Kth Smallest Element in a BST in Python

Arnab Chakraborty
Updated on 04-May-2020 09:03:32

906 Views

Suppose we have a binary search tree. We have to find the Kth smallest element in that BST. So if the tree is like −So if we want to find 3rd smallest element, then k = 3, and result will be 7.To solve this, we will follow these steps −create one empty list called nodescall solve(root, nodes)return k – 1th element of nodesthe solve method is created, this takes root and nodes array, this will work as follows −if root is null, then returnsolve(left of root, nodes)add value of root into the nodes arraysolve(right of root, nodes)Let us see the ... Read More

Kth Largest Element in an Array in Python

Arnab Chakraborty
Updated on 04-May-2020 09:00:26

2K+ Views

Suppose we have an unsorted array, we have to find the kth largest element from that array. So if the array is [3, 2, 1, 5, 6, 4] and k = 2, then the result will be 5.To solve this, we will follow these steps −We will sort the element, if the k is 1, then return last element, otherwise return array[n – k], where n is the size of the array.Let us see the following implementation to get better understanding −Example Live Democlass Solution(object):    def findKthLargest(self, nums, k):       nums.sort()       if k ==1:   ... Read More

Implement Trie (Prefix Tree) in Python

Arnab Chakraborty
Updated on 04-May-2020 08:58:35

4K+ Views

Suppose we have to make the trie structure, with three basic operations like insert(), search(), startsWith() methods. We can assume that all inputs are in lowercase letters. For example, if we call the functions as follows, we will see the outputsTrie trie = new Trie()trie.insert(“apple”)trie.search(“apple”)     //This will return truetrie.search(“app”)        //This will return falsetrie.startsWith(“app”)   //This will return truetrie.insert(“app”)trie.search(“app”)        //This will return trueTo solve this, we will follow these steps −Initially make one dictionary called child.The insert method will be like −current := childfor each letter l in word −if l is not present in ... Read More

Number of Islands in Python

Arnab Chakraborty
Updated on 04-May-2020 08:57:19

2K+ Views

Suppose we have a grid, there are few 0s and few 1s. We have to count the number of islands. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.Suppose the grid is like −11000110000010000011There are three islands.To solve this, we will follow these steps −There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −if number of rows in the grid is ... Read More

Find Peak Element in Python

Arnab Chakraborty
Updated on 04-May-2020 08:53:26

3K+ Views

Suppose we have to find the peak element in an array. The peak element is an element that is greater than its neighbors. Suppose we have an input array nums, where nums[i] ≠ nums[i+1], search for a peak element and return its index. The array can hold multiple peak elements, in that case return the index to any one of the peak elements. We can imagine that nums[-1] = nums[n] = -∞. So if the array is like [1, 2, 1, 3, 5, 6, 4], the peak elements should be 1 or 5.To solve this, we will follow these steps ... Read More

Programs for printing pyramid patterns in Python

Pradeep Elance
Updated on 04-Feb-2020 07:35:31

1K+ Views

Taking advantage of the for loop and range function in python, we can draw a variety of for pyramid structures. The key to the approach is designing the appropriate for loop which will leave both vertical and horizontal space for the position of the symbol we choose for drawing the pyramid structure.Pattern -1We draw a right angle based pattern.Example Live Demodef pyramid(p):    for m in range(0, p):       for n in range(0, m+1):          print("* ", end="")       print("\r") p = 10 pyramid(p)OutputRunning the above code gives us the following result −* * ... Read More

Program to make Indian Flag in Python

Pradeep Elance
Updated on 04-Feb-2020 07:22:08

2K+ Views

Python’s libraries to draw graphs has very extensive features which can not only give us charts but also give us flexibility to draw other diagrams like flags. In that sense those modules have an artistic touch. In this article we will see how to draw the Indian flag using the libraries numpy and matplotlib.AppraochWe create three rectangles of same width and draw them with appropriate colours and borders.Use pyplot function to draw the circle of the Ashok Chakra at the center of the middle rectangle.Use numpy and matplotlib to draw the 24 lines inside the Ashok Chakra.In all the above ... Read More

Program to create grade calculator in Python

Pradeep Elance
Updated on 04-Feb-2020 07:15:38

2K+ Views

In Academics it is a common requirement to find the grades of students after an assessment. In this article we will create a Python program which will assign the grades based on the grading criteria. Will call it A grade calculator.Grading criteriaBelow is the grading criteria we have chosen for the program.score >= 90 : "O" score >= 80 : "A+" score >= 70 : "A" score >= 60 : "B+" score >= 50 : "B" score >= 40 : "C"Program ApproachInitialize variables and array to hold the student details including the marks obtained by individual subjects.Define a function to ... Read More

Advertisements