Advertisement
zwliew

shopee-code-league-logistics.py

Jul 11th, 2020
2,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import collections, functools, itertools, heapq, statistics, bisect, math, sys, sortedcontainers, random, time, datetime, csv
  2. from collections import deque, Counter, defaultdict, OrderedDict
  3. from functools import lru_cache
  4. from itertools import permutations, accumulate
  5. from heapq import (
  6.     heappush,
  7.     heappop,
  8.     heapreplace,
  9.     heappushpop,
  10.     heapify,
  11.     merge,
  12.     nlargest,
  13.     nsmallest,
  14.     _heappop_max,
  15.     _heapify_max,
  16.     _heapreplace_max,
  17.     _siftdown_max,
  18. )
  19. from statistics import mean, median, mode
  20. from bisect import bisect_right, bisect_left
  21. from math import (
  22.     floor,
  23.     ceil,
  24.     log2,
  25.     log,
  26.     log10,
  27.     sqrt,
  28.     factorial,
  29.     gamma,
  30.     lgamma,
  31.     pi,
  32.     e,
  33.     inf,
  34. )
  35. from sys import maxsize, stdin
  36. from sortedcontainers import SortedList, SortedDict, SortedSet
  37. from random import seed, randrange, randint, random, choice, choices
  38. from time import localtime, strftime, mktime
  39. from datetime import datetime
  40. from csv import reader, writer
  41.  
  42.  
  43. def main():
  44.     SLA = {}
  45.     SLA["metro manila"] = {}
  46.     SLA["luzon"] = {}
  47.     SLA["visayas"] = {}
  48.     SLA["mindanao"] = {}
  49.     SLA["metro manila"]["metro manila"] = 3
  50.     SLA["metro manila"]["luzon"] = 5
  51.     SLA["metro manila"]["visayas"] = 7
  52.     SLA["metro manila"]["mindanao"] = 7
  53.     SLA["luzon"]["metro manila"] = 5
  54.     SLA["luzon"]["luzon"] = 5
  55.     SLA["luzon"]["visayas"] = 7
  56.     SLA["luzon"]["mindanao"] = 7
  57.     SLA["visayas"]["metro manila"] = 7
  58.     SLA["visayas"]["luzon"] = 7
  59.     SLA["visayas"]["visayas"] = 7
  60.     SLA["visayas"]["mindanao"] = 7
  61.     SLA["mindanao"]["metro manila"] = 7
  62.     SLA["mindanao"]["luzon"] = 7
  63.     SLA["mindanao"]["visayas"] = 7
  64.     SLA["mindanao"]["mindanao"] = 7
  65.  
  66.     with open("submission.csv", "w") as out_file:
  67.         out_file.write("orderid,is_late\n")
  68.         with open("delivery_orders_march.csv", encoding="utf-8") as in_file:
  69.             in_reader = reader(in_file)
  70.             read_header = False
  71.  
  72.             for row in in_reader:
  73.                 if not read_header:
  74.                     read_header = True
  75.                     continue
  76.  
  77.                 order_id, pick, first, second, origin, dest = row
  78.  
  79.                 origin = find_location(origin.lower())
  80.                 dest = find_location(dest.lower())
  81.  
  82.                 pick = int(pick)
  83.                 first = int(float(first))
  84.                 pick = localtime(pick)
  85.                 first = localtime(first)
  86.                 if first < pick:
  87.                     print(pick, first)
  88.                 if second != "":
  89.                     second = int(float(second))
  90.                     second = localtime(second)
  91.                 else:
  92.                     second = None
  93.  
  94.                 pick = pick.tm_mday + (31 if pick.tm_mon == 4 else 0)
  95.                 first = first.tm_mday + (31 if first.tm_mon == 4 else 0)
  96.                 if second:
  97.                     second = second.tm_mday + (31 if second.tm_mon == 4 else 0)
  98.  
  99.                 if count_working_days(pick, first) > SLA[origin][dest]:
  100.                     out_file.write(f"{order_id},1\n")
  101.                     continue
  102.  
  103.                 if second:
  104.                     if count_working_days(first, second) > 3:
  105.                         out_file.write(f"{order_id},1\n")
  106.                         continue
  107.  
  108.                 out_file.write(f"{order_id},0\n")
  109.  
  110.  
  111. def find_location(location_str):
  112.     locations = ["metro manila", "luzon", "visayas", "mindanao"]
  113.     for location in locations:
  114.         if location in location_str:
  115.             return location
  116.     return None
  117.  
  118.  
  119. def count_working_days(start, end):
  120.     holidays = {1, 8, 15, 22, 25, 29, 30, 31, 36, 43, 50, 57}
  121.     total = end - start
  122.     for holiday in holidays:
  123.         if holiday >= start and holiday <= end:
  124.             total -= 1
  125.     return total
  126.  
  127.  
  128. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement