From fe0228367d35a4d47329a25237c2e8e9c10be540 Mon Sep 17 00:00:00 2001 From: NajibAdan Date: Tue, 22 May 2018 22:22:51 +0300 Subject: [PATCH] first commit --- Codes/logger.py | 24 ++++++++++++++++++++++ Codes/pipe.py | 9 +++++++++ Codes/viewer.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 Codes/logger.py create mode 100644 Codes/pipe.py create mode 100644 Codes/viewer.py diff --git a/Codes/logger.py b/Codes/logger.py new file mode 100644 index 0000000..ad18fad --- /dev/null +++ b/Codes/logger.py @@ -0,0 +1,24 @@ +''' +Infra Internship Code Challenge + +Task 1 + +This script writes 1000 lines of text to standard output. Each line of text is a date in the form dd/MM/YYYY +''' +import random +import sys + + +def randomize(): + day = str(random.randint(1, 30)) + month = str(random.randint(1, 12)) + year = random.randint(17, 18) + return day.zfill(2) + '/' + month.zfill(2) + '/20' + str(year) + '\n' + + +def output(): + for x in range(0, 1000): + print randomize() + + +output() diff --git a/Codes/pipe.py b/Codes/pipe.py new file mode 100644 index 0000000..3c8d9af --- /dev/null +++ b/Codes/pipe.py @@ -0,0 +1,9 @@ +''' +Infra Internship Code Challenge + +Task 3 + +This script launches the logger program and pipes its output to the viewer program. +''' +import os +os.system("python logger.py|python viewer.py") diff --git a/Codes/viewer.py b/Codes/viewer.py new file mode 100644 index 0000000..f6466a1 --- /dev/null +++ b/Codes/viewer.py @@ -0,0 +1,54 @@ +''' +Infra Internship Code Challenge + +Task 2 + +This program reads lines of text from the standard input,counts how many times a month appears and displays dates from 0-14th of 2018 +''' +import sys +month_count = [0] * 12 + + +def format(date): + #Displays Malformed Input whenever the input isn't in the format DD/MM/YYYY + if int(date[:2]) > 0 and int(date[:2]) < 31: + if int(date[3:5]) > 0 and int(date[3:5]) < 13: + if (len(date[6:-1]) == 4): + return 0 + else: + print 'Malformed Input' + + +def read(date): + #increments the month_count for month X whenever it appears + month = int(date[3:5]) - 1 + month_count[month] = month_count[month] + 1 + + +def count(): + #prints the logged months with the percentage that month appears + month = [ + 'January', 'February', 'March', 'April', 'May', 'June', 'July', + 'August', 'September', 'October', 'November', 'December' + ] + print "_________________________________________" + for x in month: + perc = month_count[month.index(x)] / 10.0 + print x + ': ' + str(perc) + '%' + print "_________________________________________" + + +def fourteenth(date): + #Print lines from 0-14th of 2018 + if int(date[:2]) > 0 and int(date[:2]) < 14: + if int(date[-3:-1]) == 18: + print date[:-1] + + +for date in sys.stdin: + if date.strip(): + format(date) + read(date) + fourteenth(date) + +count()