Thursday, April 2, 2020

apt-get install to a folder of your choice

sudo apt-get install --download-only 'required-library' -o Dir::Cache::archives='path to your folder'

example path = '/home/ubuntu/test/'
example required library =  numpy 

find all the substrings in a python string

def find_subs(s):
  n = len(s)
  return [s[i:j + 1] for i in range(n) for j in range(i,n)]

Fibonacci series starting with any two numbers

def multiply(F, M):
    x = (F[0][0] * M[0][0] + F[0][1] * M[1][0])
    y = (F[0][0] * M[0][1] + F[0][1] * M[1][1])
    z = (F[1][0] * M[0][0] + F[1][1] * M[1][0])
    w = (F[1][0] * M[0][1] + F[1][1] * M[1][1])
    F[0][0] = x
    F[0][1] = y
    F[1][0] = z
    F[1][1] = w
   
def power(F, n):
    if( n == 0 or n == 1):return
    M = [[0, 1],[1, 1]] 
    power(F, n // 2)
    multiply(F, F)
    if (n % 2 != 0):
        multiply(F, M) 


def fib(a,b,n):
     
    F = [[0, 1],[1, 1]]
    if (n == 0): return a
    if (n == 1): return b
    power(F, n - 1)
         
    return F[0][1]*a + F[1] [1]*b

How to initialize dynamic variables

Example 1:


Example 2 :
 
str[i] = malloc(len(char)*5)

Why lowercase variable is used other than uppercase?

Practically speaking variables should be allowed in any case, but just to follow convention in a particular domain we use certain format of words.
For example Class-names usually start with  UPPERCASE and if you want to use same name for object it can be only possible by using lowercase.