The Daily WTF
Follow
CodeSOD: Mailing it In
Dan B encounters a puzzling request from a bank to receive an email with the total of all transactions in a file. The input file contains transaction amounts in zero-padded cents (e.g., "0001000" for $10). The bank requires the total as dollars and cents (e.g., "10.00").Dan's initial solution involves a complex process of string manipulation, decimal conversion, and line iteration. This approach is inefficient, error-prone, and memory-intensive.A simpler and more reliable solution is to use Python's built-in functions to parse the input file and calculate the total.The first step is to read the transaction amounts from the file using the "with" statement and list comprehension.Next, remove leading zeroes and convert each amount to a decimal using the "Decimal" type.Finally, sum the decimal amounts to obtain the total.This approach is straightforward, efficient, and avoids the pitfalls of string manipulation and multiple iterations.While the current volume of transactions is low, the improved solution ensures scalability and reliability as the workload increases.