This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class MySingletonClass | |
{ | |
static MySingletonClass instance = null; | |
public int x = 10; | |
// private constructor can't be accessed outside the class | |
private MySingletonClass() { } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
# --------------------------------------------------------------- | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
OlderNewer