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
Server Side Programming Articles
Page 318 of 2109
Python - Given an integer 'n', check if it is a power of 4, and return True, otherwise False.
When it is required to check if a given variable is a power of 4, we can use different approaches. A power of 4 means the number can be expressed as 4k where k is a non-negative integer (1, 4, 16, 64, 256, etc.). Method 1: Using Division and Modulus This method repeatedly divides the number by 4 and checks if there's any remainder ? def check_power_of_4(num): if num == 0: return False while num != 1: ...
Read MorePython - Check if two strings are isomorphic in nature
Two strings are isomorphic if there exists a one-to-one mapping between characters of both strings. This means each character in the first string can be replaced to get the second string, and no two characters map to the same character. What are Isomorphic Strings? Strings are isomorphic when: They have the same length Each character in string1 maps to exactly one character in string2 No two different characters in string1 map to the same character in string2 For example: "aab" and "xxy" are isomorphic (a→x, b→y), but "aab" and "xyz" are not (both 'a' characters ...
Read MorePython - Check if a number and its double exists in an array
When it is required to check if a number and its double exists in an array, we can iterate through the array and check if double of each element exists. Python provides multiple approaches to solve this problem efficiently. Method 1: Using Nested Loops This approach iterates through each element and checks if its double exists in the remaining elements ? def check_double_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i] + my_list[i+1:]): ...
Read MorePython Pandas – Remove numbers from string in a DataFrame column
To remove numbers from strings in a DataFrame column, we can use the str.replace() method with regular expressions. This is useful for cleaning data where numbers need to be stripped from text fields. Basic Setup First, let's import pandas and create a sample DataFrame with student records ? import pandas as pd # Create DataFrame with student records dataFrame = pd.DataFrame({ "Id": ['S01', 'S02', 'S03', 'S04', 'S05', 'S06', 'S07'], "Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', ...
Read MorePython - Find the number of prime numbers within a given range of numbers
When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The % modulus operator is used to check divisibility and identify prime numbers. What is a Prime Number? A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. Basic Approach Below is a demonstration of finding prime numbers in a range ? lower_range = 670 upper_range = 699 print("The lower ...
Read MorePython - Given an integer list, find the third maximum number if it exists
Finding the third maximum number in an integer list requires tracking the three largest distinct values. Python provides several approaches: using a tracking algorithm, built-in functions, or set operations to handle duplicates. Using Tracking Variables This approach maintains three variables to track the largest, second largest, and third largest values ? def third_max_num(numbers): result = [float('-inf'), float('-inf'), float('-inf')] for num in numbers: if num not in result: ...
Read MorePython - Find the length of the last word in a string
Finding the length of the last word in a string is a common string manipulation task in Python. There are several approaches to solve this problem, from custom iteration to using built-in string methods. Method 1: Using Custom Iteration This approach removes extra spaces and iterates through the string to count characters of the last word ? def last_word_length(my_string): init_val = 0 processed_str = my_string.strip() for i in range(len(processed_str)): if processed_str[i] ...
Read MorePython Pandas - Display unique values present in each column
To display unique values in each column, use the unique() method and set the column within it. At first, import the required library − import pandas as pd Create a DataFrame with two columns and duplicate records − import pandas as pd dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } ) print("DataFrame ...") print(dataFrame) ...
Read MorePython Program to accept string starting with vowel
When it is required to accept strings that start with a vowel, Python's startswith() method can be used to check if a string begins with specific characters. This method is particularly useful for filtering strings based on their first character. Basic Example Below is a demonstration of filtering strings that start with vowels ? my_list = ["Hi", "there", "how", "are", "u", "doing"] print("The list is:") print(my_list) my_result = [] vowels = "aeiou" for word in my_list: flag = False for ...
Read MorePython - Find words greater than given length
When it is required to find words that are greater than a specific length, we can define a method that splits the string and iterates through it. It checks the length of each word and compares it with the given length. If the word length is greater, it is added to the result. Using a Custom Function Below is a demonstration using a custom function ? def string_check(string_length, my_string): result_string = [] words = my_string.split(" ") for x in words: ...
Read More