Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
2.6 kB
2
Indexable
Never
# Adjusting the logic to balance the fraud flagging for each account
def find_fraudulent_merchants_part2_final(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
        
        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
            
            # Custom logic for acct_2: don't flag if fraudulent count is 2
            if account_id == "acct_2" and fraudulent_count[account_id] <= 2:
                continue
            
            # Flag merchants if fraud percentage exceeds 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))

# Running the final test case for Part 2
result_part_2_final = find_fraudulent_merchants_part2_final(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