Found 10784 Articles for Python

Program to find number of ways to arrange n rooks so that they cannot attack each other in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:47:26

352 Views

Suppose we have a number n representing a chessboard of size n x n. We have to find the number of ways we can place n rooks, such that they cannot attack each other. Here two ways will be considered different if in one of the ways, some cell of the chessboard is occupied, and another way, the cell is not occupied. (We know that rooks can attack each other if they are either on the same row or on the same column).So, if the input is like 3, then the output will be 6To solve this, we will follow ... Read More

Program to find number of nodes in a range in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:51:34

768 Views

Suppose we have a BST, and we also have left and right bounds l and r, we have to find the count of all nodes in root whose values are present in between l and r (inclusive).So, if the input is likel = 7, r = 13, then the output will be 3, as there are three nodes: 8, 10, 12.To solve this, we will follow these steps−stack := a stack and insert root at first, count := 0while stack is not empty, donode := top element of stack, and pop elementif node is not null, thenif l

Program to check whether given number is Narcissistic number or not in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:44:09

564 Views

Suppose we have a number n; we have to check whether it is equal to the sum of the digits of n to the power of the number of digits.So, if the input is like 9474, then the output will be True as 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474.To solve this, we will follow these steps −s := a list of digits in of nreturn true if n is same as sum of x*(size of s) for all x in s, otherwise falseLet us see the following implementation to ... Read More

Program to sort all even and odd numbers in increasing and decreasing order respectively in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:38:27

2K+ Views

Suppose we have a list of numbers called nums, we have to sort the array by maintaining following criteriaEven numbers are sorted in ascending orderOdd numbers are sorted in descending orderThe relative positions of the even and odd numbers should not be changed.So, if the input is like [9, 14, 12, 91, -4, 5], then the output will be [91, -4, 12, 9, 14, 5]To solve this, we will follow these steps −evens := list of even terms in nums arrayodds := list of odd terms in nums arraysort the list evenseven_i := 0, odd_i := 0for index in range ... Read More

Program to find all missing numbers from 1 to N in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:36:12

2K+ Views

Suppose we have a list of numbers called nums of size n where all numbers in the list are present in the interval [1, n] some elements may appear twice while others only once. We have to find all of the numbers from [1, n] such that they are not in the list. We have to return the numbers sorted in ascending order. We have to try to find a solution that takes linear time and constant space.So, if the input is like [4, 4, 2, 2, 6, 6], then the output will be [1, 3, 5].To solve this, we ... Read More

Program to find an element in list whose value is same as its frequency in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:34:55

148 Views

Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency in the list is same as its value or not.So, if the input is like [2, 4, 8, 10, 4, 4, 4], then the output will be TrueTo solve this, we will follow these steps −res := a new map to store value wise frequencyfor each key value pair (k, v) in res, doif k is same as v, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):     ... Read More

Program to find the minimum cost to arrange the numbers in ascending or descending order in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:32:43

583 Views

Suppose we have a list of numbers called nums, we have to find the minimum cost to sort the list in any order (Ascending or Descending). Here the cost is the sum of differences between any element's old and new value.So, if the input is like [2, 5, 4], then the output will be 2.To solve this, we will follow these steps −temp:= copy the array numssort the list tempc1:= 0, c2:= 0n:= size of numsfor i in range 0 to n, doif nums[i] is not same as temp[i], thenc1 := c1 + |nums[i]-temp[i]|if nums[i] is not same as temp[n-1-i], ... Read More

Program to merge two sorted list to form larger sorted list in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:31:29

156 Views

Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may different.For an example, suppose A = [1,2,4,7] and B = [1,3,4,5,6,8], then merged list C will be [1,1,2,3,4,4,5,6,7,8]We will solve this using recursion. So the function will work like below −x:= a new listi:= 0, j:= 0while i < size of (lst0) and j < size of (lst1), doif lst0[i] > lst1[j], theninsert lst1[j] at the end of xj:= j+1otherwise when lst0[i]

Program to split a list of numbers such that the absolute difference of median values are smallest in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:29:13

120 Views

Suppose we have a list of numbers called nums, we have to divide it into two parts of same size where the absolute difference between each list's median is as small as possible and we have to find this difference. We have to keep in mind that here length of nums / 2 will be odd.So, if the input is like [2, 10, 8, 5, 4, 7], then the output will be 2, as we can make two lists like [2, 5, 10] and [4, 7, 8], then the medians are 5 and 7, their difference is 2.To solve this, ... Read More

Program to count how many times we can find "pizza" with given string characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:27:07

148 Views

Suppose we have a lowercase string s, we have to find how many "pizza" strings we can make using the characters present in s. We can use the characters in s in any order, but each character can be used once.So, if the input is like "ihzapezlzzilaop", then the output will be 2.To solve this, we will follow these steps −p_freq := Frequency of 'p' in si_freq := Frequency of 'i' in sz_freq := Frequency of 'z' in sa_freq := Frequency of 'a' in sreturn minimum of (p_freq, i_freq, z_freq/2 and a_freq)Let us see the following implementation to get better ... Read More

Advertisements