# Given two strings. Write a function that will return true if one string is a rotation of the other string.
# e.g. 'bca' and 'cab' are rotations of 'abc' and the function should return true
# 'barbazfoo', 'oobarbazf' and 'rbazfooba' are rotations of 'foobarbaz'
# string1 = "barbazfoo"
# string2 = "foobarbaz"
#
#

def is_rotation(string1, string2):
    if len(string1) == 1 and len(string2) == 1 and string1[0] == string2[0]:
        return True
    if len(string1) != len(string2):
        return False
    #lookup = set()
    for i in xrange(len(string1)):
        if string2 == rotate(string1, i):
            return True
    return False
        #lookup.add(rotate(string1, i))
    #if string2 not in lookup:
    #    return False
    #else:
    #    return True


def rotate(str, n):
    return str[-n:] + str[:-n]


# Write a function that will return an array of integers that occur exactly once in a given array of integers.

# Example: input list [-2,-2,1,-2,0,1,5,3], output list [0, 5, 3]

# Follow up: what if the input list is sorted (ascending)? input [-2,-2,-2,0,1,1,3,5]

def findUniqueSorted(array):
    if len(array) <= 1:
        return list
    seen = set()
    dup = set()
    for j in array:
        if j not in seen and j not in dup:
            seen.add(j)
        else:
            dup.add(j)
            if seen: seen.remove(j)
    print list(seen)


from collections import defaultdict


def findUnique(list):
    if len(list) < 1:
        print "List doesn't exist"
        return False
    elif len(list) == 1:
        return list
    else:
        result = []
        dict = defaultdict(int)
        for i in list:
            if i in dict:
                dict[i] += 1
            else:
                dict[i] = 1
        for key, value in dict.iteritems():
            if value == 1:
                result.append(key)
    return result

# Write a program which prints out all numbers between 1 and 100.
# When the program would print out a number exactly divisible by 4, print "Linked" instead.
# When it would print out a number exactly divisible by 6, print "In" instead.
# When it would print out a number exactly divisible by both 4 and 6, print "LinkedIn."

def print_Linked_In():
    result = []
    for i in xrange(1,101):
        if i % 4 == 0:
            result.append("Linked")
        elif i % 6 == 0:
            result.append("In")
        elif (i % 4 == 0 and i % 6 == 0):
            result.append("LinkedIn")
        else:
            result.append(i)
    print result


# ---------- begin sample log extract ----------
# Jan 20 03:25:08 fakehost logrotate: ALERT exited abnormally with [1]
# Jan 20 03:25:09 fakehost run-parts(/etc/cron.daily)[20447]: finished logrotate
# Jan 20 03:26:21 fakehost anacron[28969]: Job 'cron.daily' terminated
# Jan 20 03:26:22 fakehost anacron[28969]: Normal exit (1 job run)
# Jan 20 03:30:01 fakehost CROND[31462]: (root) CMD (/usr/lib64/sa/sa1 1 1)
# Jan 20 03:30:01 fakehost CROND[31461]: (root) CMD (/var/system/bin/sys-cmd -F > /dev/null 2>&1)
# Jan 20 05:03:03 fakehost ntpd[3705]: synchronized to time.faux.biz, stratum 2
# Jan 20 05:20:01 fakehost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="20438" x-info="http://www.rsyslog.com"] start
# Jan 20 05:22:04 fakehost cs3[31163]:  Q: ".../bin/rsync -LD ": symlink has no referent: "/var/syscmds/fakehost/runit_scripts/etc/runit/service/superImportantService/run"#012Q: ".../bin/rsync -LD ": rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
# Jan 20 05:22:04 fakehost cs3[31163]:  I: Last 2 quoted lines were generated by "/usr/local/bin/rsync -LD --recursive --delete --password-file=/var/syscmds/modules/rsync_password /var/syscmds/fakehost syscmds@fakehost::syscmds_rsync"
# Jan 20 05:22:08 fakehost cs3[31163]:  Q: ".../sbin/sv restart": ok: run: /export/service/cool-service: (pid 32323) 0s
# Jan 20 05:22:08 fakehost cs3[31163]:  I: Last 1 quoted lines were generated by "/sbin/sv restart /export/service/cool-service"
# Jan 20 05:22:09 fakehost cs3[31163]:  R: cs3:  The cool service on fakehost does not appear to be communicating with the cool service leader.  Automating a restart of the cool service in attempt to resolve the communication problem.
# Jan 20 05:22:37 fakehost ACCT_ADD: WARNING: Manifest /var/syscmds/inputs/config-general/doit.txt has been processed already, bailing
# ---------- end sample log extract ----------
#
# Write a script which parses /var/log/messages and generates a CSV with two columns: minute, number_of_messages
#
# ---------- begin sample output ----------
# minute, number_of_messages
# Jan 20 03:25,2
# Jan 20 03:26,2
# Jan 20 03:30,2
# Jan 20 05:03,1
# Jan 20 05:20,1
# Jan 20 05:22,6
import os
from collections import defaultdict, OrderedDict
def generateCSV():
    counter_dict = defaultdict(lambda: defaultdict(int))
    cols = ['minute', 'total_count']
    if os.path.exists("log.txt"):
        with open("log.txt", 'r') as FP:
            for line in FP.readlines():
                dat = line.split("fakehost")[0].split(':')
                column_info = line.split("fakehost")[1].split(':')[0]
                new_column_info = column_info.split(r"[")[0].strip()
                counter_dict[dat[0] + ':' + dat[1]]['total_count'] += 1
                if new_column_info in counter_dict[dat[0] + ':' + dat[1]]:
                    counter_dict[dat[0] + ':' + dat[1]][new_column_info] += 1
                else:
                    cols.append(new_column_info)
                    counter_dict[dat[0] + ':' + dat[1]][new_column_info] = 1

    od = OrderedDict(sorted(counter_dict.items()))

    print " ".join(i for i in cols)
    for k , v in od.iteritems():
        for i in cols:
            print k, ',' + str(v[i])


    #for k, v in od.iteritems():
    #   print k  + ',' + str(v)

#print_Linked_In()
generateCSV()
#findUniqueSorted([-2,-2,-2,0,1,1,3,5])
#print findUnique([-2, -2, 1, -2, 0, 1, 5, 3])
string1 = "barbazfoo"
string2 = "foobarbaz"
print is_rotation(string1, string2)

