I wrote a program to solve a problem, I am not so good in python so want help to find areas where this program can be improved or simplified and readable too:-
I got this problem in a code challenge
Problem
Professor Hatim is researching the sexual behavior of a rare species of lizard. He assumes that they feature two different genders and that they only interact with lizard of the opposite gender. Each lizard has a integer printed on their back.
Given the details of lizard interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs, or if it contains some bug interactions that falsify it. Or simply, you have to print "Suspicious lizard found!" (without quotes), if an interaction of two lizards of same gender is found in the list of interactions, otherwise print "No suspicious lizard found!" (without quotes).
Input format
The first line contains an integer T, denoting the number of test cases.
The first line of each test case contains two integers N and M, where N denotes the number of lizards and M denotes the number of interactions.
Next M lines of each test case contains, two space separated integers, say x and y , denoting the interaction between lizard no. x and lizard no. y.
Note: Integers on the back of lizards are numbered from 1 to N.
Output format
For each test case ,print a line, saying either “No suspicious lizard found!” if the experiment is consistent with his assumption about the lizard sexual behavior, or “Suspicious lizard found!” if Professor Hatims’s assumption is definitely wrong.
Constraints
- 1 ≤ T ≤ 5
- 2 ≤ N ≤ 2000
- 1 ≤ M ≤ 105
Sample Input
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
Sample Output
Suspicious lizards found! No suspicious lizards found!
Time Limit: 1 sec(s) for each input file.
Memory Limit: 256 MB Source
Limit: 1024 KB
Marking Scheme: Marks are awarded if any testcase passes.
Allowed languages: PYTHON
CODE
from sys import argv
class Test:
def __init__(self, file):
self.file = file
def run_test_case(self):
n, m = self.file.readline().split()
n = int(n)
m = int(m)
first_gender = []
second_gender = []
for i in range(m):
x, y = 0, 0
x, y = self.file.readline().split()
if first_gender == [] and second_gender == []:
first_gender[ len(first_gender) : ] = x
second_gender[ len(second_gender) : ] = y
elif (x in first_gender and y in first_gender) or (x in second_gender and y in second_gender):
print 'Suspicious lizard found!'
return
elif (x in first_gender and y not in second_gender):
second_gender[ len(second_gender) : ] = y
elif (x in second_gender and y not in first_gender):
first_gender[ len(first_gender) : ] = y
elif (y in first_gender and x not in second_gender):
second_gender[ len(second_gender) : ] = x
elif (y in second_gender and x not in first_gender):
first_gender[ len(first_gender) : ] = x
print 'No suspicious lizard found!'
def start(filename):
file = open(filename, 'r')
num_test_cases = int(file.readline())
for i in range(num_test_cases):
test_instance = Test(file)
test_instance.run_test_case()
if __name__ == '__main__':
(script, filename) = argv
start(filename)