Answer to Question #348710 in Python for siva

Question #348710

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

-or , by adding exactly one letter to the previous word in the list

-or ,by removing 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

Note:please display the output


1
Expert's answer
2022-06-06T15:12:50-0400
t = int(input('the number of test cases: '))
def family(t):
    flag = True
    total = []
    for i in range(t):
        res = []
        n = int(input('the total number of words: '))
        words = input('Words: ')
        for word in words.split(' '):
            res.append(word)
        if n != len(res):
            total.append('not a family')
        else:
            for word in range(1, len(res)):
                if len(res[word - 1]) - 1 == len(res[word]) or  len(res[word - 1]) + 1 == len(res[word]) or len(res[word - 1]) == len(res[word]):
                    continue
                else:
                    flag = False
                    total.append('not a family')
                    break
            if flag == True:
                for i in range(1, len(res)):
                    counter = 0
                    for j in res[i]:
                        if res[i].count(j) != res[i-1].count(j):
                            counter += 1
                        else:
                            continue
                    if counter != 1:
                        total.append('not a family')
                        break
                else:
                    total.append('family')
            else:
                continue
    return total
          


result = family(t)
for res in result:
    print(res)

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