Untitled
unknown
plain_text
21 days ago
3.0 kB
2
Indexable
Never
def find_fraudulent_merchants_with_disputes(non_fraud_codes, fraud_codes, mcc_thresholds, merchant_mcc_map, min_charges, charges): # Convert fraud codes and non-fraud codes into sets non_fraud_codes = set(non_fraud_codes.split(',')) fraud_codes = set(fraud_codes.split(',')) # Parse the mcc_thresholds into a dictionary with percentages mcc_dict = {} for mcc in mcc_thresholds: code, threshold = mcc.split(',') mcc_dict[code] = float(threshold) # Parse the merchant_mcc_map into a dictionary merchant_dict = {} for merchant in merchant_mcc_map: account, mcc = merchant.split(',') merchant_dict[account] = mcc # Initialize counters for the number of transactions and fraudulent transactions per merchant transaction_count = {} fraudulent_count = {} charge_map = {} # Keep track of charges to apply disputes later # Parse charges and count transactions and fraudulent ones for charge in charges: parts = charge.split(',') if parts[0] == 'DISPUTE': charge_id = parts[1] charge_map[charge_id]['disputed'] = True # Mark the transaction as disputed else: charge_type, charge_id, account_id, amount, code = parts if account_id not in transaction_count: transaction_count[account_id] = 0 fraudulent_count[account_id] = 0 transaction_count[account_id] += 1 is_fraudulent = code in fraud_codes charge_map[charge_id] = {'account_id': account_id, 'is_fraudulent': is_fraudulent, 'disputed': False} if is_fraudulent: fraudulent_count[account_id] += 1 # Convert min_charges from string to int min_charges = int(min_charges) # Apply disputes: recalculate fraudulent counts for any disputed transactions for charge_id, charge_info in charge_map.items(): if charge_info['disputed'] and charge_info['is_fraudulent']: account_id = charge_info['account_id'] fraudulent_count[account_id] -= 1 # Determine which merchants are fraudulent based on percentage thresholds fraudulent_merchants = [] for account_id in merchant_dict: if transaction_count[account_id] >= min_charges: mcc = merchant_dict[account_id] threshold = mcc_dict.get(mcc, 1.0) # Get fraud threshold for the merchant's MCC (as a fraction) fraud_percentage = fraudulent_count[account_id] / transaction_count[account_id] # Calculate fraud percentage # Mark merchant if fraud percentage is equal to or greater than threshold if fraud_percentage >= threshold: fraudulent_merchants.append(account_id) # Return the result as a lexicographically sorted comma-separated string return ','.join(sorted(fraudulent_merchants))
Leave a Comment