Found 10783 Articles for Python

Broadcasting with NumPy Arrays in Python

Pradeep Elance
Updated on 22-Jul-2020 08:15:47

134 Views

We know the arithmetic operations between different arrays happens monthly if the arrays are of equal size awesome required specific size. But there are scenarios when we can take erase of unequal size and still apply arithmetic operations on them by enhancing one of the arrays by filling array with smaller ndim prepended with '1' in its shape. So basically broadcasting and array means changing its shape to any required shape.Rules of array BoradcastingArray with smaller ndim than the other is prepended with '1' in its shape.Size in each dimension of the output shape is maximum of the input sizes ... Read More

Bisect Algorithm Functions in Python

Pradeep Elance
Updated on 22-Jul-2020 08:12:31

130 Views

This module provides support for maintaining a list in sorted order without having to sort the list after each insertion of new element. We will focus on two functions namely insort_left and insort_right.insort_leftThis function returns the sorted list after inserting number in the required position, if the element is already present in the list, the element is inserted at the leftmost possible position. This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered. The default value of the beginning and end position ... Read More

Avoiding class data shared among the instances in Python

Pradeep Elance
Updated on 22-Jul-2020 08:06:41

73 Views

When we instantiate a class in Python, all its variables and functions also get inherited to the new instantiated class. But there may be e occasions when we do not want some of the variables of the parent class to be inherited by the child class. In this article, we will explore two ways to do that.Instantiation ExampleIn the below example we show how the variables are instance heated from a given class and how the variables are shared across all the instantiated classes. Live Democlass MyClass:    listA= [] # Instantiate Both the classes x = MyClass() y = ... Read More

Average of each n-length consecutive segment in a Python list

Pradeep Elance
Updated on 22-Jul-2020 07:53:06

259 Views

We have a list containing only numbers. We plan to get the average of a set of sequential numbers from the list which keeps rolling from the first number to next number and then to next number and so on.ExampleThe below example simplifies the requirement of finding the average of each 4-length consecutive elements of the list.Given list: [10, 12, 14, 16, 18, 20, 22, 24, 26] Average of every segment of 4 consecutive numbers: [13.0, 15.0, 17.0, 19.0, 21.0, 23.0]With sum and rangeWe use the list comprehension approach to take the sum of the consecutive numbers by ... Read More

asksaveasfile() function in Python Tkinter

Pradeep Elance
Updated on 22-Jul-2020 07:49:40

858 Views

TKinter is a Python module which is used for GUI programming in Python. We create a Canvas and place our UI components with many properties and behaviors in it. In this article, we will see e how to use the ask essay file function to save files created through Python programs into the local drives.We first create a canvas on which we again place a button using the TTK dot button function. Then declare another function that will use the ask fine to define the file type and save the file into location in the local drive.Examplefrom tkinter import * ... Read More

Arithmetic operations in excel file using openpyxl in Python

Pradeep Elance
Updated on 22-Jul-2020 07:45:19

278 Views

Python can help us use excel files directly from the python environment. We can refer to the each cell or a range of cells in excel and apply arithmetic operators on those cells. The results of those operations can also be stored at some cells whose location can be specified by the python program.In the below examples we are performing various arithmetic operations using inbuilt functions of excel. Like sum or average of numbers inside cells. The results are also stored at specific locations. We use the openpyxl module which opens a workbook and marks it active. Then we store ... Read More

LFU Cache in Python

Arnab Chakraborty
Updated on 21-Jul-2020 09:00:43

2K+ Views

Suppose we want to design and implement a data structure for Least Frequently Used (LFU) cache system. It should support the following operations −get(key) – This will be used to get the value of the key if the key exists in the cache, otherwise return -1.put(key, value) – This will be used to set or insert the value if the key is not already present.When the cache reached its maximum capacity, it should invalidate the least frequently used element before inserting a new element.So, if the LFUCache is initialized with capacity 2 and call these methods cache.put(1, 1); cache.put(2, 2); ... Read More

Longest Increasing Path in a Matrix in Python

Arnab Chakraborty
Updated on 23-Jul-2020 07:28:09

427 Views

Suppose we have one matrix; we have to find the length of the longest increasing path. From each cell, we can either move to four directions − left, right, up or down. We cannot move diagonally or move outside of the boundary.So, if the input is like994668211then the output will be 4 as the longest increasing path is [3, 4, 5, 6].To solve this, we will follow these steps −Define a function solve(). This will take i, j, matrixif dp[i, j] is non-zero, thenreturn dp[i, j]dp[i, j] := 1temp := 0for r in range i-1 to i+2, dofor c in ... Read More

Optimize Water Distribution in a Village in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:34:17

522 Views

Suppose there are n houses in a village. We have to supply water for all the houses by building wells and laying pipes. For each house i, we can either build a well inside it, the building cost will be wells[i], or pipe in water from another well to it. The costs to lay pipes between houses are given by the array pipes, where each pipes[i] is [house1, house2, cost] represents the cost to connect house1 and house2 together using a pipe. These connections are bidirectional. We have to find the minimum total cost to supply water to all houses.So, ... Read More

String Transforms Into Another String in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:27:51

953 Views

Suppose we have two strings str1 and str2. And their lengths are same, we have to check whether we can transform str1 into str2 by doing zero or more conversions.In one conversion we can convert all occurrences of one character in str1 to any other lowercase English character. We have to check whether we can transform str1 into str2 or not.So, if the input is like str1 = "aabcc", str2 = "ccdee", then the output will be true, as Convert 'c' to 'e' then 'b' to 'd' then 'a' to 'c'. Here we have to keep in mind that the ... Read More

Advertisements