通行证
- 积分
- 603
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-1-16
- 最后登录
- 1970-1-1
|
本帖最后由 ByronHsu 于 2021-9-29 16:39 编辑
- def filter_cross_matched(trade1, trade2, isMatched):
- trade1 = sorted(trade1)[::-1] # largest to smallest, so we can pop from right
- trade2 = sorted(trade2)[::-1]
- unmatched1 = []
- unmatched2 = []
- while trade1 and trade2:
- if isMatched(trade1[-1], trade2[-1]):
- trade1.pop()
- trade2.pop()
- elif trade1[-1] > trade2[-1]:
- unmatched2.append(trade2.pop())
- elif trade1[-1] < trade2[-1]:
- unmatched1.append(trade1.pop())
- # the remaining in trade1 / trade2 are also unmatched
- unmatched1 += trade1
- unmatched2 += trade2
- return unmatched1, unmatched2
- def filter_inner_matched(trade, isMatched):
- B_list = [t for t in trade if t.split(",")[1] == "B"]
- S_list = [t for t in trade if t.split(",")[1] == "S"]
- B_list, C_list = filter_cross_matched(B_list, S_list, isMatched)
- return sorted(B_list + C_list)
- def solution(house_trades, street_trades):
- # exact match
- # key = symbol + type + quantity + id
- isExact = lambda x,y: x == y
- house_trades, street_trades = filter_cross_matched(house_trades, street_trades, isExact)
-
- # fuzzy match
- # key = (symbol + type + quantity, id)
- isFuzzy = lambda x,y: x.split(",")[:3] == y.split(",")[:3]
- house_trades, street_trades = filter_cross_matched(house_trades, street_trades, isFuzzy)
-
- # offsetting match
- isOffsetMatched = lambda x, y: x.split(",")[0] + x.split(",")[2] == y.split(",")[0] + y.split(",")[2]
- house_trades, street_trades = filter_inner_matched(house_trades, isOffsetMatched), filter_inner_matched(street_trades, isOffsetMatched)
- ans = sorted(house_trades + street_trades)
- return ans
复制代码 |
|