Saturday, 29 February 2020

Polymorphism in Python

Hi Guys!!! Hope all is well...
Polymorphism is greek word Poly + Morphos = Many +forms
In programming, polymorphism means same function name (but different signatures) being uses for different types.
For clarification, Here implementing polymorphism in Python by using common names across methods in different classes rather than by using inheritance.
As for example we had a bunch of different classes representing animals, and each animal class had a method that’d print out the sound they make.
In our main()function, we have a variable called animals,which is a list of animal objects.
The question becomes, how could I iterate through each of the different objects and print out what sound they make?

class Duck:
def quack(self):
print("a duck makes a quack sound")
class Cow:
def moo(self):
print("a cow makes a moo sound")
class Dog:
def bark(self):
print("a dog makes a woof sound")
def main():
animals = [Duck(), Cow(), Dog(), Cow(), Duck(), Dog()]
for animal in animals:
#TODO want to print out the sound each of the different animals can make
main()
One way we could do this is by adding a member variable called type that we’ll create and initialize in the constructor of each class. This variable will help us with the special case logic in our main()function. And when we run it, we’ll get the correct output.
class Duck:
def __init__(self):
self.type = "duck"
def quack(self):
print("a duck makes a quack sound")
class Cow:
def __init__(self):
self.type = "cow"
def moo(self):
print("a cow makes a moo sound")
class Dog:
def __init__(self):
self.type = "dog"
def bark(self):
print("a dog makes a woof sound")
def main():
animals = [Duck(), Cow(), Dog(), Cow(), Duck(), Dog()]
for animal in animals:
animal_type = animal.type
if animal_type == "duck":
animal.quack()
elif animal_type == "cow":
animal.moo()
elif animal_type == "dog":
animal.bark()
else:
pass


However,this code works what we try to achieve but We have to write seems a lot of extra code . For What ? To call a method in each class that does the same thing. 
And being programer = laziness, So We have to find better approach.
A better way to do this using polymorphism .
since the idea behind each method is printing out the sound each animal makes, would be to generalize the names of each method and call them the same thing, so:
class Duck:
def make_sound(self):
print("a duck makes a quack sound")
class Cow:
def make_sound(self):
print("a cow makes a moo sound")
class Dog:
def make_sound(self):
print("a dog makes a woof sound")
def main():
animals = [Duck(), Cow(), Dog(), Cow(), Duck(), Dog()]
for animal in animals:
animal.make_sound()

This is what polymorphism is because we’re taking three different objects (Duck, Cow, and Dog), and we’re treating all of them in the same way through a common interface — being the make_sound() method.

Thanks
Saurabh Sharma
Happy Coding !!!

Thursday, 20 February 2020

Big O Notation in Layman term

Hi Guys !!! Hope all is well.

In my previous post Data Structures for Android Developer, I have quoted below quote. 
Bad programmers worry about the code. Good programmers worry about data structures and their relationships — Linus Torvalds

So, What is Big O?
  • Big O notation is the language we use for talking about how long an algorithm takes to run (time complexity) or how much memory is used by an algorithm (space complexity). 
  • Big O notation can express the best, worst, and average-case running time of an algorithm.
Big-O as time complexity
  •  Analysis of running times does not take certain things into account, such as the processor, the language, or the run-time environment. 
  • Instead, we can think of time as the number of operations or steps it takes to complete a problem of size n.
  • In other words, big O notation is a way to track how quickly the run-time grows relative to the size of the input
  • In the worst-case, the question becomes this: What are the most operations/steps that could happen for an input of size n?
O(1) → Constant Time

O(1) means that it takes a constant time to run an algorithm, regardless of the size of the input.

In programming, a lot of operations are constant. Here are some examples:
  • math operations
  • accessing an array via the index
  • accessing a hash via the key
  • pushing and popping on a stack
  • insertion and removal from a queue
  • returning a value from a function

O(n) → Linear Time
  • O(n) means that the run-time increases at the same pace as the input
  • In programming, one of the most common linear-time operations is traversing an array
  • Generally speaking (but not always), seeing a loop is a good indicator that the particular chunk of code you’re examining has a run time of O(n)
O(n²) → Quadratic Time
O(n²) means that the calculation runs in quadratic time, which is the squared size of the input data.
In programming, many of the more basic sorting algorithms have a worst-case run time of O(n²):
  • Bubble Sort
  • Insertion Sort
  • Selection Sort
Generally speaking (but not always), seeing two nested loops is typically a good indicator that the piece of code you’re looking at has a run time of O(n²). Likewise — three nested loops would indicate a run time of O(n³)!

