This is the analyses of a CodeChef’s beginner level question. Question is stated below for the reference.
And can also be accessed from the link present below.
- We have to input a variable, say T, as in question, which denotes the number of times we’ll check the result. So we have to iterate the main code conditions T times. A for or a while loop is required.
- Then we have to input 6 variables, say A1 A2 A3 C1 C2 C3, as stated in question, where A1, A2 and A3 denotes the age of the three children and C1, C2 and C3 denotes the units of money given to them respectively. Let us focus now on how to take input.Below is the form in which the user will input numbers. A1(space)A2(space)A3(space)C1(space)C2(space)C3
- Now we have to check the numbers input. So the basic logic is that the older child should get more units of money and if ages are equal then amount of money should also be equal. We have to check if this criteria is fulfilled, then we to print ‘FAIR’, else, we have to print ‘NOT FAIR’.
FUNCTION main T = INPUT int FOR each iteration till T( A1,A2,A3,C1,C2,C3 = INPUT integers IF (comp(A1,A2) == comp(C1, C2) and comp(A2,A3) == comp(C2,C3) and comp(A2,A3)==comp(C2,C3)) THEN print 'FAIR' ELSE print 'NOT FAIR' FUNCTION comp arguments (a,b) IF a>b THEN return 1 ELSE IF a<b THEN return -1 ELSE return 0
- Input T.
- Iterate the code below T times using for/while loop.
- Inside the above loop, compare all pair of numbers like compare(A1, A2), and compare(C1,C2) and return some result for all possibilities (greater, smaller, equal) .
- If both compare conditions above return same result then good to go, else the distribution is not according to the criteria, print ‘NOT FAIR’.
- Similarly check for other two pairs (A1,A3) and (C1,C3), (A2,A3) and (C2,C3). If all three pairs return same individual result then print ‘FAIR’
Python Code (v2.7)
def comp(a1,a2): if a1>a2: return 1 elif a1<a2: return -1 else: return 0 for i in range(int(input())): a1,a2,a3,b1,b2,b3 = map(int,raw_input().rstrip().split()) if comp(a1,a2)==comp(b1,b2) and comp(a1,a3)==comp(b1,b3) and comp(a2,a3)==comp(b2,b3) : print "FAIR" else : print 'NOT FAIR'