Our Feeds

Wednesday, 5 November 2014

Ajith KP

Digital Camera Day or Night - Python Solution

          "You need to construct a feature in a Digital Camera, which will auto-detect and suggest to the photographer whether the picture should be clicked in day or night mode, depending on whether the picture is being clicked in the daytime or at night. You only need to implement this feature for cases which are directly distringuishable to the eyes (and not fuzzy scenarios such as dawn, dusk, sunrise, sunset, overcast skies which might require more complex aperture adjustments on the camera)."



          The above question is find here: https://www.hackerrank.com/contests/image-analysis-1/challenges/digital-camera-day-or-night

          The solution to this problem in Python



# (C) http://www.terminalcoders.blogspot.in (C)
# Originally scripted by Ajith Kp
from math import *
tmp = raw_input().strip(',\n').split(' ')
sb = 0
sg = 0
sr = 0
for i in tmp:
    i = i.split(',')
    for j in range(len(i)):
        try:
            if j%3==0:
                sb+=int(i[j])
            elif j%3==1:
                sg+=int(i[j])
            else:
                sr+=int(i[j])
        except Exception:
            pass
sr = (sr/len(tmp))/255.0
sg = (sg/len(tmp))/255.0
sb = (sg/len(tmp))/255.0
R = 0
G = 0
B = 0
if sr <= 0.03928:
    R = sr/12.92
else:
    R = pow((sr+0.055)/1.055, 2.4)
if sg <= 0.03928:
    G = sg/12.92
else:
    G = pow((sg+0.055)/1.055, 2.4)
if sb <= 0.03928:
    B = sb/12.92
else:
    G = pow((sb+0.055)/1.055, 2.4)
y = 0.2126*R+0.7152*G+0.0722*B
if y<0.07:
    print 'night'
else:
    print 'day'