This file contains 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
// https://github.com/alexavlonitis | |
// Get folder size iteratively in go. | |
// The same can be achieved using the file.WalkDir() function that recursively walks a directory. | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" |
This file contains 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
// Lru cache in java with custom doubly linked list. | |
import java.util.HashSet; | |
import java.util.Iterator; | |
public class LRURunner { | |
public static void main(String[] args) { | |
Lru lru = new Lru(3); | |
lru.get("1"); |
This file contains 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
# IP address longest prefix matching algorithm in ruby | |
# Used manual conversions methods, instead of ruby's built in methods | |
# Given the router receives a destination ip address "ip" | |
# It has to find out on which network it should forward it to, from its routing table. | |
# To find the network we need to do an AND operation of the binary representation of the dest IP and the | |
# destination subnet mask of our routing table "ip" AND "dest_mask" | |
# https://community.cisco.com/t5/switching/please-explain-how-the-longest-prefix-matching-works/td-p/2891235 |
This file contains 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
# Reverse array recursively | |
def reverse_array(arr) | |
return arr if arr.empty? | |
reverse_array(arr[1..-1]) + [arr.first] | |
end | |
p reverse_array([1, 2, 3]) | |
# Reverse singly linked list |
This file contains 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
# solution from https://www.codewars.com/kata/reviews/51b62bf7a9c58071c6000026/groups/560d5575e3ff138f6400001d | |
NUMS = [ | |
[1000, "M"], | |
[900, "CM"], | |
[500, "D"], | |
[400, "CD"], | |
[100, "C"], | |
[90, "XC"], | |
[50, "L"], |
This file contains 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
# https://github.com/alexavlonitis | |
# Simple HashMap/HashTable implementation in ruby | |
class HashMap | |
def initialize(table_size = 18) | |
@table = Array.new(table_size) | |
end | |
def set(key, value) | |
table_index = table_index(key) |
This file contains 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
# https://github.com/alexavlonitis | |
# BST Tree traversals in ruby, inorder, preorder and postorder using recursion | |
# Invert BST Tree in ruby using recursion | |
class Tree | |
attr_accessor :root | |
def initialize | |
@root = nil | |
end |
This file contains 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
# https://github.com/alexavlonitis | |
# Simple string compression 'aaabbb' to 'a3b3' | |
def compress_string(text) | |
consecutive_chars = text.each_char.chunk_while(&:==).map(&:join) | |
consecutive_chars.map do |chars| | |
chars_group = chars.split('') | |
chars_group.first + chars_group.length.to_s | |
end.join |
This file contains 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
# https://github.com/alexavlonitis | |
# Ceasar's cipher in ruby | |
def cipher(word, index_shift = 13) | |
alphabet = ('a'..'z').to_a | |
alphabet_upcase = ('A'..'Z').to_a | |
cipher_store = {} | |
alphabet.each.with_index do |letter, i| | |
cipher_store[letter] = alphabet[(index_shift + i) - alphabet.size] |
This file contains 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
# Continue traveling in the same direction while there are remaining requests in that same direction. | |
# If there are no further requests in that direction, then stop and become idle, | |
# or change direction if there are requests in the opposite direction. | |
require 'set' | |
class Elevator | |
attr_reader :floors_up, :floors_down | |
attr_accessor :direction, :current_floor |
NewerOlder