Found 10476 Articles for Python

Longest Increasing Path in a Matrix in Python

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

569 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

709 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

1K+ 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

Parallel Courses in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:21:33

248 Views

Suppose there are N courses, and these are labelled from 1 to N. We also gave a relation array, where relations[i] = [X, Y], is representing a prerequisite relationship between course X and course Y. So, this means course X has to be studied before course Y.In one semester we can study any number of courses as long as we have studied all the prerequisites for the course we are studying. We have to find the minimum number of semesters needed to study all courses. And if there is no way to study all the courses, then return -1.So, if ... Read More

Divide Array Into Increasing Sequences in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:12:47

240 Views

Suppose we have a non-decreasing array of positive integers called nums and an integer K, we have to find out if this array can be divided into one or more number of disjoint increasing subsequences of length at least K.So, if the input is like nums = [1, 2, 2, 3, 3, 4, 4], K = 3, then the output will be true, as this array can be divided into the two subsequences like [1, 2, 3, 4] and [2, 3, 4] with lengths at least 3 each.To solve this, we will follow these steps −d := a new mapreq ... Read More

trunc() in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:22:56

304 Views

In this tutorial, we are going to learn about the math.trunc() method.The method math.trunc() is used to truncate the float values. It will act as math.floor() method for positive values and math.ceil() method for negative values.Example Live Demo# importing math module import math # floor value print(math.floor(3.5)) # trunc for positive number print(math.trunc(3.5))OutputIf you run the above code, then you will get the similar result as follows.3 3Example Live Demo# importing math module import math # ceil value print(math.ceil(-3.5)) # trunc for negative number print(math.trunc(-3.5))OutputIf you run the above code, then you will get the similar result as follows.-3 -3ConclusionIf you have ... Read More

time.process_time() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:21:15

2K+ Views

In this tutorial, we are going to learn about the time.process_time() method.The method time.process_time() will return a float value of time in seconds of a system and user CPU time of the current process.Example Live Demo# importing the time module import time # printing the current process time print(time.process_time()) 1.171875OutputIf you run the above code, then you will get the similar result as follows.1.171875Let's say we have a process that prints from in the given range. Let's find the time for that process. Understand the following code and run it.Example Live Demo# importing the time module import time # program to find ... Read More

time.perf_counter() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:19:13

4K+ Views

In this tutorial, we are going to learn about the time.perf_counter() method.The method time.perf_counter() returns a float value of time in seconds. Let's see anExample Live Demo# importing the time module import time # printing the time print(time.perf_counter())OutputIf you run the above code, then you will get the following result.263.3530349We can use the time.perf_counter() method to find the execution time of a program. Let's see an example.Example Live Demo# importing the time module import time # program to find the prime number def is_prime(number):    for i in range(2, number):       if number % i == 0:       ... Read More

The most occurring number in a string using Regex in python

Hafeezul Kareem
Updated on 11-Jul-2020 08:18:27

231 Views

In this tutorial, we are going to write a regex that finds the most occurring number in the string. We will check the regex in Python.Follow the below steps to write the program.Import the re and collections modules.Initialize the string with numbers.4Find all the numbers using regex and store them in the array.Find the most occurring number using Counter from collections module.Example Live Demo# importing the modules import re import collections # initializing the string string = '1222tutorials321232point3442' # regex to find all the numbers regex = r'[0-9]' # getting all the numbers from the string numbers = re.findall(regex, string) # ... Read More

Taking multiple inputs from user in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:16:43

1K+ Views

In this tutorial, we are going to learn how to take multiple inputs from the user in Python.The data entered by the user will be in the string format. So, we can use the split() method to divide the user entered data.Let's take the multiple strings from the user.Example# taking the input from the user strings = input("Enter multiple names space-separated:- ") # spliting the data strings = strings.split() # printing the data print(strings)OutputIf you run the above code, then you will get the following result.Enter multiple names space-separated:- Python JavaScript Django React ['Python', 'JavaScript', 'Django', 'React']What if we want ... Read More

Advertisements