Skip to content

Instantly share code, notes, and snippets.

View AlexAvlonitis's full-sized avatar
👾

Alex Avlonitis AlexAvlonitis

👾
View GitHub Profile
@AlexAvlonitis
AlexAvlonitis / go-tree.go
Created November 26, 2022 21:24
Get folder size iteratively in go
// 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"
@AlexAvlonitis
AlexAvlonitis / lru_java.java
Created February 3, 2022 16:45
LRU cache in java
// 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");
@AlexAvlonitis
AlexAvlonitis / ip_address_longest_prefix_matching.rb
Created January 25, 2022 15:16
IP address longest prefix matching algorithm in ruby
# 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
@AlexAvlonitis
AlexAvlonitis / reverse_sequence.rb
Last active January 24, 2022 18:41
Reverse array and linked list recursively in ruby
# 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
@AlexAvlonitis
AlexAvlonitis / roman_numerals.rb
Created October 11, 2021 12:49
Roman numerals converter
# 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"],
@AlexAvlonitis
AlexAvlonitis / hash_table_in_ruby.rb
Last active September 27, 2021 15:07
Simple hash table implemention in ruby
# 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)
@AlexAvlonitis
AlexAvlonitis / bst_traversals.rb
Last active September 27, 2021 15:03
BST tree traversals in ruby
# 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
@AlexAvlonitis
AlexAvlonitis / string_compression_ruby.rb
Last active September 27, 2021 15:04
String compression method in ruby
# 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
@AlexAvlonitis
AlexAvlonitis / ceasars_cipher_ruby.rb
Last active September 27, 2021 15:05
Ceasar's cipher in ruby
# 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]
@AlexAvlonitis
AlexAvlonitis / elevator_algorithm.rb
Last active January 20, 2022 10:27
Elevator Algorithm in Ruby
# 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