Closed
Description
Improve Binary Search Exercise: Rename Variable and Add Guard Validation
Problem Statement
The current Binary Search exercise uses a variable value, initialized with the -v option, without explicit handling in the code. This can confuse external readers of published solutions, as the source of this variable isn’t immediately clear.
To improve clarity and robustness:
1. Rename the value variable to target, making its purpose in the binary search more intuitive.
2. Add a test case that passes an empty parameter and expects a corresponding error message, encouraging students to add guard validation.
Proposed Changes
Rename value to target
- Update all references in the example implementation.
- Update test cases to use target instead of value.
Add Guard Validation Test Case
- Add a new test case to ensure students validate the presence of the variable.
Example:
awk -v target="" -f binary_search.awk input_file
Expected output (to stderr):
Error: 'target' variable not set.
Update the example implementation to include a BEGIN block for guard validation:
BEGIN {
if (target == "") {
print "Error: 'target' variable not set." > "/dev/stderr"
exit 1
}
FS = ","
}
Deliverables
- Updated example implementation with the renamed variable and guard validation.
- Updated test cases to reflect the new variable name and include the guard validation check.