Author Archives: Roopendra

Count Character Frequency in Python

Count Character Frequency in Python Counting how many times each character appears in a string is a fundamental problem with many real-world applications, from text analysis to compression algorithms. Best Solution: Using Counter from collections import Counter def char_frequency(s): return Counter(s) result = char_frequency("hello world") print(result) # Output: Counter({'l': 3,…
Continue Reading

Check for Anagrams in Python

Check for Anagrams in Python Two strings are anagrams if they contain the same characters in the same frequency, just in a different order. For example, "listen" and "silent" are anagrams. Solution 1: Sort and Compare def is_anagram(s1, s2): return sorted(()) == sorted(()) print(is_anagram("listen", "silent")) # True print(is_anagram("hello", "world")) #…
Continue Reading

Remove Duplicates from a List in Python

Remove Duplicates from a List in Python Cleaning a list of duplicate values is a frequent data processing task. Python offers several clean approaches depending on whether order matters. Fastest Method: Convert to Set (Order Not Preserved) my_list = unique_list = list(set(my_list)) print(unique_list) # Output: (order may vary) Preserve Order:…
Continue Reading

Find the Second Largest Number in a List in Python

Find the Second Largest Number in a List in Python Finding the second largest element is a common interview problem. The key challenge is handling duplicates correctly. Solution 1: Remove Duplicates, Then Sort def second_largest(nums): unique = list(set(nums)) () return unique print(second_largest()) # Output: 45 Solution 2: Using sorted() and…
Continue Reading

Check for Palindrome in Python

Check for Palindrome in Python A palindrome is a string that reads the same forwards and backwards, such as "racecar" or "madam". This problem builds directly on string reversal. Simple Solution def is_palindrome(s): return s == s # Examples print(is_palindrome("racecar")) # True print(is_palindrome("hello")) # False Case-Insensitive Check def is_palindrome_ci(s): s…
Continue Reading
Verified by MonsterInsights