Find Better Divisor of a Number in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:33:33

460 Views

Suppose we have a number n. We have to find divisor of n which one is better based on these conditions: We have two numbers p and q, the one whose digits sum to a larger number is called better than the other one. When the sum of digits is same, then the smaller number is the better one.So, if the input is like n = 180, then the output will be 9 because the divisors are [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]. So the number whose digit ... Read More

Find Length of a List Without Using Built-in Length Function in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:31:13

772 Views

Suppose we have a list nums. We have to find the length of this list but without using any length(), size() or len() type of functions.So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be 9.To solve this, we will follow these steps −Solve this by map and list operationsx := a list which contains all elements in numsconvert all elements in x to 1find sum of x by using sum() methodIn this example we have used the map() method to convert all into 1 by defining an ... Read More

Reverse a List by List Slicing in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:21:58

262 Views

Suppose we have a list of n elements called nums. We have to reverse this list by list slicing operations.So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be [2, 6, 3, 9, 6, 4, 6, 7, 5]To solve this, we will follow these steps −list slicing takes at most three parameters separated by colon. First one is start, second one is end and third one is stephere as we start from 0 we do not pass first parameter, as we end at n, we also not providing ... Read More

Create a List with n Elements from 1 to n in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:14:45

3K+ Views

Suppose we have a number n. We have to create a list of elements of size n, the elements are from 1 to n.So, if the input is like n = 5, then the output will be [1, 2, 3, 4, 5]To solve this, we will follow these steps −use python list comprehension strategy to solve this problemcreate a list with i for each i from 1 to n, for this we use range() function. This will take lower bound which is n here, and upper bound n+1 because we want to generate up to n.ExampleLet us see the following ... Read More

Get Average Heights of Distinct Entries in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:09:11

702 Views

Suppose we have a set of heights there may be some duplicate entries as well. We have to find the average of distinct entries of these heights.So, if the input is like heights = [96, 25, 83, 96, 33, 83, 24, 25], then the output will be 52.2 because the unique elements are [96, 25, 83, 33, 24], so sum is 96 + 25 + 83 + 33 + 24 = 261, average is 261/5 = 52.2.To solve this, we will follow these steps −h_set := a set from heights to remove duplicatesreturn sum of h_set items / size of ... Read More

Find Even Indexed Elements from List in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:09:01

2K+ Views

Suppose we have a list of elements called nums. We have to filter out all odd indexed elements, so only return even indexed elements from that list.So, if the input is like nums = [5,7,6,4,6,9,3,6,2], then the output will be [7, 4, 9, 6]To solve this, we will follow these steps −use python list slicing strategy to solve this problemstart from index 1, end at the end of list and increase each step by 2, so the slicingsyntax is [1::2]ExampleLet us see the following implementation to get better understanding −def solve(nums): return nums[1::2] nums = [5,7,6,4,6,9,3,6,2] print(solve(nums))Input[5,7,6,4,6,9,3,6,2]Output[7, 4, 9, 6]

Convert Complex Number to Polar Coordinate Values in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:07:03

437 Views

Suppose we have a complex number c, we have to convert it into polar coordinate (radius, angle). Complex number will be in form x + yj. The radius is the magnitude of the complex number which is square root of (x^2 + y^2). And the angle is the counter clockwise angle measured from positive x-axis to the line segment that joins x + yj to the origin. From the cmath library we can use the phase() function to calculate the angle. And abs() function on complex number will return the magnitude value.So, if the input is like c = 2+5j, ... Read More

Get All Permutations of Size R of a String in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:04:29

485 Views

Suppose we have a string s and a number r. We have to display all permutations of r number of characters in s. We have permutations() function to get all permutations. This function is present inside itertools library.So, if the input is like s = "HELLO" r = 3, then the output will be>['HEL', 'HEL', 'HEO', 'HLE', 'HLL', 'HLO', 'HLE', 'HLL', 'HLO', 'HOE', 'HOL', 'HOL', 'EHL', 'EHL', 'EHO', 'ELH', 'ELL', 'ELO', 'ELH', 'ELL', 'ELO', 'EOH', 'EOL', 'EOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', 'LOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', ... Read More

PHP grapheme_strlen Function

Urmila Samariya
Updated on 12-Oct-2021 06:54:56

321 Views

A grapheme is the smallest functional unit of a writing system. Graphemes can be interpreted as the smallest units of writing that correspond with sounds.The grapheme_strlen() function in PHP is used to get the string length in grapheme units. This function does not get the byte or character's length. The grapheme_strlen function is supported in PHP 5.3.0 and higher versions.Syntaxinteger grapheme_strlen($str_string)Parametersgrapheme_strlen() accepts only one parameter −$str_string − The string parameter is used to measure the length of the string and it must use a UTF-8 encoding string.Return ValuesThis function returns the length of the string on success or it returns ... Read More

C++ Program to Demonstrate Exception Handling

Arnab Chakraborty
Updated on 12-Oct-2021 06:53:43

387 Views

Suppose there is a function that calculates some serious complex mathematical operations. But during the operations, some exceptions can occur. We have to handle the different types of exceptions that can occur and perform the following.If the computer is unable to allocate the memory for computation, we have to print 'Memory Low!'If any other C++-related exception occurs, we have to print 'Exception:' then the exception.If something else happens, we print 'Unhandled exception'.We are given an array that contains a pair of values, and we pass it to the function. If any exception occurs, we handle it, or otherwise, we print ... Read More

Advertisements