Program to Check if All Characters have Even Frequency

Python Program to Check if All Characters have Even Frequency

In the previous article, we have discussed Python Program to Find Two Odd Occurring Elements in an Array/List
The task is to check whether all characters in the given string made up entirely of lowercase letters have an even frequency.

Counter function in Python:

The counter is a set and dict subset. Counter() takes an iterable entity as an argument and stores the elements as keys and the frequency of the elements as a value. So, in collections, if we transfer a string. When you call Counter(), you’ll get a Counter class object with characters as keys and their frequency in a string as values.

Counter() returns a Counter type object (a subclass of dict) with all characters in the string as keys and their occurrence count as values. We’ll use the [] operator to get the occurrence count of the characters from it.

For example:

let given string = “pqrspqrspp”.

In this p occurred 4 times, q,r,s occurred 2 times.

Therefore the frequency of all the characters in a given string is Even.

Examples:

Example1:

Input:

Given String = "pqrspqrstutu"

Output:

Yes,the given string { pqrspqrstutu } contains all characters at even intervals

Example2:

Input:

Given String = "btechgeeks"

Output:

No,the given string { btechgeeks } does not contains all characters at even intervals

Program to Check if All Characters have Even Frequency in Python

Below are the ways to check whether all characters in the given string made up entirely of lowercase letters have an even frequency:

Method #1: Using For Loop (Static Input)

Approach:

  • Import the Counter() function from collections using the import keyword.
  • Give the string as static input and store it in a variable.
  • Pass the given string as an argument to the function determine.
  • Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as a key-value pair and stores this dictionary in another variable (say strngfreqelements).
  • Traverse in this frequency dictionary using the for loop.
  • Inside the loop, check if the Key has a value even or odd using the modulus operator and if conditional statement.
  • If the statement is true, then return False.
  • Return true after the for loop.
  • Check if the function returns true or false using the if conditional statement.
  • If it is true, print “yes, the given string contains all characters at even intervals”.
  • Else print “No, the given string does not contain all characters at even intervals”.
  • The Exit of the program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Pass the given string as an argument to the function determine.


def determine(gvn_strng):
  # Calculate the frequency of all the given string elements using the Counter() function
    # which returns the element and its frequency as a key-value pair and stores this
    # dictionary in another variable (say strngfreqelements).

    strngfreqelements = Counter(gvn_strng)
 # Traverse in this frequency dictionary using the for loop.
    for key in strngfreqelements:
        # Inside the loop, check if the Key has a value even or odd using the modulus operator
        # and if conditional statement.
        if (strngfreqelements[key] % 2 == 1):
           # If the statement is true, then return False.
            return False
  # Return true after the for loop.
    return True


# Give the string as static input and store it in a variable.
gvn_strng = "pqrspqrstutu"
# Check if the function returns true or false using the if conditional statement.
if(determine(gvn_strng)):
  # If it is true, print "yes, the given string contains all characters at even intervals".
    print("Yes,the given string {", gvn_strng,
          "} contains all characters at even intervals")
else:
  # Else print "No, the given string does not contain all characters at even intervals".
    print("No,the given string {", gvn_strng,
          "} does not contains all characters at even intervals")

Output:

Yes,the given string { pqrspqrstutu } contains all characters at even intervals

Method #2: Using For loop (User Input)

Approach:

  • Import the Counter() function from collections using the import keyword.
  • Give the string as user input using the input() function and store it in a variable.
  • Pass the given string as an argument to the function determine.
  • Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as a key-value pair and stores this dictionary in another variable (say strngfreqelements).
  • Traverse in this frequency dictionary using the for loop.
  • Inside the loop, check if the Key has a value even or odd using the modulus operator and if conditional statement.
  • If the statement is true, then return False.
  • Return true after the for loop.
  • Check if the function returns true or false using the if conditional statement.
  • If it is true, print “yes, the given string contains all characters at even intervals”.
  • Else print “No, the given string does not contain all characters at even intervals”.
  • The Exit of the program

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Pass the given string as an argument to the function determine.


def determine(gvn_strng):
  # Calculate the frequency of all the given string elements using the Counter() function
    # which returns the element and its frequency as a key-value pair and stores this
    # dictionary in another variable (say strngfreqelements).

    strngfreqelements = Counter(gvn_strng)
 # Traverse in this frequency dictionary using the for loop.
    for key in strngfreqelements:
        # Inside the loop, check if the Key has a value even or odd using the modulus operator
        # and if conditional statement.
        if (strngfreqelements[key] % 2 == 1):
           # If the statement is true, then return False.
            return False
  # Return true after the for loop.
    return True


# Give the string as user input using the input() function and store it in a variable.
gvn_strng = input("Enter some random String = ")
# Check if the function returns true or false using the if conditional statement.
if(determine(gvn_strng)):
  # If it is true, print "yes, the given string contains all characters at even intervals".
    print("Yes,the given string {", gvn_strng,
          "} contains all characters at even intervals")
else:
  # Else print "No, the given string does not contain all characters at even intervals".
    print("No,the given string {", gvn_strng,
          "} does not contains all characters at even intervals")

Output:

Enter some random String = btechgeeks
No,the given string { btechgeeks } does not contains all characters at even intervals

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.