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
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')
Comments
Leave a comment