-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1169.py
134 lines (114 loc) · 5.07 KB
/
1169.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''
1169. Invalid Transactions
https://leetcode.com/contest/weekly-contest-151/problems/invalid-transactions/
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of
another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values
representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are
possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because
the second transaction occurs within a difference of 60 minutes,
have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000.
'''
from typing import *
class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
T = []
# parsing all transactions
for tran in transactions:
name, time, amount, city = tran.split(',')
time, amount = int(time), int(amount)
T.append((name, time, amount, city))
invalidT = []
# do comparison on all permutation
for name, time, amount, city in T:
invlid = amount > 1000
if not invlid:
for nameX, timeX, amountX, cityX in T:
if name == nameX and abs(time - timeX) <= 60 and city != cityX:
invlid = True
break
if invlid:
invalidT.append("{},{},{},{}".format(name, time, amount, city))
return invalidT
class UglySolution:
def invalidTransactions(self, transactions):
dict = {}
invalid = []
for tran in transactions:
tranInfo = tran.split(',')
if tranInfo[0] in dict:
dict[tranInfo[0]].append((int(tranInfo[1]),tranInfo[3],int(tranInfo[2]),tran))
else:
dict[tranInfo[0]] = [(int(tranInfo[1]),tranInfo[3],int(tranInfo[2]),tran)]
for personTran in dict:
personTranList = dict[personTran]
for i in range(len(personTranList)):
invalidi = False
(timei,cityi,amounti,trani) = personTranList[i]
if amounti > 1000:
invalidi = True
for j in range(i,len(personTranList)):
(timej,cityj,amountj,tranj) = personTranList[j]
if cityi!=cityj and abs(timei-timej)<=60:
invalidi = True
if amountj <= 1000 and tranj not in invalid:
invalid.append(tranj)
if invalidi and trani not in invalid:
invalid.append(trani)
return invalid
import unittest
class InvalidTranTest(unittest.TestCase):
def testExample1(self):
tran = ["alice,20,800,mtv","alice,50,100,beijing"]
r = Solution().invalidTransactions(tran)
self.assertEqual(r, ["alice,20,800,mtv","alice,50,100,beijing"])
def testExample2(self):
tran = ["alice,20,800,mtv","alice,50,1200,mtv"]
r = Solution().invalidTransactions(tran)
self.assertEqual(r, ["alice,50,1200,mtv"])
def testExample3(self):
tran = ["alice,20,800,mtv","bob,50,1200,mtv"]
r = Solution().invalidTransactions(tran)
self.assertEqual(r, ["bob,50,1200,mtv"])
def testLeetCodeTestCase1(self):
tran = ["bob,689,1910,barcelona",
"alex,696,122,bangkok",
"bob,832,1726,barcelona",
"bob,820,596,bangkok",
"chalicefy,217,669,barcelona",
"bob,175,221,amsterdam"]
r = Solution().invalidTransactions(tran)
self.assertEqual(r, ["bob,689,1910,barcelona","bob,832,1726,barcelona",
"bob,820,596,bangkok"])
def testLeetCodeTestCase2(self):
tran = ["bob,627,1973,amsterdam",
"alex,387,885,bangkok",
"alex,355,1029,barcelona",
"alex,587,402,bangkok",
"chalicefy,973,830,barcelona",
"alex,932,86,bangkok",
"bob,188,989,amsterdam"]
r = Solution().invalidTransactions(tran)
self.assertEqual(r, ["bob,627,1973,amsterdam","alex,387,885,bangkok",
"alex,355,1029,barcelona"])
if __name__ == "__main__":
unittest.main()