Skip to content

Instantly share code, notes, and snippets.

@leikind
leikind / pack_string_in_number_and_back.rb
Created November 4, 2016 09:31
pack a UTF8 string in a huge number, and unpack it
def string_to_number(str)
Integer('0b' + str.unpack('B*')[0])
end
def number_to_utf8_string(num)
str = [num.to_s(2)].pack('B*')
str.force_encoding(Encoding::UTF_8)
end
p string_to_number("мудак")
package main
import (
"fmt"
"github.com/nfnt/resize"
"image/jpeg"
"io"
"os"
)

ElixirCoin

Let's build a small distributed system to mine a new cryptocurrency and become millionaires in ElixirCoins!

An ElixirCoin is a {secret_string, positive_integer} pair for which the MD5 digest of the concatenation of the secret string with the given integer is a hash whose hexadecimal representation starts with at least 5 consecutive zeroes.

For instance:

  • {"foo", 123} is not an ElixirCoin because the MD5 hash of foo123 is ef238ea00a26528de40ff231e5a97f50
  • {"Serun+u", 1} is a valid ElixirCoin because the MD5 hash of Serun+u1 is 00000011f4de73238f12fb2c57d5dc56
defmodule Rain do
def count_blocks_with_water(blocks) do
Enum.map(Enum.max(blocks)..0, &(&1)) |>
Enum.reduce 0, fn(level, sum) ->
sum +
(Enum.with_index(blocks) |>
Enum.filter(fn({block, _}) ->
block >= level
end) |>
def count_blocks_with_water blocks
blocks.max.downto(0).reduce(0){|sum, level|
sum +
blocks.each_with_index.select{|block, idx|
block >= level
}.map{|_, idx|
idx
}.each_cons(2).reduce(0){|accu, tuple|
accu + tuple[1] - tuple[0] - 1
your_map(Function, List) ->
ParentProcessPid = self(),
Pids = lists:map(
fun(Element) ->
spawn(
fun()->
ParentProcessPid ! {self(), Function(Element) }
end
)
defmodule Sequence.Server do
use GenServer.Behaviour
# External API
def start_link(current_number) do
:gen_server.start_link({ :local, :sequence }, __MODULE__, current_number, [])
end
def next_number do
-module(sequence_server).
-behaviour(gen_server).
% External API
-export([
start_link/1,
next_number/0,
increment_number/1
]).
@leikind
leikind / SignalOfRandomNum.elm
Created December 15, 2015 20:19
A signal of random numbers every second where the first seed is a timestamp
module SignalOfRandomNum where
import Graphics.Element as GE
import Time
import Random
import Maybe exposing (..)
randomIntSignal : Int -> Int -> Signal Time.Time -> Signal Int
randomIntSignal lo hi inputSignal =
let
#Standard library
File.open(path, "w") do |file|
file_data = file.read
end
Benchmark.bm do |x|
x.report { for i in 1..n; a = "1"; end }