Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 696 of 855
Python Program to find the sum of array
In this article, we will learn different approaches to find the sum of all elements in an array. Python provides both manual iteration and built-in functions to accomplish this task. Problem Statement Given an array as input, we need to compute the sum of all elements in the array. We can solve this using manual iteration or Python's built-in sum() function. Method 1: Using Built-in sum() Function The simplest approach uses Python's built-in sum() function − # Using built-in sum() function arr = [1, 2, 3, 4, 5] result = sum(arr) print('Sum of the ...
Read MorePython program to find sum of absolute difference between all pairs in a list
In this article, we will learn how to find the sum of absolute differences between all pairs in a list. This involves calculating |a - b| for every pair of elements and summing the results. Problem Statement Given a list of numbers, we need to find the sum of absolute differences between all unique pairs. For example, with list [1, 3, 5], we calculate |1-3| + |1-5| + |3-5| = 2 + 4 + 2 = 8. Method 1: Using Nested Loops We can use two nested loops to iterate through all pairs and calculate their ...
Read MorePython program to find largest number in a list
Finding the largest number in a list is a fundamental programming task in Python. There are several efficient approaches to accomplish this, each with different advantages depending on your specific needs. Problem Statement Given a list of numbers, we need to find the largest element efficiently. We'll explore three different approaches to solve this problem. Using Built-in max() Function The most straightforward and efficient approach is using Python's built-in max() function ? numbers = [18, 65, 78, 89, 90, 45, 23] largest = max(numbers) print("Largest element is:", largest) Largest element ...
Read MorePython program to convert decimal to binary number
Converting decimal numbers to binary is a fundamental programming task. Python provides both manual and built-in approaches to achieve this conversion. Problem Statement Given a decimal number, we need to convert it into its binary representation. Using Recursive Approach The recursive method divides the number by 2 repeatedly and prints the remainders in reverse order − Algorithm DecToBin(num): if num > 1: DecToBin(num // 2) print num % 2 Example def DecimalToBinary(num): ...
Read MorePython Program to check whether it is possible to make a divisible by 3 number using all digits in an array
In this article, we will learn how to check whether it's possible to make a number divisible by 3 using all digits from an array of integers. Problem Statement Given an array of integers, we need to determine if we can form a number using all the digits from these integers such that the resulting number would be divisible by 3. Mathematical Concept This solution is based on a fundamental property from number theory: a number is divisible by 3 if and only if the sum of its digits is divisible by 3. This means we ...
Read MorePython program to check if the given string is pangram
In this article, we will learn how to check if a given string is a pangram using Python. A pangram is a sentence that contains every letter of the English alphabet at least once. What is a Pangram? A pangram is a sentence or series of words that uses every letter in the English alphabet collection. Famous examples include "The quick brown fox jumps over the lazy dog" and "Pack my box with five dozen liquor jugs". Method 1: Using Manual Alphabet Check We can check each letter of the alphabet to see if it exists ...
Read MorePython Program to check if the given array is Monotonic
In this article, we will learn how to check if a given array is monotonic in nature. An array is monotonic if it is either continuously increasing or continuously decreasing. Problem Statement Given an array containing n integers, we need to determine whether the input array is monotonic or not. An array is said to be monotonic if it satisfies one of these conditions: Monotonic Increasing: For all indices i ≤ j, A[i] ≤ A[j] Monotonic Decreasing: For all indices i ≤ j, A[i] ≥ A[j] Approach We check if all adjacent ...
Read MorePython program to check if a string contains all unique characters
In this article, we will learn different approaches to check if a string contains all unique characters. This is a common programming problem that tests our understanding of data structures and string manipulation. Problem Statement Given a string input, we need to determine whether all characters in the string are unique (no character appears more than once). Using Boolean Array We can create an array of boolean values to track which characters we have seen. If we encounter a character that we've already seen, we return False immediately. Example def isUniqueChars(st): ...
Read MorePython Program to Check Armstrong Number
An Armstrong number (also called a narcissistic number) is a positive integer that equals the sum of its own digits raised to the power of the number of digits. For example, 153 is a 3-digit Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. Understanding Armstrong Numbers For a number with n digits, it's an Armstrong number if ? abcd... = a^n + b^n + c^n + d^n + ... Common examples include: 1-digit: 1, 2, 3, ..., 9 (all single digits) 3-digit: 153, 371, 407 ...
Read MorePython Program for Sum of squares of first n natural numbers
In this article, we will learn how to calculate the sum of squares of the first n natural numbers. Given a positive integer N, we need to compute 1² + 2² + 3² + ... + N². Problem Statement We need to find the sum of squares: 1² + 2² + 3² + ... + N² for a given positive integer N. This problem can be solved using two approaches ? Iterative approach using a loop Direct mathematical formula Method 1: Using Loop (Iterative Approach) In this method, we iterate from 1 to ...
Read More