Answer to Question #349721 in Python for Bhavani

Question #349721

Family List:


A list of words is called the Family if the words in the list follow the following rule.

we should be able to obtain each word in the list by

-changing exactly one letter from the previous word in the list

Given a list of words ,determine if the list is a Family or Not a Family

input:

The first line of input is an integer T representing the number of test cases

the first line of each test case has an integer N representing the total number of words

The second line contains N space separated strings

INPUT:

3

3

hip hop top

3

hip top hop

4

teat treat greet meet

output:

family

not a family

not a family

i/p:

3

3

tic tac toe

2

tic tac

3

tet treat greet

o/p:

not a family

family

not a family


1
Expert's answer
2022-06-10T09:24:52-0400
with open('data.txt') as f:
    data = [str.replace("\n", "").split() for str in f.readlines()[2::2]]
    for seq in data:
        for i, word in enumerate(seq[1:]):
            prev = seq[i]
            if len(set(word) ^ set(prev)) != 2 or len(word) != len(prev):
                print('not a family')
                break
        else:
            print('family')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS