## DEV Community is a community of 887,564 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Daily Challenge #4 - Checkbook Balancing

Good morning, everyone.

Don’t say I didn’t warn you, we’re moving from letters to numbers with this challenge.

Today’s challenge comes from user @g964 on CodeWars.

You are given a small checkbook to balance that is given to you as a string. Sometimes, this checkbook will be cluttered by non-alphanumeric characters.

The first line shows the original balance. Each other (not blank) line gives information: check number, category, and check amount.

You need to clean the lines first, keeping only letters, digits, dots, and spaces. Next, return the report as a string. On each line of the report, you have to add the new balance. In the last two lines, return the total expenses and average expense. Round your results to two decimal places.

#### Example Checkbook

1000.00
125 Market 125.45
126 Hardware 34.95
127 Video 7.45
128 Book 14.32
129 Gasoline 16.10

#### Example Solution

Original_Balance: 1000.00
125 Market 125.45 Balance 874.55
126 Hardware 34.95 Balance 839.60
127 Video 7.45 Balance 832.15
128 Book 14.32 Balance 817.83
129 Gasoline 16.10 Balance 801.73
Total expense 198.27
Average expense 39.65

#### Challenge Checkbook

1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;

Good luck and happy coding!

Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

## Discussion (39) I'm Luis! \^-^/

Here's my attempt:

``````const checkbook = `
1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;
`.trim()

const round = value => Math.round(value * 100) / 100

const cleanedCheckBook = checkbook.replace(/[^A-Za-z0-9\s\.]/gmi, '')
const lines = cleanedCheckBook.split("\n")
const originalBalance = parseFloat(lines.shift())

let totalExpenses = 0
const expenses = []

const linesProcessed = lines.map(line => {
const [number, category, expense] = line.split(' ')
const expenseFloat = parseFloat(expense)
totalExpenses += expenseFloat
expenses.push(expenseFloat)

const currentBalance = originalBalance - totalExpenses

return `\${number} \${category} \${round(expense)} Balance \${round(currentBalance)}`
}).join("\n")

const averageSpent = expenses.reduce((total, sum) => total + sum) / expenses.length

console.log(`
Original_Balance: \${round(originalBalance)}
\${linesProcessed}
Total Expenses: \${round(totalExpenses)}
Average spent: \${round(averageSpent)}
`)
``````

``````const checkbook = `
1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;
`.trim() // this will remove spaces, tabs, and breaklines \n at the beginning and end

// Math.round doesn't round to X number of digits so I'm using this hacky method
const round = value => Math.round(value * 100) / 100
// skip all non A-Za-z0-9\s\. caracters, globally, multiline, and case insensitive
const cleanedCheckBook = checkbook.replace(/[^A-Za-z0-9\s\.]/gmi, '')
// make an array where every element is a line
const lines = cleanedCheckBook.split("\n")
// extract the first element and save the value in float
const originalBalance = parseFloat(lines.shift())

let totalExpenses = 0
const expenses = [] // for the average

// go thru each line and change it
const linesProcessed = lines.map(line => {
const [number, category, expense] = line.split(' ')
const expenseFloat = parseFloat(expense)

totalExpenses += expenseFloat
expenses.push(expenseFloat)

const currentBalance = originalBalance - totalExpenses

return `\${number} \${category} \${round(expense)} Balance \${round(currentBalance)}`
}).join("\n")

const averageSpent = expenses.reduce((total, sum) => total + sum) / expenses.length

console.log(`
Original_Balance: \${round(originalBalance)}
\${linesProcessed}
Total Expenses: \${round(totalExpenses)}
Average spent: \${round(averageSpent)}
`)
`````` Donald Feury • Edited on

Here is my attempt, did it in Go: Github

This executable boils down to:

``````package main

import (
"fmt"

"github.com/Dak425/dev-to-challenge-4-go/pkg/checkbook/memory"
)

func main() {
raw := `1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;`

cb := memory.NewInMemoryCheckBook(raw)

fmt.Print(cb.FullReport())
}
``````

Output:

``````Starting Balance: 1233.00
 -> Check Number: 125, Category: Hardware, Amount: 24.80, Remaining Balance: 1208.20
 -> Check Number: 123, Category: Flowers, Amount: 93.50, Remaining Balance: 1114.70
 -> Check Number: 127, Category: Meat, Amount: 120.90, Remaining Balance: 993.80
 -> Check Number: 120, Category: Picture, Amount: 34.00, Remaining Balance: 959.80
 -> Check Number: 124, Category: Gasoline, Amount: 11.00, Remaining Balance: 948.80
 -> Check Number: 123, Category: Photos, Amount: 71.40, Remaining Balance: 877.40
 -> Check Number: 122, Category: Picture, Amount: 93.50, Remaining Balance: 783.90
 -> Check Number: 132, Category: Tires, Amount: 19.00, Remaining Balance: 764.90
 -> Check Number: 129, Category: Stamps, Amount: 13.60, Remaining Balance: 751.30
 -> Check Number: 129, Category: Fruits, Amount: 17.60, Remaining Balance: 733.70
 -> Check Number: 129, Category: Market, Amount: 128.00, Remaining Balance: 605.70
 -> Check Number: 121, Category: Gasoline, Amount: 13.60, Remaining Balance: 592.10
Total Costs: 640.90
Average Cost: 53.41
``````

Also, wooo first post on here 🎉 Kerri Shotts

Here's my take (JavaScript). A few notes:

• I sort by line #, and then by category
• The first line might actually be blank, so I trim the input
• Categories are assumed to be single words (no spaces allowed)
• Output includes digit grouping by current locale
• Full code (incl some basic tests): gist.github.com/kerrishotts/461a90...
``````
const sanitize = str => str.replace(/[^0-9A-Za-z\.\s]/g, "");

const notBlank = str => str !== "";

const extract = str => {
const [ line, category, expense ] = str.split(/\s+/);
return { line: Number(line), category, expense: Number(expense) };
};

const byLineAndCategory = (a, b) => a.line < b.line
? -1 : a.line > b.line
? 1 : a.category < b.category
? -1 : a.category > b.category
? 1 : 0;

