May 01 2022 02:54 AM - edited May 03 2022 02:30 PM
I have a dataset more or less like this
date | Expire in | Amount | Use | is Covered by date and Amount |
01/05 | 3 | 5 | ||
02/05 | 1 | Yes | ||
04/05 | ||||
05/05 | 1 | Yes | ||
06/05 | 1 | No | ||
08/05 | 1 | No | ||
09/05 | 3 | 1 | ||
10/05 | 1 | Yes | ||
11/05 | ||||
12/05 | 1 | No | ||
every row has a date and I need to check if for a specific date where the ticket is used the amount of ticket bought on a previous date is covered or not (based both on quantity and expire days)
Can someone help me with this tricky problem?
in python it would be something like this:
matrix = [
[3,5,0],
[0,0,1],
[0,0,0],
[0,0,1],
[0,0,1],
[0,0,1],
[3,1,0],
[0,0,1],
[0,0,1],
]
def consume_ticket(index, tickets):
for k_index, v_exp_amount in tickets.items():
if v_exp_amount["expire"] < index-k_index:
continue
if v_exp_amount["amount"] > 0:
v_exp_amount["amount"] -= 1
return True
else:
continue
return False
packs = {}
for index, line in enumerate(matrix):
is_covered = False
if line[0]:
packs[index]={"expire":line[0],"amount":line[1]}
if line[2]:
is_covered = consume_ticket(index, packs)
print(f'{index} {line} {is_covered}')
else:
print(f'{index} {line}')
0 [3, 5, 0]
1 [0, 0, 1] True
2 [0, 0, 0]
3 [0, 0, 1] True
4 [0, 0, 1] False
5 [0, 0, 1] False
6 [3, 1, 0]
7 [0, 0, 1] True
8 [0, 0, 1] False