Skip to content

Instantly share code, notes, and snippets.

View drem-darios's full-sized avatar

Drēm Darios drem-darios

View GitHub Profile
@drem-darios
drem-darios / Camera.java
Created November 27, 2020 06:02
This demonstrates the Decorator pattern using functional programming
import java.awt.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
public class Camera {
private Function<Color, Color> filter;
public Camera(){
@drem-darios
drem-darios / make_squares.py
Created February 14, 2020 07:05
Given a sorted array, create a new array containing squares of all the number of the input array in the sorted order.
def make_squares(arr):
squares = [0 for x in range(len(arr))]
frontPointer = 0
backPointer = len(arr) - 1
for resultIndex in range(len(arr)):
frontSquare = arr[frontPointer] * arr[frontPointer]
backSquare = arr[backPointer] * arr[backPointer]
if frontSquare >= backSquare:
@drem-darios
drem-darios / InsertInterval.java
Last active February 13, 2020 05:13
Given a list of non-overlapping intervals sorted by their start time, insert a given interval at the correct position and merge all necessary intervals to produce a list that has only mutually exclusive intervals.
import java.util.*;
class Interval {
int start;
int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
@drem-darios
drem-darios / has_cycle.py
Created February 10, 2020 06:51
Given the head of a Singly LinkedList, write a function to determine if the LinkedList has a cycle in it or not.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def has_cycle(head):
fast_pointer = head
slow_pointer = head
while fast_pointer is not None and fast_pointer.next is not None:
@drem-darios
drem-darios / salt_signature_36.py
Created February 10, 2020 06:08
Python example of how to create a salt and use a secret key to generate a signature using the salt. This was tested with Python 3.6.8
import hmac
import os
import hashlib
import base64
import unittest
__author__ = 'drem'
class SecurityUtil(object):
@drem-darios
drem-darios / dutch_flag_sort.py
Created February 10, 2020 05:34
Given an array containing 0s, 1s and 2s, sort the array in-place. You should treat numbers of the array as objects, hence, we can’t count 0s, 1s, and 2s to recreate the array.
def dutch_flag_sort(arr):
n = len(arr)
frontPointer = 0
backPointer = n - 1
solutionIndex = 0
while solutionIndex <= backPointer:
# Swap the 0s
if arr[solutionIndex] == 0:
swap(solutionIndex, frontPointer, arr)
@drem-darios
drem-darios / remove_duplicates.py
Last active February 9, 2020 07:48
Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in-place return the new length of the array.
def remove_duplicates(arr):
firstPointer = 1
secondPointer = 1
while secondPointer < len(arr):
if arr[firstPointer - 1] != arr[secondPointer]:
# Replace duplicate with non duplicate
arr[firstPointer] = arr[secondPointer]
firstPointer += 1
secondPointer +=1
@drem-darios
drem-darios / longest_substring_with_k_distinct.py
Last active June 24, 2020 04:48
Given a string, find the length of the longest substring in it with no more than K distinct characters.
def longest_substring_with_k_distinct(str, k):
windowStart = 0
uniqueCharacterCount = {}
longestRun = 0
currentRun = 0
uniqueCharacters = 0
for windowEnd in range(len(str)):
nextCharacter = str[windowEnd]
nextCharacterCount = uniqueCharacterCount.get(nextCharacter)
# We have seen this character already to increment its count
@drem-darios
drem-darios / smallest_subarray_with_given_sum.py
Last active February 9, 2020 06:30
Given an array of positive numbers and a positive number ‘S’, find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. Return 0, if no such subarray exists.
import math
def smallest_subarray_with_given_sum(s, arr):
subArrayStart = 0
resultArraySize = math.inf
subArraySum = 0
for subArrayEnd in range(0, len(arr)):
subArraySum += arr[subArrayEnd]
while subArraySum >= s:
@drem-darios
drem-darios / max_sub_array_of_size_k.py
Last active February 6, 2020 06:39
Given an array of positive numbers and a positive number ‘k’, find the maximum sum of any contiguous subarray of size ‘k’.
# By Drem
def max_sub_array_of_size_k(k, arr):
maximumSum = 0
currentMaximumSum = 0
subArrayStart = 0
for subArrayEnd in range(len(arr)):
maximumSum += arr[subArrayEnd]
if subArrayEnd >= k - 1:
if maximumSum > currentMaximumSum: