Found 10476 Articles for Python

Length of a Linked List in Python

SaiKrishna Tavva
Updated on 09-Oct-2024 14:27:37

7K+ Views

To calculate the length of a Linked list, we traverse the list, counting each node until we reach the end, where the next node is None. Suppose, we have a singly linked list, we have to find its length. The linked list has fields next and val. So, if the input is like [2 -> 4 -> 5 -> 7 -> 8 -> 9 -> 3], then the output will be 7. The two commonly used methods to find the length of a linked list are as follows - ... Read More

Latin Square in Python

Arnab Chakraborty
Updated on 21-Oct-2021 09:37:38

1K+ Views

The Latin square is a matrix that has a special pattern. Let's see different examples to examine the pattern.1 2 2 1 1 2 3 3 1 2 2 3 1 1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1 The Latin square that you get will be of different size as you notice in the above examples. But, if you carefully observe the above matrices' pattern, you will find out that the last number of the previous row comes as the first element of the next row.That's the pattern ... Read More

Largest product of contiguous digits in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:34:26

338 Views

Suppose we have two numbers num and k, we have to find the largest product of k contiguous digits in num. We have to keep in mind that num is guaranteed to have >= k digits.So, if the input is like num = 52689762 and k = 4, then the output will be 3024, largest product of 4 consecutive digits is (8*9*7*6) = 3024.To solve this, we will follow these steps −largest := 0cand := 1while (quotient of num/10)^(k-1) > 0, dodigits := (last digit of nums)^kcand := 1while digits > 0, docand := cand * (digits mod 10)if cand ... Read More

Largest Number By Two Times in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:32:53

283 Views

Suppose we have a list of numbers; we have to check whether the largest number is bigger than the second-largest number by more than two times. As an example, if the list is like [3, 9, 6], then it will return false, as 9 is not bigger than 12 (2 times 6). When the list is [6, 3, 15], it will return true, as 15 is bigger than 12 (2 times 6).To solve this, we will follow these steps −if size of nums < 2, thenreturn Falsep_max := minimum of nums[0] and nums[1]c_max := maximum of nums[0] and nums[1]for i ... Read More

Largest Gap in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:31:10

1K+ Views

Suppose we have a list of numbers called nums, we have to find the largest difference of two consecutive numbers in the sorted version of nums.So, if the input is like [5, 2, 3, 9, 10, 11], then the output will be 4, as the largest gap between 5 and 9 is 4.To solve this, we will follow these steps −n := sorted list numsans := a new listfor i in range 0 to size of n -2, doinsert n[i+1]-n[i] at the end of ansreturn maximum of ansLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

K Prefix in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:29:49

436 Views

Suppose we have a list of numbers nums and an integer k, we have to find the maximum possible i where nums[0] + nums[1] + ... + nums[i] ≤ k. We will return -1 if no valid i exists.So, if the input is like nums = [4, -7, 5, 2, 6], k = 5, then the output will be 3, the index is 3 as if we add 4+(-7)+5+2 = 4, this is less than k, if we add last element it will no longer less than k, so the index is 3.To solve this, we will follow these steps ... Read More

Knights' Attack in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:28:18

292 Views

Suppose we have a two-dimensional binary matrix, representing a rectangular chess board, here 0 is for empty cell and 1 is for a knight. The knight is able to move two squares away horizontally and one square vertically, or two squares vertically and one square horizontally (like chess board knight). We have to check whether any two knights are attacking each other or not.So, if the input is like000000100000010Then the output will be TrueTo solve this, we will follow these steps −row, col := row count of matrix, column count of matrixfor r in range 0 to row-1, dofor c ... Read More

K and -K in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:26:33

769 Views

Suppose we have a list of numbers called nums, we have to find the largest number k where k and -k both exist in nums (they can be the same number). If there's no such element, return -1.So, if the input is like [-5, 2, 9, -6, 5, -9], then the output will be 9.To solve this, we will follow these steps −L1:= a list of 0 and positive elements in numsL2:= a list of 0 and negative elements in numssort L1 in reverse ordersort the list L2for each i in L1, dofor each j in L2, doif i+j is ... Read More

Remove One to Make Average k in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:25:01

161 Views

Suppose we have a list of numbers called nums and an integer k, we have to check whether we can remove exactly one element from the list to make the average equal to exactly k. Now we have to keep in mind that, there are some constraints −2 ≤ n ≤ 1, 000 where n is number of elements of nums listnums[i], k ≤ 1, 000, 000So, if the input is like [5, 3, 2, 4, 6, 10], k = 4, then the output will be True as if we remove 10, then the average of elements will be (5+3+2+4+6)/5 ... Read More

Inverse Factorial in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:23:39

2K+ Views

Suppose we have a number a, we have to find n, such that factorial of n (n!) is same as a. As we know, the factorial n = n * (n - 1) * (n - 2) * ... * 1. If there is no such integer n then return -1.So, if the input is like a = 120, then the output will be 5.To solve this, we will follow these steps −i := 0, num := 1L:= a new listwhile i < a, doi := factorial of numinsert i at the end of Lnum := num + 1if a ... Read More

Advertisements