Gotchas Elixirs:
iex> name = "Sean"
iex> "Hello " <> name
"Hello Sean"
# 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 } |
# 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. |
# 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 |
# 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]] |
# 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 |
# 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 |
# 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| |
# 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) |
# 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 |