Found 10807 Articles for Python

Python program to find sum of absolute difference between all pairs in a list

Pavitra
Updated on 26-Sep-2019 13:57:48

244 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a list input , we need to find the sum of absolute difference between all pairs in a list.Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object type.In this method, we have a list ‘diffs’ which contains the absolute difference.We use two loops having two variables initialized . One is to iterate through the counter and another for the list element. In every iteration, we check whether the elements are similar or not.If not, ... Read More

Python program to find largest number in a list

Pavitra
Updated on 04-Jul-2020 12:37:12

213 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven list input, we need to find the largest numbers in the given list .Here we will discuss two approachesUsing sorting techniquesUsing built-in max() functionApproach 1 − Using built-in sort() functionExample Live Demolist1 = [18, 65, 78, 89, 90] list1.sort() # main print("Largest element is:", list1[-1])OutputLargest element is: 90Approach 2 − Using built-in max() functionExample Live Demolist1 = [18, 65, 78, 89, 90] # main print("Largest element is:",max(list1))OutputLargest element is: 90ConclusionIn this article, we learnt about the approach to find largest number in a list.

Python program to convert decimal to binary number

Pavitra
Updated on 04-Jul-2020 12:41:01

873 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a number we need to convert into a binary number.Approach 1 − Recursive SolutionDecToBin(num):    if num > 1:       DecToBin(num // 2)       print num % 2Exampledef DecimalToBinary(num):    if num > 1:       DecimalToBinary(num // 2)    print(num % 2, end = '') # main if __name__ == '__main__':    dec_val = 35    DecimalToBinary(dec_val)Output100011All the variables and functions are declared in the global scope as shown below −Approach 2 − Built-in SolutionExample Live Demodef ... Read More

Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array

Pavitra
Updated on 26-Sep-2019 12:33:11

204 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array input of integers, we need to find whether it’s possible to make an integer using all the digits available in these numbers such that it would be divisible by 3.Here we will generate a function that will take two arguments namely the array of integers and the length of the array.The implementation given below works on the concept from the mental mathematics. Here we observe that a number is divisible by 3 if the sum of the digits are divisible ... Read More

Python program to check if the given string is pangram

Pavitra
Updated on 26-Sep-2019 12:24:23

6K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a string input, we need to generate a Python program to check whether that string is Pangram or not.A pangram is a sentence/ series of words which contains every letter in the English Alphabets collection.Now let’s see how we can solve the problemWe will use a loop that checks whether each character present in the input string belongs to the alphabet set or not which we will declare manually .The implementation of the approach above is given by −Example Live Demoimport string def ... Read More

Python program to check if a given string is number Palindrome

Pavitra
Updated on 26-Sep-2019 12:22:39

6K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a string input, we need to create a python function to check whether it is a palindrome or not.A string is referred to be palindrome if the reverse of the string is identical with string.We can do this by two methods −Reversal by slicingComparison via negative indexingHere we will be learning reversal pf string bu the help of slicing.To reverse a string by th method of slicing specify the following statement −Str[ : : -1 ]Where the start and end parameters are ... Read More

Python Program to check if the given array is Monotonic

Pavitra
Updated on 26-Sep-2019 12:17:17

986 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array input Arr containing n integers. We need to check whether the input array is Monotonic in nature or not.An array is said to be monotonic in nature if it is either continuously increasing or continuously decreasing.Mathematically,An array A is continuously increasing if for all i

Python program to check if a string contains all unique characters

Pavitra
Updated on 26-Sep-2019 12:14:14

408 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a sring input, we need to find whether a string contains all unique characters or not.ApproachWe will create an array of boolean values, where the variable flag at the index i indicates that whether character i in the alphabet is contained in the string or not.The second time we encounter this character we can immediately return false as string characters is no longer unique.We can also return false if the string length exceeds the value of number of unique characters presnt in ... Read More

Python Program to Check Armstrong Number

Pavitra
Updated on 26-Sep-2019 12:11:58

1K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an integer n , we need to check that the given integer is an armstrong number.A positive integer is called an Armstrong number of order n ifabcd... = a^n + b^n + c^n + d^n + …Here we will be discussing the brute-force approach for an armstrong number of 3 digits and hence of order three.To check the armstrong number of order n we need to replace 3 by the corresponding order value in line number 7.Now let’s see the implementation −Example Live ... Read More

Python Program for Sum of squares of first n natural numbers

Pavitra
Updated on 26-Sep-2019 12:04:49

575 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a positive integer N as input . We need to compute the value of 12 + 22 + 32 + ….. + N2.Problem statement:This can be solved by two methodsMultiplication addition arithmeticUsing mathematical formulaApproach 1: Multiplication & Addition ArithmeticHere we run a loop from 1 to n and for each i, 1

Advertisements