const balanceReducer = (
{openingBalance, totalExpenses, entries},
{line, category, expense}
) => {
const newTotal = totalExpenses + expense;
const newBalance = openingBalance - newTotal;
return {
openingBalance,
totalExpenses: newTotal,
averageExpense: newTotal / (entries.length + 1),
entries: [ ...entries, {line, category, expense, balance: newBalance }]
}
};

const round2 = n => (Math.round(n * 100) / 100)
.toLocaleString(undefined, {
style: "decimal",
minimumFractionDigits: 2,
useGrouping: true
});

const balanceCheckbook = (checkbook) => {
const [openingBalanceStr, ...entries] =
sanitize(checkbook)
.trim()
.split("\n")
.filter(notBlank);

const openingBalance = Number(openingBalanceStr);

const initialState = {
openingBalance,
entries: [],
averageExpense: 0,
totalExpenses: 0
};

const report =
entries
.map(extract)
.sort(byLineAndCategory)
.reduce( balanceReducer, initialState );

return `
Original Balance: \${round2(report.openingBalance)}
\${report.entries.map(({line, category, expense, balance}) =>
`\${line} \${category} \${round2(expense)} Balance \${round2(balance)}`
).join("\n")}
Total Expenses: \${round2(report.totalExpenses)}
Average Expense: \${round2(report.averageExpense)}
`.trim();
};

`````` I'm Luis! \^-^/

I think is the most scalable solution since you first move all the data to a manipulable format, deal with it and then output it. jmc • Edited on

I'm impressed by how short many of the solutions are.

Clojure:

``````(ns checkbook
(:require [clojure.string :refer [join split]]))

;; split line into tokenized "entry"
(defn tokens [line]

;; append running balance onto entries
(defn with-running-balance [[start & entries]]
(reduce
(fn [acc entry]
(let [prev-bal  (last (last acc))
cur-bal   (- prev-bal (last entry))
new-entry (conj (vec entry) "Balance" cur-bal)]
(conj acc new-entry)))
[["Original_Balance" (last start)]]
entries))

;; output entry as string, with numbers rounded
(defn format-entry [xs]
(case (count xs)
2 (apply format "%s %.2f" xs)
5 (apply format "%s %s %.2f %s %.2f" xs)))

;; append running balance, include total & average, format nums
(defn balance [s]
(let [lines   (split s #"\n")
entries (map tokens lines)
\$\$      (map last (rest entries))]
(join "\n"
(map format-entry
(conj (with-running-balance entries)
["Total expense" (apply + \$\$)]
["Average expense" (/ (apply + \$\$) (count \$\$))])))))
`````` Martin Himmel • Edited on

PHP

It wasn't specified, but I sorted the check order. Also noticed that checks 123 and 129 are repeated two and three times, respectively, while 126, 128, and 131 are missing. I'm guessing the duplicate number were supposed to be the missing numbers. 😄

``````\$text = '1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;';

function checkbook_report(string \$str) {
\$data = format_checkbook_string(\$str);
\$balance = floatval(array_shift(\$data));
\$output = 'Original Balance: ' . number_format(\$balance, 2) . PHP_EOL;
\$expenses = [];

sort(\$data);

foreach (\$data as \$index => \$line) {
\$parts = explode(' ', \$line);
// handles multi-word categories (even though they don't exist in this challenge)
foreach (\$parts as \$line_segment) {
if (\$line_segment != end(\$parts)) {
\$output .= "\$line_segment ";
}
}
\$expenses[] = floatval(end(\$parts));
\$balance -= end(\$expenses);
\$output .= number_format(end(\$parts), 2) . ', Balance: ' . number_format(\$balance, 2) . PHP_EOL;
}

\$total_expenses = array_sum(\$expenses);
\$output .= 'Total expenses: ' . number_format(\$total_expenses, 2) . PHP_EOL;
\$output .= 'Average expense: ' . number_format(\$total_expenses / count(\$expenses), 2) . PHP_EOL;
return \$output;
}

function format_checkbook_string(string \$str) {
\$data = explode(PHP_EOL, \$str);
return array_map('filter_line', \$data);
}

function filter_line(string \$line) {
return preg_replace('/[^\w\s.]+/', '', \$line);
}

echo checkbook_report(\$text);
`````` BASH

``````#!/bin/bash
input='challenge.txt';
declare -a checkbook;
total=0;
for line in \$(cat \${input}|tr -cd [' ','0-9','.','A-Z','a-z','\n']);do
checkbook+=("\${line}");
done;

echo "Original_Balance: "\${checkbook};
for (( i = 1; i < \${#checkbook[@]}; i+=3 )); do
echo \${checkbook[@]:\${i}:3};
total=`echo "scale=2;\${total} + \${checkbook[\${i}+2]}" |bc`;
done;
average=`echo "scale=2;\${total}/((\${#checkbook[@]}-1)/3)"|bc`;

echo "Total expense: " \${total};
echo "Average expense: " \${average};
``````  Alvaro Montoro • Edited on

JavaScript

This is going to be one of those "don't do this at home" types of code (or maybe "do it at home but not at work"). I tried to do it as a single chain of commands, assuming that the string is going to be valid. It can be further cleaned and reduced, I'll try later.

Here is the code commented step-by-step:

``````const generateReport = checkbook => {
let current = 0;
// use regular expressions to remove unwanted characters
return checkbook.replace(/[^0-9a-z\. \n]/gi, "")
// separate the string into an array splitting by new line
.split("\n")
// update each value to include the total at the end
.map((val, index) => {
current = index === 0 ? val : (current - val.split(" ")).toFixed(2);
return index === 0 ? "Original Balance: " + val : val + ` \${current}`;
})
// convert array into string again
.join("\n")
// concatenate the total and average
.concat(`\nTotal expense: \${(checkbook.split("\n") - current).toFixed(2)}`)
.concat(`\nAverage expense: \${((checkbook.split("\n") - current)/(checkbook.split("\n").length-1) || 0).toFixed(2)}`);
}
``````

You can see it working on this CodePen. Alvaro Montoro

And as an extra, here is a version in which the checks are also sorted:

``````const generateReport = checkbook => {
let current = 0;
return checkbook.replace(/[^0-9a-z\. \n]/gi, "")
.split("\n")
.sort((a,b) => {
const arrA = a.split(" ");
const arrB = b.split(" ");
if (arrA.length > arrB.length) {
return 1;
} else if (arrB.length > arrA.length) {
return -1;
} else {
return parseInt(arrA) > parseInt(arrB) ? 1 : -1;
}
})
.map((val, index) => {
current = index === 0 ? val : (current - val.split(" ")).toFixed(2);
return index === 0 ? "Original Balance: " + val : val + ` \${current}`;
})
.join("\n")
.concat(`\nTotal expense: \${(checkbook.split("\n") - current).toFixed(2)}`)
.concat(`\nAverage expense: \${((checkbook.split("\n") - current)/(checkbook.split("\n").length-1) || 0).toFixed(2)}`);
}
`````` Jaime López

Here my contribution in javascript:

``````const input = `1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;`;

// clean the input
let balanceInputs = input
.split('\n')
.map(x => x.split(' ')
.filter(x => x.trim() !== ''));

// process input to create an object with all information
function processData(balance, ...inputs) {
const data = {
original : parseFloat(balance),
totalExpense: 0.0,
averageExpense: 0.0
};

const orders = inputs.map(i => {
const order = {
id: parseInt(i),
concept: i.match(/[0-9a-zA-Z.\s]/g).reduce((acc, current) => acc.concat(current)),
value: parseFloat(i),
balance: data.original - data.totalExpense - parseFloat(i)
};
data.totalExpense += order.value;
return order;
});

data.averageExpense = data.totalExpense / orders.length;

return {
data: data,
orders: orders
}
}

// shows data in the console as a report
function createReport(report) {
console.log('Original_Balance: ' + report.data.original.toFixed(fixed));
report.orders.forEach(function(order) {
console.log(order.id + ' ' + order.concept + ' ' + order.value.toFixed(fixed) + ' Balance ' + order.balance.toFixed(fixed));
});
console.log('Total expense ' + report.data.totalExpense.toFixed(fixed));
console.log('Average expense ' + report.data.averageExpense.toFixed(fixed));
}

const fixed = 2; // set the number of decimal places
const data = processData(...balanceInputs); // process data
createReport(data); // shows the report
`````` Siddharth Venkatesan

Ruby

``````class Checkbook

def initialize
@entries = []
@balance = 0.0
@orig_bal = 0.0
end

def total_expense
@entries.map {|e| e.check_amount}.sum
end

def average_expense
total_expense / @entries.size
end

@balance = @orig_bal = content.to_f
(1..content.size-1).each do |n|
line = content[n]
@entries << CheckEntry.new(line)
end
calculate_balance!
end

def calculate_balance!
@entries.sort!
@entries.each do |entry|
#puts "#{@balance} : #{entry.check_amount}"
@balance -= entry.check_amount
entry.running_balance = @balance
end
end

def to_s
disp = []
disp << "%.2f" % @orig_bal
disp << @entries.map {|e| e.to_s}
disp << "Total Expenses = #{"%.2f" % total_expense}"
disp << "Average Expenses = #{"%.2f" % average_expense}"

disp.join("\n")
end
end

class CheckEntry

attr_accessor :check_number, :category, :check_amount, :running_balance

def initialize(line)
@check_number = 0
@category = ""
@check_amount = 0.0
@running_balance = 0.0
end

line = sanitize(line)
matches = /^(\d+)\s+(.*?)\s(.*)\$/.match(line)
@check_number = matches.to_i
@category = matches
@check_amount = matches.to_f
end

def sanitize(line)
line.gsub(/([^\d\w\s\.])/, '')
end

def to_s
"#{@check_number} #{@category} #{"%.2f" % @check_amount} #{"%.2f" % @running_balance}"
end

def <=>(other)
@check_number <=> other.check_number
end
end

puts Checkbook.new
``````

Output :

``````1233.00
120 Picture 34.00 1199.00
121 Gasoline 13.60 1185.40
122 Picture 93.50 1091.90
123 Flowers 93.50 998.40
123 Photos 71.40 927.00
124 Gasoline 11.00 916.00
125 Hardware 24.80 891.20
127 Meat 120.90 770.30
129 Stamps 13.60 756.70
129 Fruits 17.60 739.10
129 Market 128.00 611.10
132 Tires 19.00 592.10
Total Expenses = 640.90
Average Expenses = 53.41
`````` cvanpoelje
``````tidyCheckbook = checkbook => {
sum = 0;
rules = checkbook.split("\n");
rules = `Original_balance: \${rules}`;
amountOfRules = rules.length-1;
return rules
.map(rule => {
rule = rule.replace(/[;?!{},]/g, "").split(" ");
return rule =  { id: rule, category: rule, checkAmount: parseFloat(rule).toFixed(2)}
})
.sort((a, b) => a.id - b.id)
.map(rule => {
return (`\${rule.id} \${rule.category} \${isNaN(rule.checkAmount) ? '':rule.checkAmount}`)})
.join("\n")
.concat(`\n======================\nTotal expense: \${sum}\nAverage expense: \${sum / amountOfRules}`
)
}
`````` kesprit

My solution in Swift :

``````/// Daily Challenge #4 - Checkbook Balancing

let checkbook = """
1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;
"""

func analyseCheckbook(checkbook: String) {
typealias CheckbookEntry = (number: String, description: String, expense: Double)
let roundFormat = "%.2f"
var count: Double = 0
var totalExpense: Double = 0
var checkbookEntries = [CheckbookEntry]()

var cleanCheckbook = checkbook.filter { char -> Bool in
char.isLetter || char.isNumber || char.isWhitespace || char == "."
}.components(separatedBy: .newlines)

let originalBalance = Double(cleanCheckbook.removeFirst()) ?? 0
cleanCheckbook.forEach { checkbook in
let line = checkbook.components(separatedBy: .whitespaces)
if line.count == 3 {
checkbookEntries.append(CheckbookEntry(number: line, description: line, expense: Double(line) ?? 0))
}
}

print("Original_Balance: \(originalBalance)")

checkbookEntries.forEach { entry in
count += entry.expense
totalExpense += entry.expense
print("\(entry.number) \(entry.description) Balance \(String(format: roundFormat, originalBalance - count))")
}
let averageExpense = (totalExpense / Double(checkbookEntries.count))
print("Total expense \(String(format: roundFormat, totalExpense))")
print("Average expense \(String(format: roundFormat, averageExpense))")
}
`````` costicaaa

i think it is quite funny how most of the answers here ignore basically half of the requirement and do not print the new balance :)

i think it should be a signal you are ignoring the task and creating something that seems fun for you to solve ( looking at how everyone handles the 'checks numbers' in his own way, even tho it was not a req :) )

just my 2 cents, but always open for a good constructive namecalling kind of talk hihi Bárbara Perdigão

Java:

``````class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
String report = balanceCheckbook(input);

System.out.println(report);
}

static String  balanceCheckbook(String input){

DecimalFormat df = new DecimalFormat("####.00");

ArrayList<String> checkbook = new ArrayList<>(Arrays.asList(input.replaceAll("(\\b\\D\\W)", " ").split("\\n")));

float currentBalance = parseFloat(checkbook.get(0));
float totalExpense = 0;

StringBuilder reportBuilder = new StringBuilder(("Original_Balance: " + df.format(currentBalance) + "\n"));

for (int count = 1; count <checkbook.size(); count++){
String[] values = checkbook.get(count).replaceAll("\\b(\\s{2})", " ").split("\\s");
totalExpense += parseFloat(values);
currentBalance -= parseFloat(values);
checkbook.set(count, values + " " + values + " " + df.format(parseFloat(values)));
reportBuilder.append(checkbook.get(count)).append(" Balance ").append(df.format(currentBalance)).append("\n");
}

float averageExpense = totalExpense / (checkbook.size() - 1);

reportBuilder.append("Total expense ").append(df.format(totalExpense)).append("\n")
.append("Average expense ").append(df.format(averageExpense));

return reportBuilder.toString().replace(",", ".");
}
}
`````` cloudyhug

``````import Data.Char

processCheckbook :: String -> String
processCheckbook checkbook =
unlines \$ prettyBalance : (reverse prettyEntries) ++ [prettyTotal, prettyAverage]
where
(originalBalance : entries) = lines \$ filter (\x -> or [isAlphaNum x, isSpace x, x == '.']) checkbook
processEntry (prettyEntries, total, balance) entry =
let expense = read \$ words entry !! 2
newBalance = balance - expense
in ((entry ++ " Balance " ++ show newBalance) : prettyEntries, total + expense, newBalance)
(prettyEntries, totalExpense, _) = foldl processEntry ([], 0, read originalBalance) entries
prettyBalance = "Original_Balance: " ++ originalBalance
prettyTotal = "Total expense " ++ show totalExpense
prettyAverage = "Average expense " ++ show (totalExpense / (fromIntegral \$ length entries))
`````` ``````const round2 = n => Math.round(n*100)/100;
const balance = book => {
const lines = book.replace(/[^\da-z\.\s]/gi, '').split('\n');
const [originalBalance, ...expenses] = lines.map(line => line.match(/\d+\.\d{1,2}/)).map(Number);
const balances = expenses.reduce((acc, expense, index) => [ ...acc, acc[index] - expense ], [originalBalance]).slice(1);
const totalExpense = expenses.reduce((x, y) => x + y, 0);
return [
`Original_Balance: \${originalBalance}`,
...lines.slice(1).map((line, index) => `\${line} Balance \${round2(balances[index])}`),
`Total expense \${round2(totalExpense)}`,
`Average expense \${round2(totalExpense / expenses.length)}`
].join('\n');
}
`````` JS below :)

``````function balanced(note) {
clean = (s) => s.match(/([a-z0-9\s.])/gi).join('');
let balance, total = 0, average = 0;
return [...note.split('\n').map((n, i) => {
const a = clean(n);
if (!i) {
balance = parseFloat(a).toFixed(2);
return `Original_Balance: \${balance}`;
}
let [num, obj, amount] = a.split(' ');
total += parseFloat(amount);
average = total / (i);
balance -= amount;
return `\${num} \${obj} \${amount} Balance \${balance.toFixed(2)}`;
}),
`Total expense \${total.toFixed(2)}`,
`Average expense \${average.toFixed(2)}`]
.join('\n');
}
`````` Kevin Sullivan • Edited on
``````// UTILS
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
const map = (mapping: {
(x: any, index: number): string;
(value: any, index: number, array: any[]): {};
}) => (xs: any[]) => xs.map(mapping);
const sum = (values: number[]) =>
values.reduce((total, current) => total + current, 0);
const toLines = (text: string) => text.split("\n");
const sort = (sortFunction: (a: any, b: any) => number) => (xs: any[]) =>
xs.sort(sortFunction);

// DOMAIN
interface Checkbook {
initialBalance: number;
checks: Check[];
}
interface Check {
id: string;
amount: number;
type: string;
}
const total = (checks: Check[]) => sum(checks.map(toAmount));
const average = (checks: Check[]) => total(checks) / checks.length;
const toAmount = ({ amount }) => amount;
const currentBalance = (initialBalance: number, checks: Check[]) =>
initialBalance - total(checks);
const byId = {
asc: (a: Check, b: Check) => Number(a.id) - Number(b.id),
desc: (a: Check, b: Check) => Number(b.id) - Number(a.id),
};

// PARSING
const parseId = (text: string) => text;
const parseType = (text: string) =>
text.replace(/[^a-zA-Z0-9_]/, "").replace(/[!}]/, "");
const parseAmount = (text: string) => parseFloat(text);
const parseCheck = (text: string): Check => {
const [id, type, amount] = text.split(" ");
return {
id: parseId(id),
type: parseType(type),
amount: parseAmount(amount),
};
};
const parse = (text: string) => {
const [initialBalance, ...checks] = toLines(text);
return {
initialBalance: parseAmount(initialBalance),
checks: checks.map(parseCheck),
};
};

// HELPERS
const displayAmount = (amount: number) => amount.toFixed(2);
const col = (...children: string[]) => children.join("\n");
// const row = (...children: string[]) => children.join(" ");

// COMPONENTS
const InitialBalance = ({ balance }: { balance: number }) =>
`Original_Balance: \${displayAmount(balance)}`;
const Check = ({ check }: { check: Check }) =>
`\${check.id} \${check.type} \${displayAmount(check.amount)}`;
const RunningBalance = ({ balance }: { balance: number }) =>
`Balance \${displayAmount(balance)}`;
const CheckRow = ({ check, balance }) =>
`\${Check({ check })} \${RunningBalance({ balance })}`;
const TotalExpense = ({ total }: { total: number }) =>
`Total expense \${displayAmount(total)}`;
const AverageExpense = ({ average }: { average: number }) =>
`Average expense \${displayAmount(average)}`;

// APP
const Checkbook = ({
checkbook: { initialBalance, checks },
}: {
checkbook: Checkbook;
}) => {
const checkRows: (checks: Check[]) => string[] = pipe(
sort(byId.asc),
map((check: Check, index: number) =>
CheckRow({
check,
balance: currentBalance(initialBalance, checks.slice(0, index + 1)), // index + 1 means inclusive
}),
),
);

return col(
InitialBalance({ balance: initialBalance }),
...checkRows(checks),
TotalExpense({ total: total(checks) }),
AverageExpense({ average: average(checks) }),
);
};

// WRAPPER
export const checkbookBalancer = (text: string) => {
const checkbook = parse(text);
return Checkbook({ checkbook });
};
`````` Dylan Paulus

Nim

``````import strutils
import sequtils
import re

proc parse(input: string): string =
var separatedInput = splitLines(input)
let originalBalance = parseFloat(separatedInput)
var balance = originalBalance

result &= "Original_Balance: " & \$balance & "\n"

separatedInput.delete(0, 0)

for item in separatedInput:
let trimmed = split(item, " ")
let title = replace(trimmed, re"\W", "");
let amount = parseFloat(replace(trimmed, re"""[-!\$%^&*()_+|~=`{}\[\]:";'<>?,\/]""", ""))

balance = balance - amount
result &= trimmed & " " & title & " " & \$amount & " Balance " & \$balance & "\n"

let totalExpense = originalBalance - balance;

result &= "Total expense " & \$totalExpense & "\n"
result &= "Average expense " & \$(totalExpense / float(len(separatedInput) - 1))  & "\n"

if isMainModule:
const input = """1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;"""

echo parse(input)
`````` Doing a little catch up on challenges. Here's my solution in Go:

Enjoy the naming scheme :evil-smile:

``````type Checkbook struct {
bal      int64
cheques  []Cheque
totalExp int64
avgExp   int64
}

type Cheque struct {
no       int
category string
amount   int64
balance  int64
}

const scaling = 1000

var filter = regexp.MustCompile("[^a-zA-Z0-9 .\n]*")

// New parses a text listing of cheques
func New(chequeList string) (Checkbook, error) {
// remove unwanted runes
chequeList = filter.ReplaceAllString(strings.TrimSpace(chequeList), "")
lines := strings.Split(chequeList, "\n")
bal, ok := (&big.Float{}).SetString(strings.TrimSpace(lines))
if !ok {
return Checkbook{}, errors.Errorf("balance invalid %q", lines)
}
b, _ := bal.Mul(bal, (&big.Float{}).SetInt64(scaling)).Int64()
cb := Checkbook{
bal:     b,
cheques: make([]Cheque, len(lines)-1),
}
tot := int64(0)
for i, v := range lines[1:] {
cq, err := parseCheque(v)
if err != nil {
return Checkbook{}, err
}
cq.balance, b = b-cq.amount, b-cq.amount
cb.cheques[i] = cq
tot += cq.amount
}
cb.totalExp = tot
cb.avgExp = tot / int64(len(cb.cheques))
return cb, nil
}

// parseCheque from an entry line in the checkbook.
func parseCheque(entry string) (Cheque, error) {
entry = strings.TrimSpace(entry)
if strings.Count(entry, " ") < 2 {
return Cheque{}, errors.Errorf("cheque line invalid %q", entry)
}
fi, li := strings.Index(entry, " "), strings.LastIndex(entry, " ")
chNo, cat, amt := entry[:fi], strings.TrimSpace(entry[fi:li]), entry[li+1:]
no, err := strconv.Atoi(chNo)
if err != nil {
return Cheque{}, err
}
cq := Cheque{no: no, category: cat}
bal, ok := (&big.Float{}).SetString(amt)
if !ok {
return Cheque{}, errors.Errorf("amount invalid %q", amt)
}
cq.amount, _ = bal.Mul(bal, (&big.Float{}).SetInt64(scaling)).Int64()
return cq, nil
}

// String display of entire checkbook.
func (cb Checkbook) String() string {
b := &strings.Builder{}
fmt.Fprintf(b, "Original Balance: %0.2f\n", float64(cb.bal)/scaling)
for _, v := range cb.cheques {
v.Write(b)
fmt.Fprintln(b)
}
fmt.Fprintf(b, "Total Expenses: %0.2f\n", float64(cb.totalExp)/scaling)
fmt.Fprintf(b, "Average Expenses: %0.2f\n", float64(cb.avgExp)/scaling)
return b.String()
}

// String display of Cheque data.
func (cq Cheque) String() string {
return fmt.Sprintf("No: %d, Category: %s, Amount: %0.2f, Balance: %0.2f",
cq.no, cq.category, float64(cq.amount)/scaling, float64(cq.balance)/scaling)
}

// Write string display to writer.
func (cq Cheque) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "No: %d, Category: %s, Amount: %0.2f, Balance: %0.2f",
cq.no, cq.category, float64(cq.amount)/scaling, float64(cq.balance)/scaling)
}
`````` margo1993
``````package utils

import (
"fmt"
"regexp"
"strconv"
"strings"
)

type CheckbookLine struct {
checkNumber int
category string
checkAmount float64
}

func ProcessCheckbook(checkbook string) (string, error) {
lines := strings.Split(checkbook, "\n")

//Ignores all wrongly formatted lines
checkbookLines := parseCheckbookLines(lines[1:])

balance, e := findFloat(lines)
if e != nil {
return "", e
}

result := fmt.Sprintf("Original_Balance: %0.2f\n", balance)

totalExpense := 0.0
for _, checkbookLine := range checkbookLines {
totalExpense += checkbookLine.checkAmount

balance -= checkbookLine.checkAmount
result += fmt.Sprintf("%d %s %0.2f Balance %0.2f\n", checkbookLine.checkNumber, checkbookLine.category, checkbookLine.checkAmount, balance)
}

result += fmt.Sprintf("Total expense %0.2f\n", totalExpense)
checkbookLinesCount := len(checkbookLines)
if checkbookLinesCount > 0 {
result += fmt.Sprintf("Average expense %0.2f", totalExpense/float64(checkbookLinesCount))
}

return result, nil;
}

func parseCheckbookLines(lines []string) []CheckbookLine {
var checkbookLines []CheckbookLine
for _, line := range lines {

fields := strings.Fields(line)
if len(fields) < 3 {
continue
}

checkNumber, e := findInteger(fields)
if e != nil {
continue
}

category := findClearString(fields)
checkAmount, e := findFloat(fields)
if e != nil {
continue
}

checkbookLines = append(checkbookLines, CheckbookLine{checkNumber, category, checkAmount})
}
return checkbookLines
}

func findInteger(field string) (int, error) {
r := regexp.MustCompile("[0-9]+")
return strconv.Atoi(r.FindString(field))
}

func findClearString(field string) string {
r := regexp.MustCompile("[A-Za-z]+")
return r.FindString(field)
}

func findFloat(field string) (float64, error) {
r := regexp.MustCompile("[0-9.-]+")
return strconv.ParseFloat(r.FindString(field), 32)
}
`````` Ryan Smith

My JavaScript version:

``````/**
* Given a string containing checkbook transactions, format the transactions and calculate the running total
*/
function balanceCheckbook (checkbook) {
let balance = 0
let totalExpense = 0
let balancedCheckbook = ''

// Split the string into an array of transactions based on new lines.
const transactions = checkbook.split('\n')

// Loop over the transactions to construct the checkbook and calculate total expense.
transactions.forEach((transaction) => {
const sanitizedTransaction = sanitizeInput(transaction).split(' ')

// If this transaction only has one column, set it to be the starting balance. Otherwise, process a transaction.
if (sanitizedTransaction.length === 1) {
balance = sanitizedTransaction
balancedCheckbook += `Original_Balance: \${sanitizedTransaction} \n`
} else {
const transactionNumber = sanitizedTransaction
const description = sanitizedTransaction
const cost = sanitizedTransaction
const newBalance = (balance -= cost).toFixed(2)

totalExpense += parseFloat(cost)

// Format the transaction into a string.
balancedCheckbook += `\${transactionNumber} \${description} \${cost} Balance \${newBalance} \n`
}
})

balancedCheckbook += `Total expense \${totalExpense.toFixed(2)} \n`
balancedCheckbook += `Average expense \${(totalExpense / (transactions.length - 1)).toFixed(2)}`

return balancedCheckbook
}

/**
* Remove invalid characters from a transaction.
*/
function sanitizeInput (inputText) {
const removeSpecialCharactersRegex = /[^A-Z0-9\s.]/gi

return inputText.trim().replace(removeSpecialCharactersRegex, '')
}
``````
``````console.log(
balanceCheckbook(
`1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;`
)
)
``````

Output:

``````Original_Balance: 1233.00
125 Hardware 24.8 Balance 1208.20
123 Flowers 93.5 Balance 1114.70
127 Meat 120.90 Balance 993.80
120 Picture 34.00 Balance 959.80
124 Gasoline 11.00 Balance 948.80
123 Photos 71.4 Balance 877.40
122 Picture 93.5 Balance 783.90
132 Tires 19.00 Balance 764.90
129 Stamps 13.6 Balance 751.30
129 Fruits 17.6 Balance 733.70
129 Market 128.00 Balance 605.70
121 Gasoline 13.6 Balance 592.10
Total expense 640.90
Average expense 53.41
`````` peter279k

Here is my simple solution to parse balanced book string:

``````function balance(\$book) {
\$result = "Original Balance: ";
\$book = explode("\n", \$book);
if (\$book === "") {
\$totalDistance = sprintf("%.2f\n", (float)\$book);
\$index = 2;
} else {
\$totalDistance = sprintf("%.2f\n", (float)\$book);
\$index = 1;
}
\$result .= \$totalDistance;

\$totalExpense = 0.0;
\$currentDistance = (float)\$totalDistance;
\$currentCount = 0;
for(; \$index < count(\$book); \$index++) {
if (\$book[\$index] === "") {
continue;
}
\$currentCount += 1;
\$info = explode(' ', \$book[\$index]);
\$stringFormat = "%s %s %.2f Balance %.2f\n";
preg_match('/(\w+)/', \$info, \$matched);
\$info = \$matched;

preg_match('/(\d+).(\d+)/', \$info, \$matched);
\$info = (float)\$matched;
\$currentDistance = \$currentDistance - \$info;
\$result .= sprintf(\$stringFormat, \$info, \$info, (float)\$info, (float)\$currentDistance);
\$totalExpense += (float)\$info;
}

\$result .= sprintf("Total expense  %.2f\n", \$totalExpense);
\$result .= sprintf("Average expense  %.2f", (string)round(\$totalExpense / \$currentCount, 2));

return \$result;
}
`````` Valerio Narcisi

//TS
//TDD

``````
import checkbookBalancing from './checkbook-balancing';

describe('tdd checkbook balancing', () => {
const endOfLine = '\n';
test('Example Checkbook', () => {
const checkBook = `1000.00
125 Market 125.45
126 Hardware 34.95
127 Video 7.45
128 Book 14.32
129 Gasoline 16.10`;

const res = `Original_Balance: 1000.00\${endOfLine}125 Market 125.45 Balance 874.55\${endOfLine}126 Hardware 34.95 Balance 839.60\${endOfLine}127 Video 7.45 Balance 832.15\${endOfLine}128 Book 14.32 Balance 817.83\${endOfLine}129 Gasoline 16.10 Balance 801.73\${endOfLine}Total expense 198.27\${endOfLine}Average expense 39.65`;
expect(checkbookBalancing(checkBook)).toEqual(res);
});

test('Challenge Checkbook', () => {
const checkBook = `1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;`;

const res = `Original_Balance: 1233.00\${endOfLine}125 Hardware 24.8 Balance 1208.20\${endOfLine}123 Flowers 93.5 Balance 1114.70\${endOfLine}127 Meat 120.90 Balance 993.80\${endOfLine}120 Picture 34.00 Balance 959.80\${endOfLine}124 Gasoline 11.00 Balance 948.80\${endOfLine}123 Photos 71.4 Balance 877.40\${endOfLine}122 Picture 93.5 Balance 783.90\${endOfLine}132 Tires 19.00 Balance 764.90\${endOfLine}129 Stamps 13.6 Balance 751.30\${endOfLine}129 Fruits 17.6 Balance 733.70\${endOfLine}129 Market 128.00 Balance 605.70\${endOfLine}121 Gasoline 13.6 Balance 592.10\${endOfLine}Total expense 640.90\${endOfLine}Average expense 53.41`;
expect(checkbookBalancing(checkBook)).toEqual(res);
});
});
``````
``````
export default (str: string, endOfLine = '\n', separator = ' '): string | null => {
if (str === '') {
return null;
}

const initialState = {
originalBalance: null,
rows: '',
totalExpense: 0,
averageExpense: 0,
};

const round2 = num => Math.round(num * 100) / 100;

const res = str
.replace(/[^0-9A-Za-z\.\s]/g, '')
.split(endOfLine)
.map(val => val.trim().split(separator))
.reduce((prev, curr, i) => {
if (i === 0) {
return { ...initialState, ...{ originalBalance: round2(curr) } };
}
const totalExpense = round2(Number(prev.totalExpense + Number(curr)));
const averageExpense = round2(totalExpense / i);
return {
...prev,
...{
originalBalance: prev.originalBalance,
rows: `\${prev.rows}\${curr} \${curr} \${curr} Balance \${round2(
prev.originalBalance - prev.totalExpense - Number(curr),
).toFixed(2)}\${endOfLine}`,
totalExpense: totalExpense,
averageExpense: averageExpense,
},
};
}, initialState);

return `Original_Balance: \${res.originalBalance.toFixed(2)}\${endOfLine}\${
res.rows
}Total expense \${res.totalExpense.toFixed(2)}\${endOfLine}Average expense \${res.averageExpense.toFixed(2)}`;
};

`````` Here's my Groovy take on this:

``````class CheckBook {

static final DecimalFormat DF = new DecimalFormat('#.00', DecimalFormatSymbols.getInstance(Locale.US))
static final String LINE_SEPARATOR = '\n'
static final String PROPERTY_SEPARATOR = ' '

List<BookEntry> entries = []
Float initialBalance = 0f

CheckBook(String input) {
parse(input)
entries.sort{it.id}
}

void printOperations() {
println "Original_Balance: \${DF.format(initialBalance)}"
Float currentBalance = initialBalance
List<Float> expenses = []
entries.each { BookEntry b ->
currentBalance -= b.amount
expenses << b.amount
println "\$b Balance \${DF.format(currentBalance)}"
}
Float total = expenses.sum()
println "Total expense \${DF.format(total)}"
println "Average expense \${DF.format(total / expenses.size())}"
}

private void parse(String input) {
List<String> lines = input.split(LINE_SEPARATOR)
initialBalance = Float.valueOf(lines)
parseEntries(lines.drop(1))
}

private void parseEntries(List<String> lines) {
lines.each { String line ->
parseEntry(line)
}
}

private void parseEntry(String line) {
List<String> properties = line.split(PROPERTY_SEPARATOR)
.collect{sanitizeInput(it)}
BookEntry newEntry = new BookEntry(
id: properties.toInteger(),
name: properties,
amount: Float.valueOf(properties)
)
entries << newEntry
}

private String sanitizeInput(String toSanitize) {
}

class BookEntry {
Integer id
String name
Float amount

@Override
String toString() {
return "\$id \$name \${DF.format(amount)}"
}
}
}

``````

Call is made like the following:

``````String checkBookInput = """
1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;
""".trim().stripIndent()

CheckBook checkBook = new CheckBook(checkBookInput)
checkBook.printOperations()
``````

Interesting points that are Groovy-related:

• left shift operator to insert new element in a collection
• regex evaluation operator
• the good old Java DecimalFormat to format numbers at print time
• triple quotes multiline Strings official_dulin

Go:

``````import (
"bufio"
"fmt"
"log"
"regexp"
"strconv"
"strings"
)

var re = regexp.MustCompile(`(?mi)[^A-Za-z0-9\s\.]`)

func Balance(book string) string {
var r []string
book = re.ReplaceAllString(book, "")

var (
balance, total, average float64
err                     error
)
i := 0
for scanner.Scan() {
text := scanner.Text()
if text == "" {
continue
}
if i == 0 {
balance, err = strconv.ParseFloat(text, 32)
if err != nil {
log.Fatal(err)
}
r = append(r, fmt.Sprintf("Original Balance: %.2f", balance))
} else {
texts := strings.Split(text, " ")
cost, err := strconv.ParseFloat(texts, 32)
if err != nil {
log.Fatal(err)
}
text = fmt.Sprintf("%s %s %.2f", texts, texts, cost)
total += cost
balance = balance - cost
r = append(r, fmt.Sprintf("%s Balance %.2f", text, balance))
}
i++
}
average = total / float64(len(r)-1)
r = append(r, fmt.Sprintf("Total expense  %.2f", total), fmt.Sprintf("Average expense  %.2f", average))
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return strings.Join(r, "\n")
}
`````` Caleb Rudder

A little late to the party, but trying to work on a few skills. I thought I'd attempt to work on using closures a bit on this one:

``````const myCheckbook = checkbook(testData);
myCheckbook.balanceCheckbook();

function checkbook(input){
let userData = removeClutter(input);
let totalBalance = 0;
let totalExpenses = 0;
let expenseCounter = 0;
let averageExpense = 0;
function removeClutter(data){
let cleanData = data.replace(/([^a-zA-Z0-9\s\.])/g, '');
return cleanData.split('\n');
}

totalExpenses += expense;
totalBalance -= expense;
expenseCounter++;
averageExpense = totalExpenses / expenseCounter;
};

const balanceCheckbook = function(){
for(let i = 0; i<userData.length; i++){
if(i == 0){
totalBalance = userData;
console.log('Original_Balance: ' + totalBalance);
}else{
let line = userData[i].split(" ");
console.log(userData[i] + " Balance: " + totalBalance.toFixed(2));
}
}
console.log("Total Expenses: " + totalExpenses.toFixed(2));
console.log("Average expense: " + averageExpense.toFixed(2));
};

return{
userData: userData,
balanceCheckbook: balanceCheckbook
}
}
`````` Petr Damborský

First time seeing these challenges. I don't know If there are any special requirements (e.g.: Should the code be production-ready? Will it be shared in a team? etc.) so I wrote just a simple solution for the task.

``````const input = `
1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;
`;

let output = 'Original_Balance: ';

const entries = input.split('\n').filter(e => e);
let balance = parseFloat(entries.splice(0, 1).trim());
output += balance;

let spent = 0;
entries.forEach(e => {
const data = e.split(' ').map(d => {
if (isNaN(parseFloat(d))) {
return d.replace(/[^\w\s.]/gi, '');
} else {
return parseFloat(d);
}
});
const [check, category, amount] = [...data];
spent += amount;
output += `\n\${check} \${category} \${amount} Balance \${(balance-spent).toFixed(2)}`;
});

output += `
Total expense \${spent.toFixed(2)}
Average expense \${(spent/entries.length).toFixed(2)}
`;

console.log(output);
`````` ``````function balanceCheckbook(checkbook = '') {
if(typeof(checkbook) !== 'string')
throw new Error('input must be a string');

checkbook = checkbook.trim();

let originalBalance = 0;
let entries = checkbook.split('\n');
let balance = originalBalance;

// exclude the orginalBalance at the top
let totalEntries = entries.length - 1;

for(index in entries) {
let entry = entries[index];

// clean the line
entry = entry.replace(/[^\.\w\d ]+/g, '');

// parse the line
if(index == 0)
{
originalBalance = parseFloat(entry);
balance = originalBalance;
console.log(`Original_Balance: \${originalBalance.toFixed(2)}`);
}
else
{
let section = entry.split(' ');
let expenseString = section;
let expense = parseFloat(expenseString);

balance -= expense;
console.log(`\${entry} Balance \${balance.toFixed(2)}`);
}
}

// compute summaries
let totalExpense = originalBalance - balance;
let averageExpense = totalExpense/totalEntries;

// print summary
console.log(`Total expense \${totalExpense.toFixed(2)}`);
console.log(`Average expense \${averageExpense.toFixed(2)}`)
}
const input = `
1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;
`;
balanceCheckbook(input);
`````` Mohamed ELIDRISSI

My solution :)
Also available in my github repo: github.com/MohamedElidrissi/Daily-...

``````function balanceCheckbook(checkbook) {
checkbook = checkbook.replace(/[^A-Za-z0-9\.\n ]/g, '');
const reports = checkbook.match(/[0-9]+\s[A-Za-z]+\s[0-9]+(\.[0-9]{1,2})?/g);
const expenses = checkbook.match(/ [0-9]+(\.[0-9]{1,2})?/g);
const totalExpenses = expenses
.reduce((p, c) => parseFloat(p) + parseFloat(c))
.toFixed(2);
let balance = checkbook.match(/^[0-9]+(\.[0-9]{1,2})?/g);
let output = `Original Balance: \${balance}\n`;

reports.forEach((report, index) => {
balance -= expenses[index];
output += `\${report} Balance \${balance.toFixed(2)}\n`;
});

output += `Total expense \${totalExpenses}\n`;
output += `Average expense \${(totalExpenses / expenses.length).toFixed(2)}`;

return output;
}
`````` Ilyes Chouia • Edited on

PHP:

``````
function report(\$string) {

\$data = explode(PHP_EOL, \$string);
\$data = array_map(function(\$line){
return preg_replace('/[^\w\s.]+/', '', \$line);
}, \$data);

\$balance = floatval(array_shift(\$data));
\$result = 'Original Balance: ' . number_format(\$balance, 2) . "<br>";
\$expenses = [];
sort(\$data);
foreach (\$data as \$index => \$line) {
\$parts = explode(' ', \$line);
foreach (\$parts as \$line_segment) {
if (\$line_segment != end(\$parts)) {
\$result .= "\$line_segment ";
}
}
\$expenses[] = floatval(end(\$parts));
\$balance -= end(\$expenses);
\$result .= number_format(end(\$parts), 2) . ', Balance: ' . number_format(\$balance, 2) . "<br>";
}

\$total_expenses = array_sum(\$expenses);
\$result .= 'Total expenses: ' . number_format(\$total_expenses, 2) . "<br>";
\$result .= 'Average expense: ' . number_format(\$total_expenses / count(\$expenses), 2) . "<br>";
return \$result;
}

\$text = '1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;';

echo report(\$text);

``````