| Course | BU 451 Programming for Business Analytics |
|---|---|
| Unit | Unit 4 |
| Paper type | Control flow and functions |
| Length | About 810 words, 5 pages |
| Format | APA 7 student paper |
| School | Herzing University |
| Program | BS in Business Analytics |
| Updated | September 2026 |
Free sample paper for BU 451 Unit 4
From 142 Lines to 88, and Every Function Checked Against an Answer Already Known: Control Flow and Functions in a Composite Paddle Rental Company's Analysis Code
Student Name
Business, Herzing University
BU 451: Programming for Business Analytics
Instructor Name
February 25, 2026
From 142 Lines to 88, and Every Function Checked Against an Answer Already Known: Control Flow and Functions in a Composite Paddle Rental Company's Analysis Code
Why the Scripts Needed Restructuring
After two units, the analysis code for Riverbend Paddle Co., the Chattanooga river rental business invented for this course, worked but repeated itself. The inspection script contained four nearly identical blocks, one for each data file. The cleaning script parsed start times and return times with two copies of the same six lines. And an early draft of the site summary computed revenue for each launch site in a separate block, three times over. Repetition is where errors hide, since a correction made to one copy is easily missed in the others. This unit turns each repeated block into a named function with declared inputs and a single returned value, keeps a loop only where the repetition is real, and checks every function against a case whose correct answer is known in advance.
Wilson et al. (2014) recommend writing code for people to read, putting repeated logic into functions, and testing that code with small cases whose results can be worked out by hand. The restructured cleaning script shrank from 142 lines to 88, and the four inspection blocks became one function called four times.
The Functions
Five functions now live in a single module, functions.py, which every numbered script imports at its top, so there is exactly one copy of each piece of logic.
| Function | Inputs | Returns | Replaces |
|---|---|---|---|
| inspect_table(df, name, required) | A table, its name, the columns it must contain | A one-row summary per column: type, missing, distinct | Four copied inspection blocks |
| clean_price(values) | A column of prices, possibly with dollar signs | The same values as numbers | An inline conversion in the cleaning script |
| to_hours(start, end) | Two columns of clock times | Duration in hours, missing where either time is missing | Two copies of time parsing |
| season_week(dates) | A column of dates | Week of the season, 1 for the week starting April 1 | Repeated date arithmetic in two scripts |
| site_summary(rentals) | The cleaned rental table | Rentals, revenue and median duration by site | Three copied per-site blocks |
Each function has one job and returns one value; none prints, reads files or changes its inputs. Printing and saving stay in the numbered scripts, which call the functions and decide what to do with the results. That separation means a function can be tested with a few made-up values, without loading the season's data at all. It also makes the numbered scripts easier to read: a reader of the site summary script sees one line calling site_summary and one line saving its result, rather than forty lines of filtering and adding, and can open the function only if the detail matters. Each function carries a two-line comment stating what it expects and what it returns.
Where a Loop Stays and Where It Goes
Two kinds of repetition remained, and they were treated differently. The inspection of four files is real repetition: the same steps applied to four separate tables that cannot be combined. A loop over a list of file names now calls inspect_table once per file, which reads naturally and would handle a fifth file with no new code.
The per-site revenue blocks were not real repetition. They applied the same calculation to groups within one table, which is exactly what a grouped operation does. The draft looped over the three site codes, filtered the table each time and added up revenue. The function site_summary replaces that with a single grouped calculation by site code, which is shorter and cannot miss a site if a fourth launch site is added next season. McKinney (2022) describes this split-apply-combine approach as the standard way to summarize groups in pandas, and a loop over groups is usually a sign that a grouped operation has been overlooked.
Checking Each Function Against a Known Answer
Every function is checked by a short file, check_functions.py, that feeds it values whose correct result is known and stops with a message if the result differs. These checks run automatically as the first step of every full run, before any analysis script, so a broken function is caught before it touches the season's data.
| Function | Input used in the check | Known correct answer |
|---|---|---|
| clean_price | "$45", "45", "0" | 45.0, 45.0, 0.0 |
| to_hours | Start 09:30, return 12:00; start 14:00, return missing | 2.5; missing |
| season_week | April 1, 2025; April 8, 2025; October 31, 2025 | 1; 2; 31 |
| site_summary | A four-row table: two rentals at CHA-1 for $40 and $60, one at CHA-2 for $50, one comp at CHA-2 for $0 | CHA-1: 2 rentals, $100; CHA-2: 2 rentals, $50 |
| inspect_table | A three-column table with one missing value | Missing count of 1 in the right column |
The checks earned their place the first time they ran. The first version of to_hours returned zero rather than missing when the return time was absent, because subtracting a missing time from a present one had been handled by filling the gap with the start time. The check for a missing return caught it, and the function was corrected before the utilization analysis could count 263 rentals as lasting no time at all. These checks are not a full testing framework, but they are testing in the sense that matters: each states an expected result in advance and fails loudly when the code disagrees.
What Did Not Become a Function
Not every block was turned into a function. The seven cleaning fixes remain as separate, commented steps in the cleaning script, because each is a one-time decision about this data set, with its own count and reason, and hiding them inside a general function would make the cleaning log harder to read. Wilson et al. (2017) caution against building general tools before they are needed, and the test used here was simple: a block becomes a function when it is used more than once, or when checking it alone would catch errors the full script would hide. Riverbend Paddle Co. and every figure in its data are composites for this course.
References
McKinney, W. (2022). Python for data analysis: Data wrangling with pandas, NumPy, and Jupyter (3rd ed.). O'Reilly Media.
Wilson, G., Aruliah, D. A., Brown, C. T., Chue Hong, N. P., Davis, M., Guy, R. T., Haddock, S. H. D., Huff, K. D., Mitchell, I. M., Plumbley, M. D., Waugh, B., White, E. P., & Wilson, P. (2014). Best practices for scientific computing. PLOS Biology, 12(1), Article e1001745. https://doi.org/10.1371/journal.pbio.1001745
Wilson, G., Bryan, J., Cranston, K., Kitzes, J., Nederbragt, L., & Teal, T. K. (2017). Good enough practices in scientific computing. PLOS Computational Biology, 13(6), Article e1005510. https://doi.org/10.1371/journal.pcbi.1005510
BU 451 Unit 4 instructions, in plain terms
BU 451 Unit 4 asks students to improve a script using functions and appropriate control flow. Typical requirements include identifying repeated code, converting it into functions with clear inputs and outputs, using loops only where repetition is genuine, and checking that the functions produce correct results. Many instructors ask how many functions are appropriate and whether simple checks count as testing. Students should explain why each function exists and what it replaced. Vectorized or grouped operations are often preferred to loops over rows or groups. The submission typically includes the revised code, the checks and a written explanation in APA 7, often with before and after line counts to show the effect.
Inside the BU 451 Unit 4 example
The paper begins by identifying three kinds of repetition in the earlier scripts and why repetition hides errors. A table lists five functions with their inputs, returns and what each replaced. A paragraph explains the rule that functions return values while scripts print and save. A section contrasts the loop kept for inspecting files with the per-site loop replaced by a grouped calculation, with a citation on split-apply-combine. A table lists each function's known-answer check. A paragraph tells how a check caught a bug that would have counted 263 rentals as lasting no time. The final section explains why the seven cleaning fixes did not become a function.
BU 451 Unit 4 rubric: what full points look like
Rubrics for functions and control flow usually reward sensible decomposition, clear interfaces, appropriate use of loops, evidence of correctness and readable code, plus written explanation. Decomposition earns full credit when repeated logic becomes one function used in several places. Interfaces score highest when inputs are explicit and one value is returned without side effects. Loop use is judged on whether loops appear only where repetition is real, with grouped operations used for groups within a table. Correctness rewards checks against known answers that run before analysis. Readability earns points for names and comments that explain intent. Instructors also credit restraint, since not every block should become a function.
Common BU 451 Unit 4 mistakes, and how to avoid them
Function exercises often lose points by wrapping code in functions that still read files, print and change global variables, which makes them hard to test. Another common problem is creating many tiny functions used only once. Students frequently keep loops over rows or groups where a grouped calculation would be shorter and safer. Some write functions but never check them against a case with a known answer. Others check only the typical case and miss missing values or empty inputs. Many explanations list functions without saying what repetition each removed. Finally, reusing functions written for an employer can carry company code into a class submission; writing fresh functions for the course data avoids that problem.
Get a BU 451 Unit 4 example written to your instructions
Give us the unit instructions, the BU 451 rubric from your classroom and the script you are being asked to rework. The custom example keeps both versions side by side, declares every input, returns instead of printing, labels each branch by the case it covers and closes with checks against known answers. First custom sample free, back in 24 to 48 hours.
More BU 451 papers and business analytics samples
- BU 451 Unit 1: Environment and setup exercise
- BU 451 Unit 2: Data import and inspection script
- BU 451 Unit 3: Cleaning and transformation in code
- BU 451 Unit 5: Aggregation and joins
- BU 451 Unit 6: Analysis script with output
- BU 451 Unit 7: Error handling and validation
- BU 451 Unit 8: Reproducible analysis submission
- BU 351 Unit 4: Exploratory analysis
- BU 407 Unit 4: Optimization model summary
- BU 453 Unit 4: Schema implementation
- BU 454 Unit 4: Time series visualization
BU 451 Unit 4 questions, answered
How many functions is the right number?
Enough that each one does a single describable thing, and no more than that. If naming a function requires the word and, it is probably two. A submission with four well separated functions and a readable main section usually scores better than one with a dozen, because a marker has to hold the whole file in mind while reading it.
Do the checks at the end count as testing?
They are enough for an undergraduate submission and they are what most rubrics here are looking for. A few calls with inputs you can reason about, each with the expected result stated in a comment, show that the function was verified rather than hoped over. If your section names a testing framework, use it; where nothing is named, the plain calls read perfectly well.
I have written functions at work. Can I reuse them here?
Code written on the clock generally belongs to the employer who paid for it, and that is a question for your contract rather than for us. Even where the logic is ordinary, a function carrying internal column names, business rules or system quirks describes the company in a document that goes to a school. Write the submission against the case the assignment gives you.
Where can I find a free BU 451 Unit 4 sample paper?
You can read the complete BU 451 Unit 4 control flow and functions example above at no cost, including the function table, the checks and margin notes. For your own scripts, email the prompt and a custom example follows free, generally within 48 hours.
Do simple checks count as testing in BU 451 Unit 4?
Yes, if each states an expected result in advance and fails loudly when the code disagrees. The sample's short checks caught a real bug before the utilization analysis ran.