Sunday, May 30, 2021

Cheap Fast Good

 




You can make a great software but you have to choose 2 out of this above 3. There is no way to get around!

Monday, January 4, 2021

Python linked List implementation

 


class Node:
def __init__(self, data):
self.data=data
self.nextNode=None



class LinkedLIst:

def __init__(self):
self.head = None
self.numberOFNodes=0
# Here we get o(1) constant running time complexity for insertion.
def insert_start(self,data):

self.numberOFNodes=self.numberOFNodes+1
new_node = Node(data)

if not self.head:
self.head=new_node
else:
new_node.nextNode=self.head
self.head = new_node
#Linear running time o(n)
def insert_end(self,data):
self.numberOFNodes=self.numberOFNodes+1
new_node=Node(data)

actual_node=self.head

while actual_node.nextNode is not None:
actual_node=actual_node.nextNode

actual_node.nextNode=new_node

def size_of_list(self):

actual_node = self.head

while actual_node is not None:
print(actual_node)
actual_node=actual_node.nextNode

def traverse(self):
actual_node=self.head

while actual_node is not None:
print(actual_node.data)
actual_node=actual_node.nextNode

def deleteAtHead(self):

self.head=self.head.nextNode
self.numberOFNodes=self.numberOFNodes-1

def deleteAtTail(self):

actual_node = self.head

while actual_node.nextNode.nextNode is not None:
actual_node=actual_node.nextNode

actual_node.nextNode=None
self.numberOFNodes=self.numberOFNodes-1



linked_list=LinkedLIst()
linked_list.insert_start(4)
linked_list.insert_start(3)
linked_list.insert_start('A String type')
linked_list.insert_start(1.232)
linked_list.insert_end(12.232)
linked_list.traverse()

print('Deleting at the front')
linked_list.deleteAtHead()
linked_list.traverse()

print('Delete at the last position')
linked_list.deleteAtTail()
linked_list.traverse()

Thursday, October 1, 2020

Custom annotations in Spring Boot

           I had worked on spring framework extensively throughout my career. The one thing which I am always try to implement in any Spring project I work on is Custom annotation. Its a fancy stuff but useful in many ways. 
         Say you have numerous micro services (Duplicate code bases which will f*** you up) running on your cluster, the best and first thing you do is - Build a common library to push all your model classes, so called util package of your organization :p and the fancy stuffs  . I am fortunate that I get to do all these experiments early on my career. So lets start with the problem.

  1. We have many micro services which are built on top of spring framework. We want to use a  common service bean in all our boot apps.  
  2. This bean should be enabled with @EnableMyBean annotation.
  3. Once the spring boot app loads it should intercept this annotation and inject our MyBean service to our Spring boot application.

So the first step to do is create the annotation itself




The @Import is from org.springframework.context.annotation package  You can find the documentation over here









Next is the implementation of MyBeanSelector class. We can define it as follows.


     Lets look at each components. First the importSelector interface. As the definition on the Spring document "Interface to be implemented by types that determine which @Configuration class(es) should be imported based on a given selection criteria, usually one or more annotation attributes.

     This class is accountable for which Bean should be injected  to the spring context. It has many other cool features as well. Say you want to read some property from the environment in which the common library is used. Ie, from the Yaml file of the spring boot app which is using your common library. Then you can implement EnvironmentAware Interface. 
     
    Once we do the above, next we can define the actual service class inside "com.commons.service". 




So now You can use @EnableMyBean on your main method of the spring boot application. And you can Autowire MyService class anywhere inside your application. At the time of start up spring would automatically detect the @EnableMyBean annotation and push the Service Bean to the app's context.

Wednesday, September 30, 2020

Algorithms and Data Structures on Python - Notes 2

 Linked list


It needs a node class

It should have 2 basic characteristic

* Data

* Reference to the next node

It can be used to implement stack or queues. It does not allow random access as like in Array.

Obtaining a last node, locating a particular node requires an iteration over all or most of the items in the linked list. 

Advantage

* It is dynamic data structures(No need to specify size)

* It can allocate memory on run time

* It take O(1) constant time complexity to insert an item at the beggining or removing at the beggining, unlike array where as its O(n)

* Linked list can grow organically, unlike in array where we need o(n) for re-sizing here its just about updating the reference 


Disadvantages.

* Waste of memory due to references

* Nodes in the linked list must be read in order from the beginning as linked list have sequential access

* Difficult to do reverse travel , but we can enable this with back pointer. But this takes a memory

 

Inserting a references

* Inserting to the beginning takes O(1) constant time complexity. Because its just about adding a node

updating its next reference to the start of the linked list

* Inserting to the last node O(n) time complexity because we need to traverse throughout the linked list

to get to the last node, where last node will be pointing to a NULL reference. NULL reference is very much 

important in terms of linked list because that will be the last node of the linked list


Remove

Removing at the beginning is always fast because we don't need to search for item. Its 

only about removing, eg. In java assign to null.

Removing at a given point is O(n) linear time complexity.   


Sunday, September 20, 2020

Algorithms and Data Structures on Python - Notes 1

 

         Data Structures Overview  

          Bad programmers worry about code , good programmers worry about data structures and their relationships! 

       Data structures provide efficient ways to store data. A proper data structure can boost an Algorithm. If we need to make a app performs good then we must care about selecting proper data structure.

     There is a tradeoff between running time complexity and memory time complexity. Eg. Dijktras algorithm with priority queue performs better but it will take more memory. If we need to minimize the memory then the application will be slow.

          Abstract data types  -  Data Structures 

Abstract data type is basically the logical implementation. We can use data structures like array or linked list to implement it. So Abstract data types are logical definition  and data structures are concrete implementations. This is the difference between abstract data types and data structures.


Arrays - Basics

         It can be N dimensional according to the needs. Arrays are good because it has O(1) time complexity to get by index. 

  Disadvantages are 

  1. It is compile-time : it is no so dynamic data structures.
  2. If it is full we need to create a bigger array to copy the contents. That operation is going to take O(N) time complexity
  3. It is not able to store items with different type
Arrays - Operations
       Add : We can do it in O(1) constant time complexity because we are going to insert the element at the end of the array. And we certainly we should know the location where we are inserting.
                 If we insert to middle of  of the array, then we have to shift entire array and we ends up O(N) linear time complexity. 
       Remove : If we remove the last item then we will be able to do it on O(1). But to remove an item with a particular index then shifting is involved so we will end up with O(N). 
      Arrays are good data structures? It depends on the application it uses

     


              


Tuesday, December 31, 2019

Python Unit Test: Basics 1

 When comes to any programming language what i check first is how to write a unit test on it. 

   "Coding is not difficult- Bill Gates" So what is difficult? I would say testing is difficult. Here is a basic example on how to test a small piece of python code.


                The above snippet is a basic recursive program which will return the factorial of a number. Lets see how we can write a basic test case for this. I have my directory structure as follows.



Lets see the contents of n_factorial_tests.py.



  • The first line is on importing the program to the test class. 
  • Next we are importing unittest : This module provides a rich set of tools for constructing and  running tests.
  • While defining a function you can see we have used an object "self". This used to represent an instance of a class. Here it means accessing the contents of unittest.TestCase which is being inherited to this class.
  • Now we can access the functions defined unittest.TestCase  module. Because we have inherited that to our test class.
  • self.assertEqual(n_factorial.factorial(5),120) : This is the statement where we assert for our expectation vs the actual value.
  • If you run this on PyCharm we can see the below.

Finally i wrote a python code and tested it ❤️

Managing Concurrent Updates with Distributed Locks

Managing Concurrent Updates with Distributed Locks Managing Concurrent Updates with Distributed Locks In distri...