Skip to content

Commit

Permalink
Merge pull request #9 from carmelolg/feature_3
Browse files Browse the repository at this point in the history
statistics added
  • Loading branch information
carmelolg committed Feb 5, 2020
2 parents c390a49 + 36a1324 commit 568adaf
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ __pycache__/
*.txt
*.py~
*~
*.xml
*.iml

# C extensions
*.so
Expand Down
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class Constants(object):
longMonths = ['01', '03', '05', '07', '08', '10', '12']
normalMonths = ['11','04','06', '09']
shortMonths = ['02']
startYear = 0
endYear = 10000
numMonths = 12
numDaysShortMonth = 28
numDaysNormalMonth = 30
Expand Down
9 changes: 8 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@
i18nCalculation = calculate.getPalindromeDate(i18nDates)
commonCalculation = calculate.getPalindromeDateInCommon(itCalculation['dates'], i18nCalculation['dates'])

itNextDateResult = calculate.getNextPalindromeDate(itCalculation['dates'], itDate.formatDate())
itLastDateResult = calculate.getLastPalindromeDate(itCalculation['dates'], itDate.formatDate())
i18nNextDateResult = calculate.getNextPalindromeDate(i18nCalculation['dates'], i18nDate.formatDate())
i18nLastDateResult = calculate.getLastPalindromeDate(i18nCalculation['dates'], i18nDate.formatDate())

# Step 4 - Console output

justPrint.title()
justPrint.header()
justPrint.stats('Italian standard in statistics', itNextDateResult['distance'], itNextDateResult['date'], itLastDateResult['distance'], itLastDateResult['date'])
justPrint.stats('International standard in statistics', i18nNextDateResult['distance'], i18nNextDateResult['date'], i18nLastDateResult['distance'], i18nLastDateResult['date'])
justPrint.dates('International and italian intersection', commonCalculation['count'], commonCalculation['dates'])
justPrint.dates('Italian standard', itCalculation['count'], itCalculation['dates'])
justPrint.dates('International standard', i18nCalculation['count'], i18nCalculation['dates'])
justPrint.dates('International and italian intersection', commonCalculation['count'], commonCalculation['dates'])
justPrint.footer()

37 changes: 35 additions & 2 deletions service/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from service.palindrome import Palindrome
from service.date.dateUtils import DateUtil
import sys
import datetime

class Calculate(object):
palindromeUtils = Palindrome()
Expand All @@ -28,9 +30,40 @@ def getPalindromeDateInCommon(self, itPalindromeDate, i18nPalindromeDate):
for i18nDate in i18nPalindromeDate:
if self.utils.equalsBetweenITAndI18NDate(itDate, i18nDate):
count += 1
palindromeDate.append(itDate + '|' + i18nDate)
palindromeDate.append(itDate + ' | ' + i18nDate)

result['count'] = count
result['dates'] = palindromeDate
return result


def getNextPalindromeDate(self, list, format):
result = dict()
nextDate = None
min = sys.maxsize
today = datetime.datetime.now()
for date in list:
currentDate = datetime.datetime.strptime(date, format)
differenceDay = self.utils.daysBetweebTwoDates(today, currentDate)
if min > differenceDay and currentDate > today:
min = differenceDay
nextDate = date

result['date'] = nextDate
result['distance'] = str(min)
return result

def getLastPalindromeDate(self, list, format):
result = dict()
lastDate = None
min = sys.maxsize
today = datetime.datetime.now()
for date in list:
currentDate = datetime.datetime.strptime(date, format)
differenceDay = self.utils.daysBetweebTwoDates(today, currentDate)
if min > differenceDay and currentDate < today:
min = differenceDay
lastDate = date

result['date'] = lastDate
result['distance'] = str(min)
return result
4 changes: 4 additions & 0 deletions service/date/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class AbstractDate(ABC):
@abstractmethod
def compileDate(self, year, month, day):
pass

@abstractmethod
def formatDate(self):
pass

def _init(self):
for x in range(self.const.startYear, self.const.endYear + 1):
Expand Down
9 changes: 9 additions & 0 deletions service/date/dateUtils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/python3

import datetime

class DateUtil(object):
def equalsBetweenITAndI18NDate(self, itDate, i18nDate):
itYear = itDate[6:]
Expand All @@ -17,3 +19,10 @@ def equalsBetweenITAndI18NDate(self, itDate, i18nDate):
def isYearValid(self, year):
return True if int(year[:2][::-1]) <= 12 and int(year[:2][::-1]) > 0 and int(year[2:][::-1]) > 0 and int(year[2:][::-1]) <= 31 else False

def daysBetweenTwoDatesInString(self, d1, d2, format):
d1 = datetime.strptime(d1, format)
d2 = datetime.strptime(d2, format)
return abs((d2 - d1).days)

def daysBetweebTwoDates(self, d1, d2):
return abs((d2 - d1).days)
2 changes: 2 additions & 0 deletions service/date/i18nDate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
class I18NDate(AbstractDate):
def compileDate(self, year, month, day):
return year + '-' + month + '-' + day
def formatDate(self):
return '%Y-%m-%d'
2 changes: 2 additions & 0 deletions service/date/itDate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
class ITDate(AbstractDate):
def compileDate(self, year, month, day):
return day + '-' + month + '-' + year
def formatDate(self):
return '%d-%m-%Y'

10 changes: 9 additions & 1 deletion service/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def pad(self, string):
return "{:<50}".format(string)

def field(self, field):
print("- ", self.pad(field), " -")
print("- ", self.pad(field), " -")

def divisor(self):
print('--------------------------------------------------------')
Expand Down Expand Up @@ -43,3 +43,11 @@ def dates(self, title, count, fields):
self.space()
self.divisor()

def stats(self, title, nextCount, nextField, lastCount, lastField):
self.space()
self.field(title)
self.space()
self.field('Next: ' + nextField + ' in ' + str(nextCount) + ' days')
self.field('Last: ' + lastField + ' ' + str(lastCount) + ' days ago')
self.space()
self.divisor()

0 comments on commit 568adaf

Please sign in to comment.