Services

Resources

Company

📊

Basic Number filtering exercise

This is a basic number-filtering exercise. To make the most of this exercise, look at only one story at a time, complete it, and move on to the next story. Try to do Test Driven Development (TDD) for this problem, if possible. If not TDD, at least write the test cases after writing the code - input and expected outputs are given in each story.

Story 1

Given a list of integers, write a program to return only the even numbers from this list.

Sample Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sample Output: 2, 4, 6, 8, 10

Story 2

Given a list of integers, write a program to return only the odd numbers from this list.

Sample Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sample Output: 1, 3, 5, 7, 9

Story 3

Given a list of integers, write a program to return only the prime numbers from this list.

Sample Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sample Output: 2, 3, 5, 7

Story 4

Given a list of integers, write a program to return only the odd prime numbers from this list.

Sample Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sample Output: 3, 5, 7

Story 5

Given a list of integers, write a program to return only the even and multiples of 5 from this list.

Sample Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Sample Output: 10, 20

Story 6

Given a list of integers, write a program to return only the odd and multiples of 3 greater than 10 from this list.

Sample Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Sample Output: 15

Story 7

Given a list of integers, and a set of conditions (odd, even, greater than 5, multiple of 3, prime, and many more such custom conditions that may be dynamically defined by user), write a program to return only the integers from the given list that match ALL the conditions.

Sample Input:
A list containing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Conditions specified using a set of functions: odd, greater than 5, multiple of 3
Sample Output: 9, 15

Sample Input:
A list containing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Conditions specified using a set of functions: even, less than 15, multiple of 3
Sample Output: 6, 12

Story 8

Given a list of integers, and a set of conditions (odd, even, greater than 5, multiple of 3, prime, and many more such custom conditions that may be dynamically defined by user), write a program to return only the integers from the given list that match ANY of the conditions.

Sample Input:
A list containing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Conditions specified using a set of functions: prime, greater than 15, multiple of 5
Sample Output: 2, 3, 5, 7, 10, 11, 13, 15, 16, 17, 18, 19, 20

Sample Input:
A list containing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Conditions specified using a set of functions: less than 6, multiple of 3
Sample Output: 1, 2, 3, 4, 5, 6, 9, 12, 15, 18

Expectations

  • Write small modular functions (e.g. odd, even, prime, etc) to check if a particular number is odd, even or prime. These functions should take input a single number and return a bool.

  • Test-drive the code, i.e. write test cases before you write the implementation. Writing tests first will help you figure out function signature and edge cases, if any.

  • Identify expected input and output for this program. How will you accept the inputs to the program (e.g. via tests, or stdout or file) and where will the program produce the output (stdout, file, etc)?

  • Start by creating separate functions for each of the test cases.

  • Try to identify the common parts of the program - looping on input array and if, some set of conditions match, then add the number in the output array. Think how you will abstract this into some common structure.

  • Implement orchestration functions called “all” and “any” for Story 7 and 8. Does Go have such functions in collections library already? If yes, what are those and how do these work? If no, can you write such functions using Go’s generics? This way, the functions can work on any types, not just ints.