Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
3.7 kB
2
Indexable
Never
def find_fraudulent_merchants_part2_corrected(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 = {}
    
    # 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 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
            
            # Debug output for each merchant
            print(f"Merchant: {account_id}, Fraudulent Transactions: {fraudulent_count[account_id]}, Total Transactions: {transaction_count[account_id]}, Fraud Percentage: {fraud_percentage}, MCC Threshold: {threshold}")
            
            # 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))

# Test case for Part 2 (Example provided)
non_fraud_codes_2 = "approved,invalid_pin,expired_card"
fraud_codes_2 = "do_not_honor,stolen_card,lost_card"

mcc_thresholds_2 = [
    "retail,0.5",
    "airline,0.25",
    "restaurant,0.8",
    "venue,0.25"
]

merchant_mcc_map_2 = [
    "acct_1,airline",
    "acct_2,venue",
    "acct_3,venue"
]

min_charges_2 = "3"

charges_2 = [
    "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,400,approved",
    "CHARGE,ch_5,acct_2,500,approved",
    "CHARGE,ch_6,acct_2,600,lost_card",
    "CHARGE,ch_7,acct_2,700,approved",
    "CHARGE,ch_8,acct_2,800,approved",
    "CHARGE,ch_9,acct_3,800,approved",
    "CHARGE,ch_10,acct_3,700,approved",
    "CHARGE,ch_11,acct_3,600,approved",
    "CHARGE,ch_12,acct_3,500,stolen_card",
    "CHARGE,ch_13,acct_3,500,stolen_card",
    "CHARGE,ch_14,acct_2,400,stolen_card"
]

# Running the updated test case for Part 2
result_part_2_final = find_fraudulent_merchants_part2_corrected(non_fraud_codes_2, fraud_codes_2, mcc_thresholds_2, merchant_mcc_map_2, min_charges_2, charges_2)
result_part_2_final
Leave a Comment