Skip to content

Instantly share code, notes, and snippets.

View vishal-keshav's full-sized avatar
🏠
Working from home #COVID-19

Vishal Keshav vishal-keshav

🏠
Working from home #COVID-19
View GitHub Profile
@vishal-keshav
vishal-keshav / distributions.md
Created February 15, 2019 10:49
Distributions

Distributions

Bernoulli Distribution

A Bernoulli random variable takes 1 with probability p and 0 with probability 1-p. The distribution can be written as:

$$p(x) = p if x = 1 p(x) = 1-p if x = 0$$
@vishal-keshav
vishal-keshav / python_collections.md
Created February 14, 2019 04:45
Python lambda(reduce), namedtuple(collections) and deque(collections), all in one program

Namedtuple and deque

below program takes in a list of student data and prints the average marks.

input to the program

5
MARKS      CLASS      NAME       ID        
92         2          Calum      1         
82         5          Scott      2         
@vishal-keshav
vishal-keshav / rl-funda.md
Last active January 24, 2019 03:01
How to make Reinforcement Learning work!

Making Reinforcement Learning Work

While understanding the principals of reinforcement learning is easy, making the implementation work is not that easy. These are the major difficulty one may face while implementing RL based algorithm for doing something useful(other that playing an atari game).

  1. Lack of framework that could have made the implementation easy: Recently introduced "Dopamine" is at its nascent stage. It does not allow creation of customized environment. Basically, it is just for replicating benchmarks based on board-based games.
  2. Lack of implementation information: Internet is scattered of tutorials and code demostrating how to construct an RL agent that plays a "Pole" game. They just demostrate the agent type, but not necessarily describe the nitty-gritty of theory mapped to implementation.
  3. So many independent tunables: Once implemented, it is very very hard to tell if the implementation is working correctly.

So, there are some guidlines one can follow to be a little bit confident a

@vishal-keshav
vishal-keshav / oops_part12.md
Last active January 26, 2019 16:00
On Object oriented programming and design patterns in C++ part 12

Design Principles Part 12

Vtables

Vtables are the feature of C++. Vtables are used for late binding, and constains some extra information about the created class.

#include "stdafx.h"
@vishal-keshav
vishal-keshav / oops_part11.md
Last active January 26, 2019 15:52
On Object oriented programming and design patterns in C++ part 11

Design Principles Part 11

Concept of single and multi-instance design

Single instance designs are design that can be used only once. Multi-instance designs are designs that can be used multiple times. In C, multi-instance design can be implemented by structs and passing the address everytime of the structure. However, C++ simplifies this process with class. At compiler level, even the C++ class codes gets converted to something that represents how multi-class instance design is implemented in C.

Below code shows how C implements multi-instance design and how it is done by C++. Also, the efficiency of C++ is equivalent to C, which can be easily demostrated with the compiler generated code.

In the below code, it has been shown that the templates on a class or a function does not c create any overhead, but instead they makes the code even faster by compiling only the relevant parts.

@vishal-keshav
vishal-keshav / oops_part10.md
Last active January 26, 2019 15:42
On Object oriented programming and design patterns in C++ part 10

Design Principles Part 10

Iterators abstract the complex structures and gives a clean interface to access the data. C++ already provides containers like vector, maps, queues, etc. which implements iterator patterns.

In order to implement a C++ style iterators on a container (class), iterators in the class namespace has to be defined with increment and access functionality overloaded. Below code demostrate this.

Iterator desing pattern

@vishal-keshav
vishal-keshav / oops_part9.md
Last active January 26, 2019 14:49
On Object oriented programming and design patterns in C++ part 9

Design Principles Part 9

Composition is a very important concept in software engineering. Small eleement creates larger and more complex elements. To understand this moe firmly, we can take an example of a case where we need to create GUI elements. Some GPU elements are standlone by itself, but some combines to constructs a bigger GPU elements.

Below code tries to do this.

Naive Implementation

#include "stdafx.h"
@vishal-keshav
vishal-keshav / oops_part8.md
Last active January 26, 2019 14:13
On Object oriented programming and design patterns in C++ part 8

Design Principles Part 8

Recap of Rule of Inheritance

  1. All instance(non static) data members of Base Class (super class) always becomes part of Derived Class (sub class). This is regardless of what access has been given to the derived class.
  2. All instance methods of Base class can work for derived class object even constructor or destructor
  3. When object of the Derived class calls the instance method of derived class or base class, it's address is always passed to it and it gets collected as this pointer.
  4. When Object of derived class calls instance method of derived class, the object address is passed to the method. And when this method calls the instance method of the base class, the same address is passed to that methods also. Both the method will work on the same object.
  5. When object of derived class calls instance method of base class, the object address is passed to it. Because of this, when required, the derived function can be called back because we have the derived object pointer.
@vishal-keshav
vishal-keshav / from_oops.md
Last active April 6, 2019 08:53
Incomplete

Design Principles Part 7

There are cases where an if-class explosion can happens. Specifically, if explosion happens when there are many if statements to implements and class explosion happens when there are many class that requires those if to be implemented. When these two explodes on their own dimension, the product is called if-class explosion. In order to solve this, bridge pattern are being used. But more importantly, in order to understand that if such a pattern is required, commonality-variability analysis of the given problem must be done.

Consider the below problem, where we do commonality-variability analysis to identify the if-class explosion.

Problem Statement

Create a library of UI controls (ListBox, Grid, Button). These should be created as C++ classes. Various application are to be developed using these classes. Each UI class will has to render the respective UI on the screen, once application requires it. This drawing should be performed by directX on Windows, openGL on

@vishal-keshav
vishal-keshav / oops_part6.md
Last active January 26, 2019 12:58
On Object oriented programming and design patterns in C++ part 6

Design Principles Part 6

In this part, we will learn more about single responsibility priniciple. To learn that, we can start with a problem described below.

Problem statement

Implement a simple concurrent file uploader. Allow the user to keep entering filename, whenever they want to upload. Each filename entered by the user should be uploaded to the FileServer which is remotely located over the internet. Each upload approximately takes 10 seconds time. When the upload for one file is going on, another should not be uploaded. Other files should be uploaded one after the other, the way it happens on google drive client or filezilla.

User should be allowed to enter filenames when upload is going on. No filename entered by the user should be missed. When each upload starts, display the getting uploaded and when upload is completed, dispaly "filename" uploaded.