In [5]:
#! /usr/bin/python

In [1]:
def isnice( line ):
#    print(line)

#   It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
    twopair = False
    for i in range(0, len(line)-3):
#        print(line[i]+line[i+1])
        for j in range(i+2, len(line)-1):
#            print(line[j]+line[j+1])
            if line[i]+line[i+1] == line[j]+line[j+1]:
                twopair = True
                
#   It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
    oneletter = False
    for i in range(0, len(line)-2):
        if line[i] == line[i+2]:
            oneletter = True

#    print(twopair)
#    print(oneletter)
#    print(twopair and oneletter)
    return twopair and oneletter

In [2]:
filename = './input'
naughty = 0
nice = 0

with open(filename) as f:
    for linen in f:
        line = linen.rstrip('\n')
        if isnice(line):                 #      print("%s is nice" % line)
            nice += 1
        else:                            #      print("%s is naughty" % line)
            naughty += 1                 #    print("--")

In [5]:
print ("naughty strings: %d" % naughty)
print ("nice strings: %d" % nice)


naughty strings: 945
nice strings: 55

In [ ]: