Skip to content

Instantly share code, notes, and snippets.

Loic loicginoux

View GitHub Profile
@loicginoux
loicginoux / daily_coding_pb_39.rb
Created Jan 7, 2020
# Daily Coding Problem: Problem #39 [Medium]
View daily_coding_pb_39.rb
# 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 Dec 27, 2019
Daily Coding Problem: Problem #30 [Medium]
View daily_coding_pb_30.rb
# 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 Dec 20, 2019
Daily Coding Problem: Problem #23 [Easy]
View coding_problem_23.rb
# 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 Dec 19, 2019
Daily Coding Problem: Problem #21
View daily_coding_problem_21.rb
# 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 Dec 11, 2019
Daily Coding Problem: Problem #9 [Hard]
View test.rb
# 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 Dec 11, 2019
Daily Coding Problem: Problem #13
View test.rb
# 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|
View gist:eeb6ab009c9bb87a722460daa003760f

Gotchas Elixirs:

String Concatenation

iex> name = "Sean"
iex> "Hello " <> name
"Hello Sean"
@loicginoux
loicginoux / active_record_log_subscriber.rb
Last active Sep 24, 2019
how to know which sql query comes from which line of code
View active_record_log_subscriber.rb
# 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)
View inoice_number_generator.rb
# 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
@loicginoux
loicginoux / firebase-messaging-sw.js
Last active Apr 21, 2020
Adding FCM to an html page to receive notifications and sending then via FCM ruby gem. (edit: HTTPS needed!)
View firebase-messaging-sw.js
// [START initialize_firebase_in_sw]
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
You can’t perform that action at this time.