Found 10476 Articles for Python

Program to find intervals that do not intersect the cut interval in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:23:27

233 Views

Suppose we have a sorted and disjoint intervals list and another list cut, that represents an interval. We have to delete all parts of intervals that are intersecting with cut interval, and return the new list.So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]] cut = [8, 46], then the output will be [[2, 8], [46, 61]]To solve this, we will follow these steps −cut_start, cut_end := cutans := a new listfor each start, end in intervals, doif maximum of cut_start and start < minimum of end and cut_end, thenif start < cut_start, theninsert interval ... Read More

Program to find number of minimum steps to reach last index in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:19:24

531 Views

Suppose we have a list of numbers called nums and we are placed currently at nums[0]. On each step, we can either jump from the current index i to i + 1 or i - 1 or j where nums[i] == nums[j]. We have to find the minimum number of steps required to reach the final index.So, if the input is like nums = [4, 8, 8, 5, 4, 6, 5], then the output will be 3, as we can jump from index 0 to index 4 as their values are both 4. And then we jump back to index ... Read More

Program to find number of friend groups in a set of friends connections in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:15:35

786 Views

Suppose we have a a friends list, where friends[i] is a list of people i is friends with. The connection of friendships are two-way. And each person is friend with themselves and two people are in a friend group as long as there is some path of mutual friends connecting them. We have to find the total number of friend groups.So, if the input is like friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]], then the output will be 3, as The three friend groups are as below −To solve this, we will follow ... Read More

Program to find maximum sum by flipping each row elements in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:13:02

277 Views

Suppose we have a 2D binary matrix. For any row or column in the given matrix we can flip all the bits. If we can perform any number of these operations, and that we treat each row as a binary number, we have to find the largest sum that can be made of these numbers.So, if the input is like010001then the output will be 11, as if we flip both rows we get 101 and 110, then the sum is 5 + 6 = 11To solve this, we will follow these steps −for each row r in matrix, doif r[0] ... Read More

Campus Bikes II in Python

Arnab Chakraborty
Updated on 18-Nov-2020 10:59:02

239 Views

Suppose we have a 2D grid, that represents a campus, there are N workers and M bikes, The value of N

Serialize and Deserialize BST in Python

Arnab Chakraborty
Updated on 17-Nov-2020 11:19:53

557 Views

Suppose we want to design an algorithm to serialize and deserialize a binary search tree. Serialization is the process of converting something (data structure or object) into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link. This can be reconstructed later that process is deserialization.So, if the input is like [5, 2, 9, 1, 3, 7], then the output will be Serialized output 5.2.9.1.3.7.N.N.N.N.N.N.N Deserialized output: 1, 2, 3, 5, 7, 9, (inorder traversal)To solve this, we will follow these steps −Define a function serialize() . ... Read More

Max Increase to Keep City Skyline in Python

Arnab Chakraborty
Updated on 17-Nov-2020 11:09:19

269 Views

Suppose we have a 2-dimensional array called grid, where each value of grid[i][j] represents the height of a building located there. We can increase the height of any number of buildings, by any amount. Height 0 is considered to be a building as well. At the end, the "skyline" when viewed from all four directions of the grid, must be the same as the skyline of the original grid. Because a city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. So we have to find the maximum total sum that ... Read More

Design Log Storage System in Python

Arnab Chakraborty
Updated on 16-Nov-2020 14:32:07

386 Views

Suppose we have some logs, that each log contains a unique id and timestamp. The Timestamp is a string that has the format: Year:Month:Day:Hour:Minute: Second, for example, 2019:01:01:23:59:59. All domains are zero-padded decimal numbers.We have to design a log storage system to implement the following functions −void Put(int id, string timestamp): This will take the log's unique id and timestamp, and it stores the log in the storage system.int[] Retrieve(String start, String end, String granularity): This will return the id of logs whose timestamps are within the range from start to end parameters. The granularity parameter indicates the time level ... Read More

Python - Inserting item in sorted list maintaining order

Hafeezul Kareem
Updated on 13-Nov-2020 19:17:11

8K+ Views

In this article, we are going to learn how to insert an item in a sorted list maintaining the order. Python has a built-in module called bisect that helps us to insert any element in an appropriate position in the list.Follow the below steps to write the code.Import the module bisect.Initialize list and element that need to insertThe module bisect has a method called insort that inserts an element into a list in an appropriate position. Use the method and insert the element.Print the list.Example Live Demo# importing the module import bisect # initializing the list, element numbers = [10, ... Read More

Python - Intersect two dictionaries through keys

Hafeezul Kareem
Updated on 13-Nov-2020 19:13:25

2K+ Views

In this article, we are going to learn how to intersect two dictionaries using keys. We have to create a new dictionary with common keys. Let's see an example.Input: dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} Output: {'A': 1, 'C': 3}We are going to use the dictionary comprehension to solve the problem. Follow the below steps to write the code.Initialize dictionaries.Iterate over the dictionary one and add elements that are not in dictionary two.Print the result.Example Live Demo# initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = ... Read More

Advertisements