#! /usr/bin/env python2.5
# Penalty
#
# Computes the penalty for one homework based on a predefined value and a 
# wheight list
#
# Usage:
#   ./penalty.py upload_time deadline
#
# Input:
#   upload_time = time of upload (string)
#   deadline = assignment deadline (string)
#       date format: "dd-mm-yy hh:mm:ss"
# Output:
#   penalty_points = number of points to be substracted from the grade
#   days_late = number of days exceeding the deadline


__author__='Ana Savu, ana.savu86@gmail.com'


import sys
import time
import math


def parse_time(upload_time_str, deadline_str):

    # parse input strngs according to format

    upload_time = time.strptime(upload_time_str, "%d-%m-%y %H:%M:%S")
    deadline = time.strptime(deadline_str, "%d-%m-%y %H:%M:%S")

    return (upload_time, deadline)


def compute_penalty(upload_time, deadline, penalty, wheights, limit):

# penalty - for every day after the deadline the product of the penalty and 
#   the wheight is added to the penalty_points variable
# wheights - the penalty's wheight per day (the last wheight from the list 
# is used for subsequent computations)
# limit - the limit for the penalty value

    # time interval between deadline and upload time (seconds)
    interval = time.mktime(upload_time) - time.mktime(deadline)

    penalty_points = 0

    # only if the number of days late is positive (deadline exceeded)
    if interval > 0:
        days_late = int(math.ceil(interval / (3600 * 24)))
        
        for i in range(days_late):

            # the penalty exceeded the limit
            if penalty_points > limit:
                break
            else:
                # for every day late the specific wheight is used
                wheight = wheights[min(i, len(wheights) - 1)]
                penalty_points += wheight * penalty
    else:
        days_late = 0

    return (min(penalty_points, limit), days_late)


def compute_penalty_linear(upload_time, deadline):
    
    # for every day pass the deafline the penalty value is added
    # to the penalty_points
    return compute_penalty(upload_time, deadline, 0.25, [1], 3)


def compute_penalty_fixed_deadline(upload_time, deadline):

    # if the number of days pass the deadline exceeds 'x' the homework is
    # not graded (x = len(wheights) - 1)
    return compute_penalty(upload_time, deadline, 1, [1, 1, 1, 7], 10)


def compute_penalty_wheighted(upload_time, deadline):

    # the wheight for penalty is diferent depending on the day
    return compute_penalty(upload_time, deadline, 1, [1, 2, 3, 4, 0], 10)


def main():
   
    if len(sys.argv) != 3:
        print >> sys.stderr, 'Usage: %s upload_time deadline' % sys.argv[0]
        sys.exit(1)
    
    upload_time_str = sys.argv[1]
    deadline_str = sys.argv[2]

    (upload_time, deadline) = parse_time(upload_time_str, deadline_str)

    # based on the type of penalty computing used call one of 
    # the functions below
    (penalty_points, days_late) = compute_penalty_linear(upload_time, deadline)
    # (penalty_points, days_late) = compute_penalty_fixed_deadline(upload_time, deadline)
    # (penalty_points, days_late) = compute_penalty_wheighted(upload_time, deadline)
    # (penalty_points, days_late) = compute_penalty(upload_time, deadline, my_penalty, 
    #         my_wheights, my_limit)

    
    #if penalty_points > 0:
    print "-%.2f: %d zile intarziere" % (penalty_points, days_late)    

if __name__ == '__main__':
    main()

@@ -2545,24 +2545,920 @@
 4, 0%5D, 10)%0A%0A
+def time_late(upload_time, deadline):%0A    interval = time.mktime(upload_time) - time.mktime(deadline)%0A    penalty_points = 0%0A%0A    # only if the number of days late is positive (deadline exceeded)%0A    if interval %3E 0:%0A        days_late = int(math.floor(interval / (3600 * 24)))%0A        interval -= days_late * (3600 * 24)%0A        hour_late = int(math.floor((interval / 3600)))%0A        interval -= hour_late * 3600%0A        mins_late = int(math.floor((interval / 60)))%0A        secs_late = interval - mins_late * 60%0A        return %22 %25d zile %25d ore %25d minute %25d secunde%22 %25 (days_late, %0A                                                         hour_late,%0A                                                         mins_late,%0A                                                         secs_late)%0A    else:%0A        days_late = 0%0A        return %22 tema trimisa inainte de expirarea deadline-ului%22%0A        %0A    
 %0Adef main():
@@ -4255,25 +4255,9 @@
 f: %25
-d zile intarziere
+s
 %22 %25 
@@ -4277,24 +4277,47 @@
 ts, 
-days_late)
+time_late(upload_time, deadline))%0A
     %0A
-%0A
 if _

print detailed lateness