Found 10476 Articles for Python

Counting Bits in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:59:03

2K+ Views

Suppose we have a non-negative integer number num. For each number i in the range 0 ≤ i ≤ num we have to calculate the number of 1's in their binary counterpart and return them as a list. So if the number is 5, then the numbers are [0, 1, 2, 3, 4, 5], and number of 1s in these numbers are [0, 1, 1, 2, 1, 2]To solve this, we will follow these steps −res := an array which holds num + 1 number of 0soffset := 0for i in range 1 to num + 1if i and i ... Read More

Largest Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:55:35

584 Views

Suppose there is a list of non-negative integers, we have to arrange them such that they form the largest number. So if the array is [10, 2], then the largest number will be 210.To solve this, we will follow these steps −Arrange the numbers where the most significant digits are greater than place them at first, like this arrangement the numbers. After that just join the numbers from the array.ExampleLet us see the following implementation to get a better understanding − Live Demofrom functools import cmp_to_key class Solution(object):    def largestNumber(self, nums):       for i in range(len(nums)):     ... Read More

Word Break in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:39:14

970 Views

Suppose we have one non-empty string s and a dictionary wordDict. That is containing a list of non-empty words, determine when s can be segmented into a space-separated sequence of one or more dictionary words. We have to follow some rules −The same word in the dictionary may be reused multiple numbers of times in the segmentation.We can assume that the dictionary does not contain duplicate words.Suppose the string s = “applepenapple”, and word dictionary is like [“apple”, “pen”], then the output will be true because the string s can be segmented as “apple pen apple”.Let us see the steps ... Read More

Binary Tree Inorder Traversal in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:11:48

3K+ Views

Suppose we have a binary tree. We have to traverse this tree using the inorder traversal scheme without using recursion. So if the tree is likeThen the traversal will be [2, 5, 7, 10, 15, 20]To solve this, we will follow these steps −Create two array res and stack, set curr := rootRun one infinite loopwhile current is not nullpush curr into a stack, and set curr := left of currwhen the length of stack = 0, then return resnode := popped element from the stackinsert a value of node into rescurr := right of currExampleLet us see the following ... Read More

Decode Ways in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:06:16

1K+ Views

Suppose we have a message containing letters from A to Z is being encoded to numbers using the following mapping − 'A' → 1, 'B' → 2 ... 'Z' → 26. So if we have one non-empty string, containing only digits, then we have to find, in how many ways that can be decoded. So if the string is like “12”, then that can be made from “AB”, or “L”, so there are two possible ways. So the answer will be 2.To solve this, we will follow these steps −We will solve this using dynamic programming.n := length of sdp ... Read More

Combinations in C++

Arnab Chakraborty
Updated on 27-Apr-2020 14:16:54

1K+ Views

Suppose we have two integers n and k. We have to find all possible combinations of k numbers out of 1 ... n. So if n = 4 and k = 2, then the combinations will be [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]To solve this, we will follow these steps −We will use recursive function to solve this. the function solve() is taking n, k, temp array and start. The start is initially 1. This will act likeif size of temp array = k, then insert temp into res array, and returnfor i := ... Read More

Sort Colors in Python

Arnab Chakraborty
Updated on 27-Apr-2020 14:12:40

3K+ Views

Suppose we have an array with n objects. These are colored red, white, or blue, sort them in place so that the objects of the same color are adjacent. So with the colors in the order red, white and blue. Here, we will use the numbers like 0, 1, and 2 to represent the color red, white, and blue respectively. So if the array is like [2,0,2,1,1,0], then the output will be [0,0,1,1,2,2]To solve this, we will follow these steps −set low := 0, mid := 0 and high := length of array – 1while mid

Pow(x, n) in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:51:18

2K+ Views

Suppose we have two inputs x and n. x is a number in range -100.0 to 100.0, and n is a 32-bit signed integer. We have to find x to the power n without using library functions.So if the given inputs are x = 12.1, n = -2, then output will be 0.00683To solve this, we will follow these steps −power := |n| and res := 1.0while power is not 0if last bit of power is 1, then res := res * xx := x * xif n < 0return 1 / resreturn resExample(Python)Let us see the following implementation to ... Read More

Rotate Image in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:45:13

456 Views

Suppose we have one 2D matrix, that is representing one image. We have to rotate this image to 90 degrees clockwise. So if the image is like157963213Then the output will be291165337To solve this, we will follow these steps −Consider temp_mat = [], col := length of matrix – 1for col in range 0 to length of matrixtemp := []for row in range length of matrix – 1 down to -1add matrix[row, col] in tempadd temp into temp_matfor i in range 0 to length of matrixfor j in range 0 to length of matrixmatrix[i, j] := temp_mat[i, j]Example(Python)Let us see the ... Read More

Multiply Strings in C++

Aishwarya Naglot
Updated on 11-Nov-2024 15:46:46

9K+ Views

We have given two strings consisting of integers and can have lengths up to 200. both strings do not contain any leading 0, but 0 as the number itself can be present. We have to multiply integer strings, so we need to find a solution. Let's see how we can tackle this problem in an easier way. Suppose we have two numbers as strings. We need to multiply them and return the result, also in a string. For example, if the numbers are "26" and "12", then the result will be "312". Following are various ways to ... Read More

Advertisements