From 0106ef5b05d89701242eaec5f1f51fc16957e2f6 Mon Sep 17 00:00:00 2001 From: huangrui199126 Date: Sun, 28 Aug 2016 11:19:25 -0700 Subject: [PATCH] Create getMinGap --- Problems/src/getMinGap | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Problems/src/getMinGap diff --git a/Problems/src/getMinGap b/Problems/src/getMinGap new file mode 100644 index 0000000..fe26529 --- /dev/null +++ b/Problems/src/getMinGap @@ -0,0 +1,49 @@ +#!/usr/bin/python + +import sys +import datetime +class Transaction: + def getDate(self, str): + return datetime.date(*map(int, str.strip().split('/'))) + def __init__(self,name,time): + self.name = name + self.time = self.getDate(time) +arr = [Transaction("iWatch", "2016/05/01"), +Transaction("iPhone", "2016/01/08"), +Transaction("phone case", "2016/01/10"), +Transaction("laptop", "2016/01/05"), +Transaction("iPhone", "2016/01/02"), +Transaction("phone case", "2016/01/05"), +Transaction("glass", "2016/01/01") +] + +def getList(str,arr): + res=[] + for item in arr: + if item.name==str: + res.append(item.time) + return res + +#dif = day1 - day2 +#dif.days + +def getMin(str1, str2): + list1=getList(str1, arr) + list2=getList(str2, arr) + list1.sort() + list2.sort() + i,j=0,0 + res=sys.maxint + while ilist2[j]: + j+=1 + else: + i+=1 + j+=1 + return res + +print getMin("iPhone", "phone case") +print getMin("glass", "phone case")