Source: https://yellorn.com/programming/regex-find-yellorn
At Yellorn, we always want to find out how popular we are getting every day and have scraped conversations from popular sites. Each conversation fits in 1 line and there are N such conversations. Each conversation has at most 1 word that says yellorn (all in lowercase). We would like you to help us figure out whether a conversation:
- Starts with yellorn
- Ends with yellorn
- Start and ends with yellorn
Input Format
First line of the input contains an integer, N. Then N lines follow.
From the second line onwards, each line contains a set of W words separated by a single space
Constraints
- 1 <= N <= 10
- 1 <= W <= 100
- All the characters in W are lowercase alphabet characters.
- If C is the count of the characters in W, then 1 <= C <= 20
Output Format
For every line,
- Print 1 if the conversation starts with yellorn
- Print 2 if the conversation ends with yellorn
- Print 0 if the conversation starts and ends with yellorn
- Print -1 if none of the above.
Explanation
The first conversation ends with yellorn and hence 2
The second conversation starts with yellorn and hence 1
The third conversation has only one word, it starts and ends with yellorn and hence 0.
The fourth conversation satisfies none of the above properties and hence -1.
Testcase
Input
4
i love hackerrank
hackerrank is an awesome place for programmers
hackerrank
i think hackerrank is a great place to hangout
Output
2
1
0
-1
Solution
import sys
import re
first_pattern = r"^yellorn"
last_pattern = r"yellorn$"
for i in range(int(input())):
text = sys.stdin.readline()
first = re.search(first_pattern, text)
last = re.search(last_pattern, text)
result = -1
if first and last:
result = 0
elif first:
result = 1
elif last:
result = 2
print(result)
Comments
Post a Comment