Skip to content

Instantly share code, notes, and snippets.

View eduardopoleo's full-sized avatar

Eduardo Poleo eduardopoleo

View GitHub Profile
def add_two(a, b)
a.to_i + b.to_i
end
add_two(1, 1)
describe '#add_two' do
context 'when I add two positive numbers' do
def produce_all(connection)
count = Outbox::Adapters::General::Messages.count
batches = (count / batch_size.to_f).ceil
batches.times do
messages = Outbox::Adapters::General.order(:id).limit(batch_size).to_a
grouped_messages = group_by_model_type(messages)
while messages_to_process?(grouped_messages)
+----------------+------------------------+----------------+
| Solution | Elapsed Real Time (s)* | Space(bytes)** |
+----------------+------------------------+----------------+
| Brute Force | 168.1 | N/A |
| Hash | 9.8 | 6488 |
| Array | 3.14 | 1064 |
| Bit Array | 6.2 | 40 |
| Web Developer | 9.7 | 2128 |
+----------------+-----------------------------------------+
require 'objspace'
# "111...".to_i(2) A 128 "1"
big_int = 340282366920938463463374607431768211455
ObjectSpace.memsize_of(big_int) # => 40
array = [true] * 128
ObjectSpace.memsize_of(big_int) # => 1064
hash = {"a" => true, "b" => true ...}
def unique_web_developer(s)
s.split("").uniq.length == s.length
end
1) Performing the "|" operation
100000
11000
______
111000
2) Assinging the result to vector
vector = 111000
100000 -> shifted value result of (1 << 5)
11000 -> characters with code 3 and 4 have been seen already.
______
000000 = 0
def unique_with_bit_array(s)
vector = 0
s.each_char do |char|
code = char.ord
if (vector & (1 << code)) > 0
return false
end
def unique_with_array(s)
char_set = Array.new
s.each_char do |char|
code = char.ord
return false if char_set[code]
char_set[code] = true
end
def unique_with_hash2(s)
characters = {}
s.each_char do |char|
return false if characters[char]
characters[char] = true
end
return true