Wrote a python script that reads from a file input.txt
input.txt
2 //number of test cases
2 4 //testcase 1 ->'2' is size of 1st array and 4 is size of 2nd array
6 10 6 7 8 9 //testcase 1 -> 1st array is [6,10] and 2nd array is [6,7,8,9]
1 3 //testcase 2 ->'1' is size of 1st array and 3 is size of 2nd array
7 7 8 14 //testcase 2 -> 1st array is [7] and 2nd array is [7,8,14]
The 1st line in the file indicates number of test cases. In this example, we have 2 test cases. Each test case have 2 lines to process - in which first line indicates size of 1st array and size of 2nd array. 2nd line indicates the both array details.
ie, In above example, line 2 indicates size of 1st array and 2nd array for testcase1. line 3 indicates 2 arrays in mentioned sizes for testcase1.line 4 indicates size of 1st array and 2nd array for testcase2. line 5 indicates 2 arrays in mentioned sizes for testcase2.
I need to check whether the elements of 1st array is present in the 2nd one for each test cases. I wrote below program, but that will execute only for 1 test case(ie, I'm checking 2nd and 3rd line manually by giving the check i == 0)
from itertools import islice
def search(arr, element):
for i in range(len(arr)):
if int(arr[i]) == int(element):
return "yes"
return "no"
f = open("output.txt", "w")
with open("input.txt") as y_file:
count = y_file.readline()
if(count > 0):
for i, line in enumerate(y_file):
if(i == 0):
num, size = line.split()
split_list = [int(num), int(size)]
if(i == 1):
temp = iter(line.split())
res = [list(islice(temp, 0, ele)) for ele in split_list]
for i in range(len(res[0])):
result = search(res[1], res[0][i])
f.write("The result is : " + str(result) + "\n")
f.close()
Can anyone please help me in this?
output will be like
The result is : yes
The result is : no
The result is : yes