O(log n) → Logarithmic Time
O(log n) means that the running time grows in proportion to the logarithm of the input size. this means that the run time barely increases as you exponentially increase the input.
In programming, the act of searching a word through a physical dictionary is an example of a binary search operation — the most typical example used when discussing logarithmic run times.

O(n log n) → Linearithmic Time
which is often confused with O(log n), means that the running time of an algorithm is linearithmic, which is a combination of linear and logarithmic complexity. Sorting algorithms that utilize a divide and conquer strategy are linearithmic, such as the following:
  • merge sort
  • timsort
  • heapsort
Calculating Big O
Thus far, we’ve only focused on talking about big O .
 How do we calculate big O if we are dealing with an algorithm that has several parts?
Below is the few point keep in mind  during calculation of Big O
  • Assume the worst
  • Remove constants
  • Use different terms for inputs
  • Drop any non dominants
1. Assume the worst
When calculating Big O, you always think about the worst case.
For example, if you were to loop over an array and look for an item, it could be the first item or it could be the last. As we aren’t certain then we must assume O(n) in this instance.

2. Remove constants

We could express the Big O notation of a function as O(1 + n/2 +100) but it’s really too specific. 
We can remove our O(1) operations because, as a rule of thumb, they are likely to be insignificant compared to our O(n) operations. 
If we are providing a million items in our input array, then an extra 100 operations from O(1) aren’t our main concern.
So then we have O(n / 2) — as n gets larger and larger, dividing it by two has a diminishing effect. So ultimately our Big O notation for this function becomes O(n).

3. Different terms for inputs
If we were to loop twice of the same array then our Big O is technically O(2n) but let’s drop the constants so we are left with O(n). 
But now we need to think about different terms for inputs. What does that mean?
if We have a function that take 2 diferent argument (long and int)
One could be one item long, the other could contain a million items. So our function is no longer O(n), it is O(a + b). The n we use in our notation is arbitrary, so we need to reflect both inputs in our notation.

4. Drop non-dominant terms
So we have a single loop at the top which is O(n) and then a nested loop which is O(n²). So our total is O(n + n²). But as we saw in rule #2, we want to drop constants. So which of the two terms is more important to us?
In this case we drop O(n) and retain O(n²) as this is more important. It is the dominant term as it has a much heavier impact on performance.

Thanks
Saurabh Sharma
Happy Coding !!!

Wednesday, 19 February 2020

Best Available Linux Distro as per need


Hi Guys !!! Today I ma going to put a list of best Linux Desktop OS (Linux Distro) based on use case
 At the end of blog, I will share  online Linux disro test site.  Where you can check all available Linux Desktop OS without physically installing it in your laptop/desktop.

S. No.
Linux Distro Name
Feature or Type of Work support
1.
Elementary OS
  • Probably the best looking distro in the world
  • Smartly designed and looks great
  • Excellent desktop environment
  • Not many pre-installed apps


2.
Linux Mint
  • A strong option for those new to Linux
  • Ideal for those switching from Windows/Mac
  • Good media support out of the box
  • Impressive amount of customisation options



3.
Arch Linux
  • slightly less user-friendly distro
  • Arch allows you to customize your build
  • The main aim of Arch Linux is to keep things simple
  • User Friendly Distributions
  1. Manjaro
  2. ArcoLinux
  3. Chakra
  4. Anarchy Linux



4.
Ubuntu
  • One of the most popular distros for good reasons
  • Very accessible for novices
  • Security and stability of LTS version
  • Lubuntu spin is great for underpowered PCs



5.
Tails
  • A distro for the privacy-conscious
  • Emphasis on security and privacy
  • The OS routes all its internet traffic through the anonymity Tor network
  • it’s based on Debian Linux
  • Uses the Gnome desktop so the interface is still clear and user-friendly



6.
CentOs
  • Offshoot of Enterprise version of Red Hat Linux
  • Built for stability
  • Ideal for a server
  • Not so great for daily desktop usage



7.
openSUSE
  • Primarily targeted at devs and sysadmins
  • Very polished distro
  • Nicely secure Can create your own version of the OS



8.
Zorin OS
  • Entirely made for Windows refugees
  • Zorin OS is an Ubuntu-based, highly polished Linux distribution
  • Comes with a huge (I mean really huge) list of pre-installed software
  • Wine and PlayOnLinux come pre-installed so you can run your loved Windows software and games here too

https://distrotest.net/
This site let you test all available Linux disto . It helps you to decide which one is good for you.
Only above site need good internet connection for testing. 


Thanks
Saurabh Sharma
Happy Coding 

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...