Skip to content

Instantly share code, notes, and snippets.

@akhileshmoghe
akhileshmoghe / python_map_reduce_filter.py
Created December 31, 2021 13:29
python_map_reduce_filter
# 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.
@akhileshmoghe
akhileshmoghe / std_accumulate.cpp
Created December 31, 2021 06:06
std_accumulate: Accumulate values in range
// 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;
@akhileshmoghe
akhileshmoghe / std_transform.cpp
Created December 30, 2021 10:19
std_transform
// 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; }
@akhileshmoghe
akhileshmoghe / Merge_Sort.cpp
Created December 22, 2021 04:09
Merge Sort
#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;
@akhileshmoghe
akhileshmoghe / Bubble_Sort.cpp
Created December 21, 2021 03:37
Bubble Sort: Not much used in practical use-cases, but used when the elements are already sorted and just a check or one/two swaps needed.
/*
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,
@akhileshmoghe
akhileshmoghe / VirtualInheritanceDelegatingToSisterClass.cpp
Last active January 17, 2022 03:34
Virtual Inheritance Delegating To Sister Class
// 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:
@akhileshmoghe
akhileshmoghe / VirtualInheritanceDiamondProblem.cpp
Created December 18, 2021 07:03
Virtual Inheritance to solve Diamond Problem
#include<iostream>
using namespace std;
class GrandParent {
// Data members of GrandParent
protected:
int grandParentAge;
public:
GrandParent(int x) {
cout << "GrandParent::GrandParent(int ) called" << endl;
@akhileshmoghe
akhileshmoghe / Protected_Inheritance.cpp
Created December 18, 2021 06:51
Private, Protected, Public members accessibility with respect to Protected Inheritance.
// 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:
@akhileshmoghe
akhileshmoghe / public_inheritance.cpp
Created December 18, 2021 06:43
Private, Protected, Public members accessibility with respect to Public Inheritance
// 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:
@akhileshmoghe
akhileshmoghe / InsertionSort_using_BinarySearch.cpp
Last active December 18, 2021 05:32
InsertionSort_using_BinarySearch: Optimizing Insertion Sort using with Binary Search
/**
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.