In Python, you can find and print all words in a string that have even length (divisible by 2). This is useful for text processing tasks where you need to filter words based on their character count. For example, in the string "A big cube was found inside the box", the words "cube" (length 4) and "inside" (length 6) have even lengths. Basic Approach The simplest method involves splitting the string into words and checking each word's length ? def print_even_length_words(text): # Split the string into individual words ... Read More
In this article, we will learn how to find the most occurring character in a string and count its occurrences using Python's Counter class. Problem Statement Given an input string, we need to find the character that appears most frequently and return both the character and its count. Approach Create a dictionary using Counter method having characters as keys and their frequencies as values. Find the maximum occurrence count and get the corresponding character. Using Counter from collections The Counter class provides an easy way to count character frequencies ? ... Read More
In this article, we will learn how to find the highest 3 values in a dictionary using different Python approaches. Problem Statement Given a dictionary, we need to find the three highest values and display them along with their corresponding keys. Using collections.Counter The Counter class provides a most_common() method that returns the n most common elements and their counts ? from collections import Counter # Initial Dictionary my_dict = {'a': 3, 'b': 4, 'c': 6, 'd': 5, 'e': 21} counter = Counter(my_dict) # Finding 3 highest values highest = counter.most_common(3) ... Read More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance