String Lookup
Ram challenges Anil to a game, He decides to give a target word W to Anil.The rules of the game are very simple.Ram gives Anil N sentences one by one and asks Anil to print the first sentence which contains the given word W.If the sentence numbering starts from 1, can you identify the first sentence number containing the word W?
Note: Consider lower and upper case letters as different.
Input
The first line of input is the word W.
The second line of input is a positive integer N.
The next N lines of input contain a string in each line.
Output
The output should be a single line containing an integer.
If the given word is not in any line of input strings, print -1.
print('Enter word W: ', end='')
W = input()
print('Enter positive integer N: ', end='')
N = int(input())
numStr = -1
print('Enter strings: ')
for i in range(N):
S = input()
for j in S:
if not j.isalpha():
S = S.replace(j, ' ')
Srez = S.split()
if numStr == -1:
for j in Srez:
if (j == W):
numStr = i + 1
if numStr == -1:
print('-1')
else:
print(numStr)
Comments
Leave a comment