Complete Implementation of Sets
Let's look at the complete code implementation of programs on sets.
Complete implementation of set
In this lesson, we’ve added the complete code that we’ve covered so far on different problems on sets (both with and without functions).
Without functions
The complete code in a single code editor is given below. Skim through this and see how complex the implementation of all these tasks would have been to follow if we had not used functions.
Problems with the above implementation
Congratulations! You have successfully implemented the different programs regarding sets with the help of several nested loops. In programs, where we did not make functions, you must have noticed the following problems:
- We were repeating some of the tasks with different data, e.g., printing the arrays, sorting the arrays, searching in arrays, computing the set difference of the two sets, and so on. This repetition must be avoided. We overcome this issue when we implement the same using functions.
- This repetition always wastes a lot of time as one needs to make the entire effort again with a similar problem with different inputs.
- It is challenging to extend this type of code because it becomes unmanageable due to its huge length.
- It can be hard to debug if there is a logical error somewhere in the code.
With functions
Let’s look at the complete code. You may see how all the repetition of the code in the above playground is completely avoided through modularisation.
Pros of using functions
From the 2nd implementation, we have the following very important points,
- Modularized code looks much cleaner.
- It is much more manageable.
- Functional implementation has significantly reduced redundancies in the code.
- While coding such a huge program, errors arising are a natural occurrence. If the code is in modularized/functional form, it is much more easily debugged.
- Modularized code is considered a great precious programming skill. Without it, you cannot work with other programmers because your code will not be usable by others.