Python Good and Bad Persons program Codechef solution
Python's Good and Bad Persons programme Codechef solution
Problem
Chef is a really nice and respectful person, in sharp contrast to his little brother, who is a very nasty and disrespectful person. Chef always sends messages to his friends in all small letters, whereas the little brother sends messages in all capital letters.
You just received a message given by a string. You don't know whether this message was sent by Chef or his brother. Also, the communication channel through which you received the message is erroneous, and hence you can flip a letter from uppercase to lowercase or vice versa. However, you know that this channel can make at most K such flips.
Determine whether the message could have been sent only by Chef, only by the little brother, by both, or by none.
Input
- The first line of the input contains a single integer, T, denoting the number of test cases. The description of T-test cases follows.
- The first line of each test case contains two space-separated integers N and K denoting the length of the string s and the maximum number of flips that the erroneous channel can make.
- The second line contains a single string denoting the message you received.
Output
For each test case, output a single line containing one string: "chef", "brother", "both" or "none".
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 100
- 0 ≤ K ≤ N
- s consists only of lowercase and uppercase English letters.
Input
4
5 1
frauD
5 1
FRAUD
4 4
Life
10 4
sTRAWBerry
Output
chef
brother
both
none
Explanation
Example 1: Only one flip is possible. So it is possible that Chef sent "fraud" and the channel flipped the last character to get "frauD.". However, it is not possible for the brother to have sent "FRAUD", because then it would need 4 flips. Hence the answer is "chef".
Example Case 2: Only one flip is possible. So it is possible that the brother sent "FRAUD" and the channel didn't flip anything. However, it is not possible for Chef to have sent "fraud", because then it would need 5 flips. Hence the answer is "brother".
Example Case 3: Four flips are allowed. It is possible that Chef sent "life" and the channel flipped the first character to get "Life". It is also possible that the brother sent "Life" and the channel flipped the last three characters to get "Life". Hence the answer is "both".
Example Case 4: Four flips are allowed. It is not possible that Chef sent "strawberry", because it would need five flips to get "strawberry.". It is also not possible that the brother sent "STRAWBERRY", because that would also need five flips. Hence the answer is "none".
Solution:
try: for i in range(int(input(“Enter the number of terms: “))): n,k=map(int, input(“Enter the length of string and the number of flips: “).split()) s=input(“Enter the message: “) l=[] for i in s: l.append(ord(i)) c1,s1=0,0 for j in l: if j<=90 and j>=65: c1+=1 elif j>=95 and j<=122: s1+=1 if s1<=k and c1>k: print("brother") elif c1<=k and s1>k: print("chef") elif s1<=k and c1<=k: print("both") else: print("none") except: pass |
Steps to solve this problem:
- In the try block, ask the user to enter a number of terms.
- In the loop, ask the user to enter the length of the string and the number of flips, and using the map() function, get iterator objects and store them in variables n and k.
- Ask the user to enter the message and store it in
- Create an empty list l and append the unicode values of each letter of the string s to a list l.
- Assign variables c1, s1 with 0.
- Traverse the list using a for loop and check the condition: if the unicode values of the letters of the string are greater than or equal to 65 and less than or equal to 90, then increase the value of s1 by 1.
- Check if the unicode values of the letters of the string are greater than or equal to 95 and less than or equal to 122, then increase the value of c1 by 1.
- Now check if s1 is less than equal to k and c1 is greater than k, then print brother."
- Now check if c1 is less than equal to k and s1 is greater than k, then print chef."
- Now check if s1 is less than equal to k and c1 is less than or equal to k, then print both."
- If all conditions above return false, then print none."
- In the except block, we just left it empty, so we used the pass statement in it.