Brain Teaser

T : Number of test cases

M: value of M

N: Number of strings

For each string, ascii value of each character of the string is raised to M and multiplied together. For each test case the sum of above values  is ODD or EVEN

Input:

1

10    2

ac ab

Output:

ODD

 

TIP: Challenge lies in finding the solution without doing all the mathematical operations mentioned in the question. (yes, also called optimizing..!)

 

 

Solution:

#!/usr/bin/python

T = int(raw_input())
while T>0:
    T = T - 1
    tmp = raw_input()
    M = int(tmp.split()[0])
    K = int(tmp.split()[1])
    tmp = raw_input()
    final_odd = False
    for s in tmp.split():
        odd = None
        for i in s:
            if ord(i)%2==0:        # the power is even
                odd = False
            else:                # the power is odd
                if odd==None:            # if odd is True, odd*odd=odd
                    odd = True
        # odd + odd = even
        # odd + even = odd
        # even + even = even
        if final_odd != odd:
            final_odd = True
        else:
            final_odd = False
    if final_odd:
        print "ODD"
    else:
        print "EVEN"

Published by

Sreejith

A strong believer of: 1. Knowledge is power 2. Progress comes from proper application of knowledge 3. Reverent attains wisdom 4. For one's own salvation, and for the welfare of the world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s