Exercise: Solve this problem in Go. Write code and test cases too.Basic Number filtering exercise
Time to complete: 1 day
What will you learn:
- How to write unit test cases.
- How to design small functions for specific use cases.
- Code reuse: reusing
oddfunctions in Stories 4, 5 and 6, reusing various functions in stories 7 and 8.
- How to compose multiple functions together? How to form a chain of functions?
- Identify abstractions using higher order functions (functions that accept or return functions). i.e. using functions as first-class citizens.
- Understand Go’s generics and whether and how these can be used to write generic methods that can operate on any type.
How to evaluate your solution:
- Is your code following Go naming conventions - for files, functions, variables, etc?
- Have you written unit tests for each story? Are you “listening to the tests” to see if you can reuse any code during the refactor phase?
- What’s the code coverage for your code? Why the code can or cannot have 100% code coverage?
- What would you need to change in the program if we wanted to create a plugin model where users can dynamically provide the conditions? This is the crux of the program. Can you identify the right abstractions for Story 7 and Story 8?
- How will you model the conditions in the code? Is there a standard Go construct for this already? If yes, what is it called, and how does it work? If not, what will you call this construct, and how can you use it generically?
- Have you tried running the program with a different range of inputs, for example: 50 to 100? Does it work as expected?
- How are you allowing the users to specify various conditions for filtering? In the future, if we add more filtering conditions (e.g., all numbers that are perfect squares), what code will you change? Can you make sure your code allows “plug-ins” for such conditions so that any new condition doesn’t require in code change in the core logic?
- The program specifies two very similar filters
multiple of 3and
multiples of 5. Have you created separate functions for each of these? If we were to ask for a
multiple of 7, would you write another function? Can you write a
multiple of Nfunction that accepts
Nas a parameter and returns a function? i.e. a higher order function?
- Write implementation for stories 7 and 8 with Go’s generics to work with any numeric data types.
- Don’t look at the solution unless you have you. Otherwise, you’re not getting much value from the bootcamp.
I know what I am doing; stop patronising me
If you say so 🤷, here’s the sample implementation: