Skip to content

Instantly share code, notes, and snippets.

View sksoumik's full-sized avatar

Sadman Kabir Soumik sksoumik

View GitHub Profile
# Insertion Sort Time complexity: Worst Case & Average Case: O(n**n)
# Insertion Sort Time complexity: Best Case: O(n)
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
import java.io.*;
class MySingletonClass
{
static MySingletonClass instance = null;
public int x = 10;
// private constructor can't be accessed outside the class
private MySingletonClass() { }
n = int(input('Enter the number of n: '))
def fact(n):
# Base Case
if n == 1:
return 1
# Recursive case
else:
return n * fact(n - 1)
# Fibonacci series using recursion
def fibonacci(n):
if n == 0:
return n
elif n == 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
# Merge Sort | Time Complexity O(NlogN)
def mergeSort(alist):
print("Splitting ", alist)
if len(alist) > 1:
mid = len(alist) // 2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
print("LeftHalf ", lefthalf)
# Merge Sort | Time Complexity O(NlogN)
def mergeSort(alist):
print("Splitting ", alist)
if len(alist) > 1:
mid = len(alist) // 2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
print("LeftHalf ", lefthalf)
class Person(models.Model):
name = models.CharField(max_length=255) # Mandatory
bio = models.TextField(max_length=500, blank=True) # Optional (don't put null=True)
birth_date = models.DateField(null=True, blank=True) # Optional (here you may add null=True)
# A lambda function that multiplies two numbers
multiply = lambda x, y: x * y
print(multiply(2, 2)) # output: 4
# A lambda function that multiplies three numbers
multiply = lambda x, y, z: x * y * z
print(multiply(2, 2, 2)) # output: 8
# A lambda function that adds 10 to the number passed in as an argument, and print the resul
a = lambda a: a + 10
def add_t(x):
return x + 1
tuple_1 = (1, 2, 3, 4)
map_sample_1 = tuple(map(add_t, tuple_1))
print(map_sample_1) # output: (2, 3, 4, 5)
# ---------------------------------------------------------------
seq = [1, 2, 3, 4]
# result contains odd numbers of the list
result = filter(lambda x: x + 1, seq)
print(list(result)) # Output: [1, 2, 3, 4]
# Does nothing on seq as the lambda function does not hold any condition
# filter only works on condition, so we need to give some conditional statement with lambda
result = filter(lambda x: x % 2 == 0,
seq) # returns the elements which are only divisible by two
print(list(result)) # output: [2, 4]