Refactoring legacy loops into clean, expressive standard algorithms.
These exercises train your ability to replace raw loops with the correct standard algorithm (std::find_if, std::count_if, std::accumulate, std::sort, std::partition, std::unique, etc.).
Each exercise includes:
- a small piece of legacy code
- unit tests (GoogleTest)
- a TODO hint pointing toward the appropriate algorithm
- a link to run the code directly on Compiler Explorer
All tests must continue to pass after your refactor.
There are no trick questions: only the implementation should change, not the behavior.
- Pick any exercise below.
- Open the CE link.
- Run the code (CTRL+K if it doesn't start automatically) and confirm that all tests pass.
- Refactor the legacy loop to use a suitable standard algorithm.
- Rebuild and rerun the tests. All tests must pass.
- Commit your solution locally if you want to keep a record.
CE link: https://compiler-explorer.com/z/P5qd8hjsc
File: exercises/ex01_find_first_dead.cpp
CE link: https://compiler-explorer.com/z/qTWseobfM
File: exercises/ex02_count_poisoned.cpp
CE link: https://compiler-explorer.com/z/Gj66nGcE3
File: exercises/ex03_remove_offscreen.cpp
CE link: https://compiler-explorer.com/z/GGrYrhxGb
File: exercises/ex04_apply_damage.cpp
CE link: https://compiler-explorer.com/z/cTEjT5o5r
File: exercises/ex05_total_weight.cpp
CE link: https://compiler-explorer.com/z/Y7scnvjGe
File: exercises/ex06_sort_by_distance.cpp
CE link: https://compiler-explorer.com/z/qad6KToee
File: exercises/ex07_row_walkable.cpp
CE link: https://compiler-explorer.com/z/aoodb83Ye
File: exercises/ex08_partition_near.cpp
CE link: https://compiler-explorer.com/z/j5ased9YW
File: exercises/ex09_unique_frames.cpp
CE link: https://compiler-explorer.com/z/v9E4G9cq1
File: exercises/ex10_low_hp_names.cpp
CE link: https://compiler-explorer.com/z/oqcW7aEdT
File: exercises/ex11_shuffle_deck.cpp