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

Carvia Tech | May 04, 2019 at 11:42 AM | 5 views | Python Coding Problem


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. Top 100 interview questions on Data Science & Machine Learning
  2. Write a program for Hailstone Sequence in Python
  3. Write a python program to find Largest Substring that occurs more than once
  4. Pass the ball game: NxN matrix in python
  5. Write a program to find if a number is Lychrel Number in Python
  6. Introduction to Python 3.6 & Jupyter Notebook
  7. python problem 1: find the runner-up score


Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 month ago


Recommended books for interview preparation:
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel

This website uses cookies to ensure you get the best experience on our website. more info