Write a program to check if the given word is Isogram & Pair isogram in python

Upasana | May 04, 2019 | 1 min read | 1,805 views | Python Coding Problems


An isogram is a word or phrase without a repeating letter.

Isogram

An isogram (also known as a "nonpattern word") is a logological term for a word or phrase without a repeating letter. It is also used by some people to mean a word or phrase in which each letter appears the same number of times, not necessarily just once.

Examples include:

  1. uncopyrightables - 16 characters

  2. abolishment - 11 letters

  3. complaints, background, bankruptcy - 10 letters

We can write a simple program that checks if the given word is Isogram or not using the below logic:

  1. lowercase the input word

  2. remove all the duplicate characters using set and store the output in variable a

  3. get all the characters without removing duplicate into another variable b

  4. check if length of a and b is exactly the same i.e. it is an Isogram, else not.

Isogram: python implementation
def check_isogram(string):
    string = string.lower()
    a = [x for x in list(set(list(string))) if x != ' ']
    b = [x for x in list(string) if x != ' ']
    if len(a) == len(b):
        return string + " is an Isogram"
    else:
        return string + " is not an Isogram"
print(check_isogram("document"))
Output
document is an Isogram

Pair Isograms

A pair isogram is a word where every letter appears exactly twice.

check_pair_isogram.py
from collections import Counter

def check_pair_isogram(string):
    dict = Counter(list(string))
    dict_list = dict.values()
    if len(set(dict_list)) == 1:
        return string + " is pair Isogram"
    else:
        return string + " is NOT pair Isogram"

print(check_pair_isogram("intestines"))
Output

intestines is pair Isogram


Top articles in this category:
  1. Write a program to find if a number is Lychrel Number in Python
  2. Python coding challenges for interviews
  3. Write a python program to find Largest Substring that occurs more than once
  4. Write a program for Hailstone Sequence in Python
  5. Connect to MySQL with Python 3.x and get Pandas Dataframe
  6. Find if credit card number is valid or not
  7. Connect to Postgresql with Python 3.x and get Pandas Dataframe

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.