This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+----------------+------------------------+----------------+ | |
| 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 | | |
+----------------+-----------------------------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ...} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique_web_developer(s) | |
s.split("").uniq.length == s.length | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Performing the "|" operation | |
100000 | |
11000 | |
______ | |
111000 | |
2) Assinging the result to vector | |
vector = 111000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100000 -> shifted value result of (1 << 5) | |
11000 -> characters with code 3 and 4 have been seen already. | |
______ | |
000000 = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique_with_bit_array(s) | |
vector = 0 | |
s.each_char do |char| | |
code = char.ord | |
if (vector & (1 << code)) > 0 | |
return false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique_with_hash2(s) | |
characters = {} | |
s.each_char do |char| | |
return false if characters[char] | |
characters[char] = true | |
end | |
return true |