Skip to main content

A Brief Guide about Docker for Developer in 2023

  What is Docker? Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is based on the idea of containers, which are a way of packaging software in a format that can be easily run on any platform. Docker provides a way to manage and deploy containerized applications, making it easier for developers to create, deploy, and run applications in a consistent and predictable way. Docker also provides tools for managing and deploying applications in a multi-container environment, allowing developers to easily scale and manage the application as it grows. What is a container? A container is a lightweight, stand-alone, and executable package that includes everything needed to run the software, including the application code, system tools, libraries, and runtime. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. It al...

Top 8 Algorithms Every Programmer Should Know 🧑‍💻🧑‍💻


In programming, an algorithm is a set of instructions or a procedure for solving a specific problem or achieving a specific task. Algorithms can be expressed in any programming language and can be as simple as a sequence of basic operations or as complex as a multi-step process involving different data structures and logic. The main goal of an algorithm is to take in input, process it and provide an output that is expected. Algorithms can be classified based on the time and space complexity, the technique used for solving the problem, and the type of problem it solves. Examples of algorithm are sorting, searching, graph traversals, string manipulations, mathematical operations, and many more.


def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)

print(quicksort([3,6,8,10,1,2,1]))
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)

def merge(left, right):
result = []
i = 0
j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
print(merge_sort([3,6,8,10,1,2,1]))
def heap_sort(arr):
n = len(arr)
for i in range(n, -1, -1):
heapify(arr, n, i)
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)

def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
print(heap_sort([3,6,8,10,1,2,1]))
def binary_search(arr, x):
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1
print(binary_search([1,2,3,4,5,6,7], 4))
class HashTable:
def __init__(self):
self.size = 10
self.keys = [None] * self.size
self.values = [None] * self.size

def put(self, key, data):
index = self.hash_function(key)
while self.keys[index] is not None:
if self.keys[index] == key:
self.values[index] = data # update
return
index = (index + 1) % self.size
self.keys[index] = key
self.values[index] = data

def get(self, key):
index = self.hash_function(key)
while self.keys[index] is not None:
if self.keys[index] == key:
return self.values[index]
index = (index + 1) % self.size
return None

def hash_function(self, key):
sum = 0
for pos in range(len(key)):
sum = sum + ord(key[pos])
return sum % self.size

t = HashTable()
t.put("apple", 10)
t.put("orange", 20)
t.put("banana", 30)
print(t.get("orange"))
import heapq

def dijkstra(graph, start):
heap = [(0, start)]
visited = set()
while heap:
(cost, v) = heapq.heappop(heap)
if v not in visited:
visited.add(v)
for u, c in graph[v].items():
if u not in visited:
heapq.heappush(heap, (cost + c, u))
return visited

graph = {
'A': {'B': 2, 'C': 3},
'B': {'D': 4, 'E': 5},
'C': {'F': 6},
'D': {'G': 7},
'E': {'G': 8, 'H': 9},
'F': {'H': 10},
'G': {},
'H': {}
}
print(dijkstra(graph, 'A'))
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)


print(fibonacci(10))
from collections import Counter, namedtuple

def huffman_encoding(data):
"""
Generates a Huffman encoded string of the input data
"""
# Create a frequency counter for the data
freq_counter = Counter(data)
# Create a namedtuple for the Huffman tree nodes
HuffmanNode = namedtuple("HuffmanNode", ["char", "freq"])
# Create a priority queue for the Huffman tree
priority_queue = PriorityQueue()
# Add all characters to the priority queue
for char, freq in freq_counter.items():
priority_queue.put(HuffmanNode(char, freq))
# Combine nodes until only the root node remains
while priority_queue.qsize() > 1:
left_node = priority_queue.get()
right_node = priority_queue.get()
combined_freq = left_node.freq + right_node.freq
combined_node = HuffmanNode(None, combined_freq)
priority_queue.put(combined_node)
# Generate the Huffman code for each character
huffman_code = {}
generate_code(priority_queue.get(), "", huffman_code)
# Encode the input data
encoded_data = ""
for char in data:
encoded_data += huffman_code[char]
return encoded_data, huffman_code
print(huffman_encoding("aaaaabbbcccc"))
def solveNQueens(n):
def could_place(row, col):
# check if a queen can be placed on board[row][col]
# check if this row is not under attack from any previous queen in that column
for i in range(row):
if board[i] == col or abs(board[i] - col) == abs(i - row):
return False
return True

def backtrack(row=0, count=0):
for col in range(n):
if could_place(row, col):
board[row] = col
if row + 1 == n:
count += 1
else:
count = backtrack(row + 1, count)
return count
board = [-1 for x in range(n)]
return backtrack()
print(solveNQueens(4))
import random

def randomized_quicksort(arr):
if len(arr) <= 1:
return arr
pivot = random.choice(arr)
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return randomized_quicksort(left) + middle + randomized_quicksort(right)

print(randomized_quicksort([3,6,8,10,1,2,1]))


Comments

Popular posts from this blog

JavaScript: What the heck is a Callback?

  What is a Callback? Simply put:   A callback is a function that is to be executed  after  another function has finished executing — hence the name ‘call back’. More complexly put:   In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions. Functions that do this are called  higher-order functions . Any function that is passed as an argument is called a  callback function . ^ That’s a lot of words. Lets look at some examples to break this down a little more. Why do we need Callbacks? For one very important reason — JavaScript is an event driven language. This means that instead of waiting for a response before moving on, JavaScript will keep executing while listening for other events. Lets look at a basic example: function first(){ console.log(1); } function second(){ console.log(2); } first(); second(); As you would expect, the function  first  is executed f...

Flutter — Clean Code

  Introduction: Clean code is essential in every programming language to make the code more readable, maintainable, and understandable. The same is true for Flutter. Clean code practices in Flutter are the best way to ensure that your code is easy to understand, easy to maintain, and easy to modify. In this article, we will discuss some of the best clean code practices in Flutter with examples. Follow Flutter Naming Conventions: When writing code in Flutter, it is essential to follow the naming conventions recommended by the Flutter team. Flutter follows the Dart language naming conventions. These conventions help other developers to understand your code easily. Here is an example of how to name a class in Flutter: // Good naming convention class MyClass {} // Bad naming convention class my_class {} Use Descriptive Variable and Function Names: Use descriptive variable and function names so that other developers can understand the purpose of the variable or function. Avoid using...

A Brief Guide about Docker for Developer in 2023

  What is Docker? Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is based on the idea of containers, which are a way of packaging software in a format that can be easily run on any platform. Docker provides a way to manage and deploy containerized applications, making it easier for developers to create, deploy, and run applications in a consistent and predictable way. Docker also provides tools for managing and deploying applications in a multi-container environment, allowing developers to easily scale and manage the application as it grows. What is a container? A container is a lightweight, stand-alone, and executable package that includes everything needed to run the software, including the application code, system tools, libraries, and runtime. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. It al...