
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Count all prefixes in given string with greatest frequency using Python
In this tutorial, we are going to write a program that counts and prints the words with a higher frequency of an alphabet than the second one.
Take a string and two alphabets. The prefixes with a higher frequency of the first alphabet will be printed. And display the count at the end of the output.
Let's see some examples.
Input
string:- apple alphabets:- p, e
Output
ap app appl apple 4
Input
string:- apple alphabets:- e, p
Output
0
Let's see the steps to write the code.
Define a function and write the code in it.
Initialize count to 0 and an empty string.
Iterate over the string.
Get the prefix using the string slicing and index. And store it in an empty string.
Compare the alphabet frequency in the prefix.
Print and increment the count if satisfied.
Print the count at the end.
Example
# defining a function for multiple calles def prefixes(string, _1, _2): # count count = 0 # empty string for comparison prefix = "" # iterating over the string for i in range(len(string)): # getting the prefix from the string prefix = string[:i + 1] # comparing the count of alphabets in the prefix if prefix.count(_1) > prefix.count(_2): # printing the prefix if success print(prefix) # incrementing the count by 1 count += 1 # printing the count print(f"Total prefixes matched: {count}") if __name__ == '__main__': # invokging the function print(f"----------------apple p e---------------------") prefixes('apple', 'p', 'e') print() print(f"----------------apple e p---------------------") prefixes('apple', 'e', 'p')
Output
If you run the above code, you will get the following result.
----------------apple p e--------------------- ap app appl apple Total prefixes matched: 4 ----------------apple e p--------------------- Total prefixes matched: 0
Conclusion
If you are facing any problems in understanding the code, mention them in the comment section.
- Related Articles
- Count all prefixes of the given binary array which are divisible by x in C++
- Python – Count frequency of sublist in given list
- How can we remove all the prefixes or suffixes from a given string in MySQL?
- Check if a String starts with any of the given prefixes in Java
- Program to count substrings with all 1s in binary string in Python
- Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary
- Count all Palindromic Subsequence in a given String in C++
- Program to count submatrices with all ones using Python
- Check if a string has all characters with same frequency with one variation allowed in Python
- Count all pairs with given XOR in C++
- Python program to count number of vowels using set in a given string
- Python program to Count words in a given string?
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Count Unique Characters of All Substrings of a Given String in C++
