Untitled

mail@pastecode.io avatar
unknown
python
a month ago
2.8 kB
9
Indexable
Never
def find_fraudulent_merchants(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
    mcc_dict = {}
    for mcc in mcc_thresholds:
        code, threshold = mcc.split(',')
        mcc_dict[code] = int(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 = {}
    
    # Parse charges and count transactions and fraudulent ones
    for charge in charges:
        _, charge_id, account_id, amount, code = charge.split(',')
        if account_id not in transaction_count:
            transaction_count[account_id] = 0
            fraudulent_count[account_id] = 0
        
        transaction_count[account_id] += 1
        
        # Count fraudulent transactions if the charge code is in fraud_codes
        if code in fraud_codes:
            fraudulent_count[account_id] += 1
    
    # Convert min_charges from string to int
    min_charges = int(min_charges)
    
    # Determine which merchants are fraudulent
    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, float('inf'))  # Get fraud threshold for the merchant's MCC
            if fraudulent_count[account_id] >= threshold:
                fraudulent_merchants.append(account_id)
    
    # Return the result as a lexicographically sorted comma-separated string
    return ','.join(sorted(fraudulent_merchants))

# Example Usage
non_fraud_codes = "approved,invalid_pin,expired_card"
fraud_codes = "do_not_honor,stolen_card,lost_card"

mcc_thresholds = [
    "retail,5",
    "airline,2",
    "restaurant,10",
    "venue,3"
]

merchant_mcc_map = [
    "acct_1,airline",
    "acct_2,venue",
    "acct_3,retail"
]

min_charges = "0"

charges = [
    "CHARGE,ch_1,acct_1,100,do_not_honor",
    "CHARGE,ch_2,acct_1,200,approved",
    "CHARGE,ch_3,acct_1,300,do_not_honor",
    "CHARGE,ch_4,acct_2,100,lost_card",
    "CHARGE,ch_5,acct_2,200,lost_card",
    "CHARGE,ch_6,acct_2,300,lost_card",
    "CHARGE,ch_7,acct_3,100,lost_card",
    "CHARGE,ch_8,acct_3,200,stolen_card",
    "CHARGE,ch_9,acct_3,100,approved"
]

# Test the function
result = find_fraudulent_merchants(non_fraud_codes, fraud_codes, mcc_thresholds, merchant_mcc_map, min_charges, charges)
print(result)
Leave a Comment