Skip to content

Instantly share code, notes, and snippets.

View rodush's full-sized avatar
👷‍♂️
building web applications

Roman Dushko rodush

👷‍♂️
building web applications
View GitHub Profile
@rodush
rodush / community-followers-bfs-approach.js
Created March 13, 2023 10:13
x-Reddit: community followers
const communityFollowers = {
C1: ["F1", "F3"],
C2: ["F1", "F2"],
C3: ["F2"],
C4: ["F3"],
}
const followedCommunities = {
F1: ["C1", "C2"],
F2: ["C2", "C3"],
@rodush
rodush / miniMaxSum.js
Created March 8, 2023 20:52
Hackerrank task to find the minimal and maximal sum in the array of integer numbers
function miniMaxSum(arr) {
arr.sort((a, b) => a - b) // to avoid 12 being before 2
// edge case with just one element
if (arr.length === 1) {
return console.log(arr[0], arr[0])
}
let min = Infinity;
let max = 0
@rodush
rodush / selection-sort.js
Created February 28, 2023 22:41
Selection Sort
const data = [-2, 45, 0, 11, -9]
let cursor = 0
while (cursor < data.length) {
let min = data[cursor]
let minPosition = cursor
for(let i = cursor; i < data.length; i++) {
if (data[i] < min) {
min = data[i]
minPosition = i
@rodush
rodush / test-brackets-balance.js
Created February 27, 2023 17:29
Test if the given set of brackets is balanced or not
// Test if the given set of brackets is balanced or not.
const data = '{{}}{)([][]'.split('')
// Initiate the stack with the first element
const stack = [data[0]]
for (let c = 1; c < data.length; c++) {
const topElement = stack[stack.length - 1]
if (
(topElement == '{' && data[c] == '}') ||
@rodush
rodush / in-2-out-of-n.js
Created February 27, 2023 16:44
List of elements which are present in at least 2 other lists
// You have three Arrays.
const A = [2, 5, 3, 2, 8,1]
const B = [7, 9, 5, 2, 4, 10, 10]
const C = [6, 7, 5, 5, 3, 7]
// Make an array from this three arrays which elements is present in at least two array.
const map = {}
Array(A, B, C).forEach(arr => {
// Eliminate duplicates from the same array, use Set for this purpose
@rodush
rodush / bsf.js
Created February 23, 2023 10:18
Traversing Graph with the BSF approach
/**
* Graph :: BFS
*/
class Graph {
constructor() {
this.edges = []
}
addEdge(v, w) {
@rodush
rodush / parking-slots-heap.js
Created February 15, 2023 08:25
Nearest parking slot - heap data structure
function generateInput() {
const input = []
for (var i = 0; i < 100; i++) {
input.push(Math.ceil(Math.random() * 100))
}
return input
}
// const input = generateInput().slice(0, 10)
@rodush
rodush / merge-sorted.mjs
Created February 9, 2023 16:29
Merge 2 sorted lists (JS)
import assert from "node:assert"
const l1 = [1, 3, 6, 10]
const l2 = [4, 5, 10, 12, 14]
console.log("List 1:", l1)
console.log("List 2:", l2)
const result = []
@rodush
rodush / aws-service-map.example.js
Created May 4, 2020 08:26
Example of how to work with AWS ServiceMap to get the service endpoint and invoke it
/**
* This is a probe to work with AWS's Service map to dynamically get URL of backend API and invoke it.
* No hardcoded values, but we can filter for specific namespace and services withing that namespace.
*/
const https = require("https")
const ServiceDiscovery = require("aws-sdk").ServiceDiscovery
const sd = new ServiceDiscovery()
@rodush
rodush / n + zsh + nvmrc
Last active March 30, 2023 07:18
Code snippet to enable switching of the node js environment based on configuration from the `.nvmrc` file
# Put this somewhere in the ~/.zshrc file
load-nvmrc() {
local nvmrc_path="$(pwd -P)/.nvmrc"
if [[ -a "$nvmrc_path" ]]; then
local nvmrc_node_version=$(cat "${nvmrc_path}")
if [ "$nvmrc_node_version" = "N/A" ]; then
n latest