Problem Statement:
You are given a CSV file containing financial transactions. Each row in the file represents
a transaction with the following fields:
TransactionID (String) – Unique identifier for the transaction
Date (String, format: YYYY-MM-DD) – Date of the transaction
Amount (Float) – Transaction amount in USD
Type (String) – Either "Credit" or "Debit"
AccountNumber (String) – The account number associated with the transaction
Your task is to write a program that:
1. Reads the CSV file.
2. Computes the total credited amount and total debited amount.
3. Finds the account with the highest number of transactions.
4. Writes the computed results to an output text file in the following format:
Code-
Total Credit: <total_credit>
Total Debit: <total_debit>
Most Active Account: <account_number>
Input Format:
A CSV file named [Link] with the above-mentioned columns.
Output Format:
A text file named [Link] with the computed results.
Constraints:
The file contains at most 100,000 transactions.
Transaction amounts are between 0.01 and 1,000,000.00 USD.
Python Solution:
import csv
from collections import defaultdict
def process_transactions(file_path):
total_credit = 0
total_debit = 0
account_counts = defaultdict(int)
# Read the CSV file
with open(file_path, mode='r') as file:
reader = [Link](file)
for row in reader:
amount = float(row['Amount'])
account = row['AccountNumber']
transaction_type = row['Type']
if transaction_type == 'Credit':
total_credit += amount
elif transaction_type == 'Debit':
total_debit += amount
account_counts[account] += 1
# Find the most active account
most_active_account = max(account_counts, key=account_counts.get)
# Write results to [Link]
with open('[Link]', 'w') as output_file:
output_file.write(f"Total Credit: {total_credit:.2f}\n")
output_file.write(f"Total Debit: {total_debit:.2f}\n")
output_file.write(f"Most Active Account: {most_active_account}\n")
# Example usage
process_transactions('[Link]')