Skip to content

Instantly share code, notes, and snippets.

@dreamr
Last active December 28, 2015 01:19
Show Gist options
  • Save dreamr/e3a79ae604fda87bfa5e to your computer and use it in GitHub Desktop.
Save dreamr/e3a79ae604fda87bfa5e to your computer and use it in GitHub Desktop.
BeQuick code challenge work
#!/usr/bin/env ruby
# encoding: utf-8
# So this is basically a data flow problem
# I could have gotten lambda on it, made it lazy
# and that would speed this up a lot. However it
# not be readable by anyone who isn't into
# functional coding and lambda madness (tm)
def get_sequences(string)
alpha_only = string.gsub(/\d|\W/, '')
0.upto(alpha_only.chars.size).map {|i|
alpha_only[i..i+3].upcase
}.select {|word| word if word.chars.size == 4 }
end
def dict_to_hash(dict_lines)
dict_lines.reduce({}) do |hash, word|
hash.merge(word => get_sequences(word))
end
end
def log_results(hash)
File.delete("words.txt", "sequences.txt")
hash.each do |word, sequences|
next if sequences.none?
File.open("words.txt", "a") {|f| f.puts word }
File.open("sequences.txt", "a") do |f|
sequences.uniq.each {|seq| f.puts seq }
end
end
end
def split_that_shit!
dict_lines = File.readlines("dictionary.txt")
log_results(dict_to_hash(dict_lines))
end
split_that_shit!
require 'minitest/autorun'
describe "#log_results" do
let(:hash) {{
'Aarhus' => %w(AARH ARHU RHUS),
'10th' => [],
'AAAS' => %w(AAAS)
}}
it "must log the sequences" do
log_results(hash)
File.read("words.txt").must_equal(
"Aarhus\nAAAS\n"
)
end
end
describe "#dict_to_hash" do
let(:dictionary) { "Aarhus\n10th\nAAAS\n" }
it "must return a hash" do
dict_to_hash(dictionary).must_equal({
'Aarhus' => %w(AARH ARHU RHUS),
'10th' => [],
'AAAS' => %w(AAAS)
})
end
end
describe "#get_sequences" do
it "will correctly get da sequences for 'Aarhus'" do
get_sequences("Aarhus").must_equal [
"AARH",
"ARHU",
"RHUS"
]
end
it "will correctly get da sequences for '10th'" do
get_sequences("10th").must_equal []
end
it "will correctly get da sequences for 'AAAS'" do
get_sequences("AAAS").must_equal [
"AAAS"
]
end
end
@dreamr
Copy link
Author

dreamr commented Nov 11, 2013

I have 3 code challenges to do today from different companies. So please let me give this disclaimer: This may not be 100% correct with the amount of info I have on the subject. It should be very close and show what I can do.

solution #1: https://gist.github.com/dreamr/e3a79ae604fda87bfa5e
solution #2: https://gist.github.com/dreamr/e67253f1345f06fd5fea

I heavily used enumerators here to speed this up. The difference is this, in the first example I load the whole dict into a string and then go about doing stuff, operating on that giant string (or array). The second gist reads the dict line by line and operates on that level.

Which one I would deploy would depend on several things:

How jr are the coders I am working with?
How much memory is on production
How big can we expect dictionary to be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment