DEV Community

loading...

Discussion on: Daily Challenge #4 - Checkbook Balancing

Collapse
ganderzz profile image
Dylan Paulus

Nim

import strutils
import sequtils
import re

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

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

  separatedInput.delete(0, 0)

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

    balance = balance - amount
    result &= trimmed[0] & " " & 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)