Skip to content

ulfben/algorithms-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Modern C++ Algorithms Workshop

Refactoring legacy loops into clean, expressive standard algorithms.

Goal

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.

Quick start

  1. Pick any exercise below.
  2. Open the CE link.
  3. Run the code (CTRL+K if it doesn't start automatically) and confirm that all tests pass.
  4. Refactor the legacy loop to use a suitable standard algorithm.
  5. Rebuild and rerun the tests. All tests must pass.
  6. Commit your solution locally if you want to keep a record.

Exercises

Exercise 1: Find first dead enemy

CE link: https://compiler-explorer.com/z/P5qd8hjsc
File: exercises/ex01_find_first_dead.cpp

Exercise 2: Count poisoned enemies

CE link: https://compiler-explorer.com/z/qTWseobfM
File: exercises/ex02_count_poisoned.cpp

Exercise 3: Remove off-screen bullets

CE link: https://compiler-explorer.com/z/Gj66nGcE3
File: exercises/ex03_remove_offscreen.cpp

Exercise 4: Clamp health after damage

CE link: https://compiler-explorer.com/z/GGrYrhxGb
File: exercises/ex04_apply_damage.cpp

Exercise 5: Total weight of inventory

CE link: https://compiler-explorer.com/z/cTEjT5o5r
File: exercises/ex05_total_weight.cpp

Exercise 6: Sort enemies by distance

CE link: https://compiler-explorer.com/z/Y7scnvjGe
File: exercises/ex06_sort_by_distance.cpp

Exercise 7: Walkable row / lava detection

CE link: https://compiler-explorer.com/z/qad6KToee
File: exercises/ex07_row_walkable.cpp

Exercise 8: Partition near enemies

CE link: https://compiler-explorer.com/z/aoodb83Ye File: exercises/ex08_partition_near.cpp

Exercise 9: Remove consecutive duplicate frames

CE link: https://compiler-explorer.com/z/j5ased9YW
File: exercises/ex09_unique_frames.cpp

Exercise 10: Gather names of low HP enemies

CE link: https://compiler-explorer.com/z/v9E4G9cq1
File: exercises/ex10_low_hp_names.cpp

Exercise 11: Shuffle deck

CE link: https://compiler-explorer.com/z/oqcW7aEdT
File: exercises/ex11_shuffle_deck.cpp

About

Workshop: refactoring legacy loops into clean, expressive standard algorithms

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages