title-img


C++ Class Templates

A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments. Here is an example of a class, MyTemplate, that can store one element of any type and that has just one member function divideBy2, which divides its value by 2. template <class T> class MyTemplate { T element; public: MyTemplate (T arg) {element=arg;}

View Solution →

Introduction to Sets Python

A set is an unordered collection of elements without duplicate entries. When printed, iterated or converted into a sequence, its elements will appear in an arbitrary order. Example >>> print set() set([]) >>> print set('HackerRank') set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R']) >>> print set([1,2,1,2,3,4,5,6,0,9,12,22,3]) set([0, 1, 2, 3, 4, 5, 6, 9, 12, 22]) >>> print set((1,2,3,4,5,5)) set([1, 2, 3, 4, 5]) >>> print set(set(['H','a','c','k','e','r','r','a','n','k'])) se

View Solution →

Preprocessor Solution C++

Preprocessor directives are lines included in the code preceded by a hash sign (#). These lines are directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements. #define INF 10000000 if( val == INF) { //Do something } After the preprocessor has replaced the directives, the code will be if( val == 10000000) { //Here INF is replaced by the value with

View Solution →

Eye and Identity Python

identity The identity tool returns an identity array. An identity array is a square matrix with all the main diagonal elements as 1 and the rest as 0. The default type of elements is float. import numpy print numpy.identity(3) #3 is for dimension 3 X 3 #Output [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] eye The eye tool returns a 2-D array with 1's as the diagonal and 0's elsewhere. The diagonal can be main, upper or lower depending on the optional parameter k. A positive

View Solution →

Operator Overloading C++

Classes define new types in C++. Types in C++ not only interact by means of constructions and assignments but also via operators. For example: int a=2, b=1, c; c = b + a; The result of variable c will be 3. Similarly, classes can also perform operations using operator overloading. Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax

View Solution →