Tuesday, 7 January 2025

Day 80: Python Program that Displays which Letters are in First String but not in Second

 


from collections import Counter

def find_unique_letters(str1, str2):
    count1 = Counter(str1)
    count2 = Counter(str2)
    
    unique_letters = count1 - count2
    
    result = []
    for char, count in unique_letters.items():
        result.extend([char] * count) 
    return result

str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")

unique_letters = find_unique_letters(str1, str2)

print("Letters in the first string but not in the second string:", unique_letters)
#source code --> clcoding.com 

Code Explanation:

Importing Counter:
from collections import Counter: This imports the Counter class from Python's collections module. A Counter is a special dictionary used to count the occurrences of elements in an iterable (such as a string, list, etc.).

Defining the Function find_unique_letters:
def find_unique_letters(str1, str2): This defines a function that takes two strings str1 and str2 as input. The function will return a list of letters that are present in str1 but not in str2.

Counting Character Frequencies:
count1 = Counter(str1): This creates a Counter object for the first string str1, which counts how many times each character appears in str1. For example, if str1 = "aab", count1 would be:
{'a': 2, 'b': 1}
count2 = Counter(str2): Similarly, this creates a Counter object for str2, counting the character frequencies in str2.

Finding Unique Letters in str1 (Not in str2):
unique_letters = count1 - count2: This subtracts the Counter of str2 from the Counter of str1. The result is a new Counter object that contains only the characters that are present in str1 but not in str2. This subtraction operation works as follows:

It keeps the characters from str1 whose count is greater than in str2 (or if they don't appear in str2 at all).
For example, if str1 = "aab" and str2 = "abb", the result of count1 - count2 would be:
{'a': 1}  # Since 'a' appears once more in str1 than in str2

Building the Result List:
result = []: Initializes an empty list result to store the characters that are found in str1 but not in str2.
for char, count in unique_letters.items(): This loops through each character (char) and its count (count) in the unique_letters Counter object.
result.extend([char] * count): For each character, it appends that character to the result list a number of times equal to its count. For example, if 'a': 1, then 'a' will be added once to the result list.

Returning the Result:
return result: After building the list of unique letters, the function returns the result list, which contains the letters that are in str1 but not in str2.

User Input:
str1 = input("Enter the first string: "): This takes the first string input from the user.
str2 = input("Enter the second string: "): This takes the second string input from the user.

Calling the Function:
unique_letters = find_unique_letters(str1, str2): This calls the find_unique_letters function with the user-provided strings str1 and str2 and stores the result in unique_letters.

Printing the Result:
print("Letters in the first string but not in the second string:", unique_letters): This prints the list of letters that are found in str1 but not in str2.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (78) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (951) Python Coding Challenge (398) Python Quiz (47) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses