Budget Planner
Part of the Logic & Flow section of Coddy's R journey. Lesson 63 of 64.
Challenge
HardComplete budget(lines). The first line is the header month,type,amount and every other line is one entry, such as 2024-01,income,3000. An entry is valid when its type is income or expense and its amount is a number above 0; skip every other entry.
Return one line per month in the valid entries, in order, 2024-01: income 3000.00, spent 1250.50, saved 1749.50, where saved is income minus spending (a month without income or spending counts 0 for it). Then add Best month: with the month that saved the most (the first one when several tie) and Skipped: with the number of skipped entries.
When no entry is valid, return only No valid entries and the Skipped line.
The supplied code passes every input line to budget() and prints each returned line.
Try it yourself
budget <- function(lines) {
# Write your code here
character(0)
}
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
cat(budget(input), sep = "\n")
All lessons in Logic & Flow
1Strings In Depth
Substrings with substr()Formatting with sprintf()Splitting and JoiningSearching StringsReplacing TextRecap - Username Builder4Matrices
Creating MatricesIndexing MatricesRow and Column SummariesMatrix ArithmeticRecap - Seating Chart2Key-Value Lookups
Named Vector LookupsChecking KeysAdding and Removing KeysLooping Over NamesRecap - Stock Desk3Sets and Counting
Unique ValuesSet OperationsMembership TestsCounting with table()Recap - Event Guests6Functions as Values
Anonymous FunctionsPassing FunctionsReturning FunctionsClosures with StateRecap - Discount Rules9Data Frames
Creating Data FramesColumns and RowsFiltering RowsAdding and SortingRecap - Sales Report12Project - Expense Tracker
Recording ExpensesTotal SpendingPractice on your own: Online R compiler