Given a string as input to the final problem, your task is to find the frequency of all the characters of a given string. Once you find the frequency of occurrences of all the characters in a given string, print out the least occurrences.
The least occurrences may be referred to as the character that is occurring only one time. Also the character occurring less than or equal to 2 times may be referred as list occurring character
Write a Python Function to Print Least Frequent
Let's take a function named list_frequent() and pass the input argument to this function as a string that has user-entered.
Take an empty dictionary variable frequency to store the key value pair. The key is Character that is present in the string and value is the counter that counts the occurrences of each character in given string
def least_frequent(text):
freq = dict()
Using the following, iterate all the letters present in the input string, check if the letter is already present in the dictionary then increment the value counter. Else if the letter is occurring first time in the dictionary so initialize the value to 1 as shown below.
for letter in text:
if letter in freq:
freq[letter] += 1
else:
freq[letter] = 1
When the loop is completed you will be having a key value pair that stores the frequency of occurrences of each character. So now we have to print the character that occurs least. For example the character has occurred only one time in the string.
Using the following, iterate the key value pair of the dictionary and check if value has occurred only one time then print all the keys separated by its space using the print function call.
for key, value in freq.items():
if value == 1:
print(key, end=" ")
This is optional : Since we don't know what exactly least occurrences refers to either single occurrences or 2 times
=============================
You can also change the least occurrences of a particular letter to 2 instead of 1 as in the above case, so let's change it to 2.
for key, value in freq.items():
if value <= 2:
print(key, end=" ")
In the main program, take a variable named as data, using the input function call and ask the user to enter the string. Once the string is entered, print a user-friendly message that is the least frequent character in a string using the print function call.
Use the variable named as data input to the above defined function and call the function least_frequent() as shown below
data = input("Enter a String : ")
print("Least Frequent Characters in String are ... ")
least_frequent(data)
Complete program For single occurrences
============================================
def least_frequent(text):
freq = dict()
for letter in text:
if letter in freq:
freq[letter] += 1
else:
freq[letter] = 1
for key, value in freq.items():
if value == 1:
print(key, end=" ")
data = input("Enter a String : ")
print("Least Frequent Characters in String are ... ")
least_frequent(data)
OUTPUT
============
Enter a String : Write a Python Function to Print Least Frequent Character in String
Least Frequent Characters in String are ...
W y L s q C S g
Change in Python Code for Least Occurrence as 2
===============================================
for key, value in freq.items():
if value <= 2:
print(key, end=" ")
OUTPUT
================
Enter a String : Write a Python Function to Print Least Frequent Character in String
Least Frequent Characters in String are ...
W P y h F u c L s q C S g
Conclusion
================
Make sure you know what exactly is a least occurrence refers to either one or two based on that you can replace the above python code.
Comment it down below if you have any suggestions to improve the above program.
0 Comments