Skip to content

Instantly share code, notes, and snippets.

View loicginoux's full-sized avatar

Loic loicginoux

View GitHub Profile
@loicginoux
loicginoux / find_largest_redis_keys.rb
Last active August 10, 2021 13:13
Get the REDIS keys size and print the ones taking more space (in ruby)
# store all keys with their memory value
keys_by_val = {}
keys = REDIS.keys('*').each do |key|
mem = REDIS.memory('usage', key)
keys_by_val[key] = mem
end
# order them by value
ordered = keys_by_val.sort_by { |k,v| v ? v : 0 }
@loicginoux
loicginoux / daily_coding_pb_39.rb
Created January 7, 2020 19:15
# Daily Coding Problem: Problem #39 [Medium]
# Daily Coding Problem: Problem #39 [Medium]
# Conway's Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or alive, and at each tick, the following rules apply:
# Any live cell with less than two live neighbours dies.
# Any live cell with two or three live neighbours remains living.
# Any live cell with more than three live neighbours dies.
# Any dead cell with exactly three live neighbours becomes a live cell.
# A cell neighbours another cell if it is horizontally, vertically, or diagonally adjacent.
# Implement Conway's Game of Life. It should be able to be initialized with a starting list of live cell coordinates and the number of steps it should run for. Once initialized, it should print out the board state at each step. Since it's an infinite board, print out only the relevant coordinates, i.e. from the top-leftmost live cell to bottom-rightmost live cell.
@loicginoux
loicginoux / daily_coding_pb_30.rb
Created December 27, 2019 21:17
Daily Coding Problem: Problem #30 [Medium]
# You are given an array of non-negative integers that represents a two-dimensional elevation map where each element is unit-width wall and the integer is the height. Suppose it will rain and all spots between two walls get filled up.
# Compute how many units of water remain trapped on the map in O(N) time and O(1) space.
# For example, given the input [2, 1, 2], we can hold 1 unit of water in the middle.
# Given the input [3, 0, 1, 3, 0, 5], we can hold 3 units in the first index, 2 in the second, and 3 in the fourth index (we cannot hold 5 since it would run off to the left), so we can trap 8 units of water.
# Solution:
# we run level after level, starting by level 1 and finishing at last level, the highest wall
# for each level we run through the array
# when we discover a wall we can start keeping bucket of water
@loicginoux
loicginoux / coding_problem_23.rb
Created December 20, 2019 20:28
Daily Coding Problem: Problem #23 [Easy]
# You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on.
# Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board.
# For example, given the following board:
# [[f, f, f, f],
# [t, t, f, t],
# [f, f, f, f],
# [f, f, f, f]]
@loicginoux
loicginoux / daily_coding_problem_21.rb
Last active December 19, 2019 08:43
Daily Coding Problem: Problem #21
# Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required.
# For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
def run(intervals_array)
rooms = []
intervals_array.each do |interval|
if rooms.empty?
rooms << [interval]
else
@loicginoux
loicginoux / test.rb
Last active December 11, 2019 10:04
Daily Coding Problem: Problem #9 [Hard]
# Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.
# For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5.
# Follow-up: Can you do this in O(N) time and constant space?
def find_next(array, acc, idx, path)
acc = 0 if acc.nil?
return [acc, path] if array[idx].nil?
return [acc, path] if idx >= array.length - 2 && idx > 1
@loicginoux
loicginoux / test.rb
Created December 11, 2019 09:15
Daily Coding Problem: Problem #13
# Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters.
# For example, given s = "abcba" and k = 2, the longest substring with k distinct characters is "bcb".
def finds(s, k)
arr = s.scan /\w/
max = 0
longest_word = ''
total_length = arr.length
last_idx = total_length - 1
arr.each_with_index do |l, i|

Gotchas Elixirs:

String Concatenation

iex> name = "Sean"
iex> "Hello " <> name
"Hello Sean"
@loicginoux
loicginoux / active_record_log_subscriber.rb
Last active September 24, 2019 10:48
how to know which sql query comes from which line of code
# file config/initializers/active_record_log_subscriber.rb
###
# log result:
# Alert Load (0.8ms) SELECT `alerts`.* FROM `alerts` INNER JOIN `sharings` ON `alerts`.`id` = `sharings`.`alert_id` WHERE `sharings`.`user_id` = 37 AND `alerts`.`id` = 54148 AND (status = 0) LIMIT 1
# ↳ app/controllers/entries_controller.rb:20:in `set_objects'
# source: http://www.jkfill.com/2015/02/14/log-which-line-caused-a-query/
###
module LogQuerySource
def debug(*args, &block)
@loicginoux
loicginoux / inoice_number_generator.rb
Created February 13, 2019 17:55
invoice number generator
# generate invoice number in a sequential mode
# using redis and redlock gem
# ex:
# Accounting::InvoiceNumberGenerator::Base.new.lock do |next_number|
#. # create your invoice here
# end
class Accounting::InvoiceNumberGenerator::Base
LOCK_TIMEOUT = 10000
FIRST_NUM_SEQUENCE = 100