name = "currency_converter" category = "pipeline" mode = "convert" description = """ A currency converter that reads conversion requests from stdin. Each line has the format: AMOUNT FROM TO RATE - AMOUNT: a decimal number (e.g., 12.35) - FROM: 3-letter currency code - TO: 3-letter currency code - RATE: the exchange rate from FROM's base to TO's base Some currencies are pegged to others at fixed rates: BAM is pegged to EUR at 1.95583 BMD is pegged to USD at 1.0 BND is pegged to SGD at 1.0 DJF is pegged to USD at 177.721 PAB is pegged to USD at 1.0 When a pegged currency is involved, the conversion must account for the peg coefficient. The formula is: result = amount * (rate / coef_from) * coef_to where coef is the peg ratio (1 if not pegged). Output one line per input: "AMOUNT FROM = RESULT TO" with RESULT computed using bc with scale=2. For invalid lines (wrong field count or non-numeric amount), output "ERROR: ". """ bash_source = ''' #!/bin/bash pegged_to() { case "$1" in BAM) echo "EUR:1.95583" ;; BMD) echo "USD:1.0" ;; BND) echo "SGD:1.0" ;; DJF) echo "USD:177.721" ;; PAB) echo "USD:1.0" ;; *) echo "NONE:1" ;; esac } while IFS= read -r line || [[ -n "$line" ]]; do # Skip empty lines [[ -z "$line" ]] && continue # Parse fields set -- $line if [[ $# -ne 4 ]]; then echo "ERROR: $line" continue fi amount=$1 from=$2 to=$3 rate=$4 # Validate amount is numeric if [[ ! "$amount" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then echo "ERROR: $line" continue fi # Validate rate is numeric if [[ ! "$rate" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then echo "ERROR: $line" continue fi # Get peg info peg_from=$(pegged_to "$from") coef_from=$(echo "$peg_from" | cut -d: -f2) peg_to=$(pegged_to "$to") coef_to=$(echo "$peg_to" | cut -d: -f2) # Calculate: result = amount * (rate / coef_from) * coef_to result=$(echo "scale=8; $amount * ($rate / $coef_from) * $coef_to" | bc) result=$(printf "%.2f" "$result") echo "$amount $from = $result $to" done ''' [[test_cases]] description = "Standard conversion with direct rate" stdin = """100 USD EUR 0.92 50 GBP JPY 188.50""" expected_stdout = """100 USD = 92.00 EUR 50 GBP = 9425.00 JPY""" [[test_cases]] description = "Pegged currency conversions" stdin = """100 BAM USD 1.08 200 BMD EUR 0.92 50 USD DJF 1.0""" expected_stdout = """100 BAM = 55.22 USD 200 BMD = 184.00 EUR 50 USD = 8886.05 DJF""" [[test_cases]] description = "Invalid input lines" stdin = """abc EUR USD 0.92 100 USD 100 EUR USD 0.85""" expected_stdout = """ERROR: abc EUR USD 0.92 ERROR: 100 USD 100 EUR = 85.00 USD""" [[test_cases]] description = "Pegged-to-pegged conversion" stdin = "100 BAM BMD 1.08" expected_stdout = "100 BAM = 55.22 BMD"