From this hackerrank:
Problem Statement
The Head Librarian at a library wants you to make a program that calculates the fine for returning the book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:
- If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.
- If the book is returned in the same calendar month as the expected return date, Fine = 15 Hackos × Number of late days
- If the book is not returned in the same calendar month but in the same calendar year as the expected return date, Fine = 500 Hackos × Number of late months
- If the book is not returned in the same calendar year, the fine is fixed at 10000 Hackos. Input
You are given the actual and the expected return dates in D M Y format in two separate lines. The first line contains the D M Y values for the actual return date and the next line contains the D M Y values for the expected return date.
My development philosophy was to TDD this. First write code that reads the date correctly, then start implementing the test cases from the problem statement.
I did not put anything in a separate class.
My code passed all their test cases but looks ugly as heck. Any suggestions for improvement would be appreciated!
import datetime
def testFine(dueDate, returnDate, actualFine):
fine = getFine(dueDate, returnDate)
if (fine != actualFine):
print( "Test FAILED for duedate %s and returnDate %s with fine: %d but expected:%d." %( dueDate, returnDate, fine, actualFine) )
else:
print( "Test passed for duedate %s and returnDate %s with fine: %d." % ( dueDate, returnDate, fine) )
def tests():
#If the book is not returned in the same calendar year, the fine is fixed at 10000 Hackos.
testFine(datetime.date(2015,5,5), datetime.date(2016,5,6),10000)
#If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.
testFine(datetime.date(2015,5,5), datetime.date(2015,5,5),0)
testFine(datetime.date(2015,5,5), datetime.date(2015,5,3),0)
#If the book is returned in the same calendar month as the expected return date, Fine = 15 Hackos × Number of late days
testFine(datetime.date(2015,5,5), datetime.date(2015,5,6),15)
testFine(datetime.date(2015,2,5), datetime.date(2015,2,10),75)
#If the book is not returned in the same calendar month but in the same calendar year as the expected return date, Fine = 500 Hackos × Number of late months
testFine(datetime.date(2015,2,5), datetime.date(2015,3,1),500)
testFine(datetime.date(2015,2,5), datetime.date(2015,6,1),2000)
def getFine(dueDate, returnDate):
if (returnDate.year > dueDate.year):
return 10000
if (returnDate.year == dueDate.year and returnDate.month == dueDate.month and returnDate.day > dueDate.day):
return 15*(returnDate.day-dueDate.day)
if (returnDate.year == dueDate.year and returnDate.month > dueDate.month):
return 500*(returnDate.month-dueDate.month)
return 0
def getDateFromCin():
s = input()
nums = [int(n) for n in s.split(" ")]
return datetime.date(nums[2], nums[1],nums[0])
def testFunc():
testline = input()
print (testline + "test")
return
def cinMain():
returnDate = getDateFromCin()
dueDate = getDateFromCin()
fine = getFine(dueDate, returnDate)
print (fine)
def main():
tests()
cinMain()