Skip to content

Instantly share code, notes, and snippets.

@ltw
Last active December 21, 2015 03:39
Show Gist options
  • Select an option

  • Save ltw/6243688 to your computer and use it in GitHub Desktop.

Select an option

Save ltw/6243688 to your computer and use it in GitHub Desktop.
Helping Tristan
def get_bucket(buckets_filled, max_buckets, idx)
quot, rem = idx.divmod(2)
quot = rem == 1 ? quot + (max_buckets/2) : quot
quot > buckets_filled ? :B : :A
end
p get_bucket(11, 12, 8) == :A ? "OH YEAH 1" : "shit 1" # AAAAAAAAAAAB
p get_bucket( 1, 12, 11) == :B ? "OH YEAH 2" : "shit 2" # ABBBBBBBBBBB
p get_bucket(11, 20, 1) == :A ? "OH YEAH 3" : "shit 3" # AAABABABABABABABABAB
p get_bucket(11, 37, 1) == :B ? "OH YEAH 4" : "shit 4" # ABABABABABABABABABABABBBBBBBBBBBBBBBB
p get_bucket( 5, 20, 3) == :B ? "OH YEAH 5" : "shit 5" # ABABABABABBBBBBBBBBB
def make_bucket_array(buckets_filled, max_buckets)
buckets = max_buckets.times.map { :B }
(0..(buckets_filled-1)).map do |i|
idx = i >= (max_buckets/2) ? ((i % (max_buckets/2)) * 2 + 1) : i * 2
buckets[idx] = :A
end
buckets
end
p make_bucket_array(11, 12).join('') == "AAAAAAAAAAAB"
p make_bucket_array( 1, 12).join('') == "ABBBBBBBBBBB"
p make_bucket_array(11, 20).join('') == "AAABABABABABABABABAB"
p make_bucket_array(11, 37).join('') == "ABABABABABABABABABABABBBBBBBBBBBBBBBB"
p get_bucket(11, 12, 8) == make_bucket_array(11, 12)[ 8]
p get_bucket( 1, 12, 11) == make_bucket_array( 1, 12)[11]
p get_bucket(11, 20, 1) == make_bucket_array(11, 20)[ 1]
p get_bucket(11, 37, 1) == make_bucket_array(11, 37)[ 1]
p get_bucket( 5, 20, 3) == make_bucket_array( 5, 20)[ 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment