Thursday, 9 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:

1. Importing the Counter Class
from collections import Counter
The Counter class from Python's collections module is used to count how many times each character appears in a string. It produces a dictionary-like object where the keys are characters, and the values are their counts.

2. Defining the Function
def find_unique_letters(str1, str2):
    count1 = Counter(str1)
    count2 = Counter(str2)
The function takes two strings as input: str1 and str2.
It uses Counter to count the occurrences of each letter in both strings:
count1 stores the letter counts from str1.
count2 stores the letter counts from str2.

3. Subtracting Counts
    unique_letters = count1 - count2
This step computes the difference in counts between count1 and count2.
When you subtract one Counter from another, it keeps only the positive differences for each key. For 4. 

Preparing the Result
    result = []
    for char, count in unique_letters.items():
        result.extend([char] * count)
unique_letters.items() gives pairs of each letter (char) and its count (count) from the difference.
The result list is built by repeating each character based on its remaining count using [char] * count. 

5. Returning the Result
The function returns the list of letters present in str1 but not in str2.

6. Taking User Input and Calling the Function
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
The program prompts the user to input two strings.
These strings are passed to the find_unique_letters function.

7. Displaying the Result
print("Letters in the first string but not in the second string:", unique_letters)
The result is printed as a list of letters present in str1 but missing (or underrepresented) in str2.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (80) 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) Java quiz (1) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (953) Python Coding Challenge (398) Python Quiz (53) 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