This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Map Function: | |
# Syntax: map(<function>, <iterable-1>, <iterable-2>, ...) iterable = List/Tuple/range() | |
# It return an iterator that applies function to every item of iterable, yielding the results. | |
# If additional iterable arguments are passed, function must take that many arguments and function is applied to the items from all iterables in parallel. | |
# With multiple iterables, the iterator stops when the shortest iterable is exhausted. | |
List = [4,3,2,1] | |
SquaredList = list(map(lambda x: x**2, List)) # lambda Anonymous Function is usually used with map(). | |
print('List: ', List, 'SquaredList: ', SquaredList) # It's not mandatory to use lambda function with map(), we can pass any user defined function also. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// accumulate example | |
#include <iostream> // std::cout | |
#include <functional> // std::minus | |
#include <numeric> // std::accumulate | |
int myfunction (int x, int y) {return x+2*y;} | |
struct myclass { | |
int operator()(int x, int y) {return x+3*y;} | |
} myobject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference: https://www.cplusplus.com/reference/algorithm/transform/ | |
// transform algorithm example | |
#include <iostream> // std::cout | |
#include <algorithm> // std::transform | |
#include <vector> // std::vector | |
#include <functional> // std::plus | |
int op_increase (int i) { return ++i; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void merge(int arr[], int left, int mid, int right) | |
{ | |
int i, j, k; | |
// create 2 sub arrays, left and right | |
int leftSubArrSize = mid - left + 1; | |
int rightSubArrSize = right - mid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Bubble Sort: | |
Sorting is done by: | |
Comparing Adjacent items and | |
Swapping them if they are not in order. | |
Algorithm repeatedly passes through array and Swaps element to make them in order. | |
When there is NO Swap in an iteration, that's end of Sorting. | |
This way, usually Array is sorted in 2nd Last iteration itself, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A powerful technique that arises from using Virtual Inheritance is to delegate a method from a class in another class | |
// by using a Common Abstract Base Class. This is also called "Cross Delegation". | |
#include<iostream> | |
using namespace std; | |
class GrandParent { // Abstract Base class common to Parent1 & Parent2 | |
// Data members of person | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class GrandParent { | |
// Data members of GrandParent | |
protected: | |
int grandParentAge; | |
public: | |
GrandParent(int x) { | |
cout << "GrandParent::GrandParent(int ) called" << endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// derived classes | |
#include <iostream> | |
using namespace std; | |
class Polygon { | |
int private_width; // this var is not accessible to any derived class, as it is a private variable. | |
protected: | |
int width, height; // protected members are accessible from derived classes. | |
static int static_width; | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// derived classes | |
#include <iostream> | |
using namespace std; | |
class Polygon { | |
int private_width; // this var is not accessable to any derived class, as it is a private variable. | |
protected: | |
int width, height; // protected members are accessable from derived classes. | |
static int static_width; | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Reference: https://www.geeksforgeeks.org/binary-insertion-sort/ | |
In normal Insertion Sort, | |
it takes O(n) comparisions(at nth iteration) in Worst Case. | |
We can reduce it to O(log n) by using Binary Search. | |
The algorithm as a whole still has a running Worst Case running time of O(n2) | |
because of the series of swaps required for each insertion. |
NewerOlder