This file contains hidden or 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 can_form_hexagon(sides) | |
return false unless sides.size == 6 | |
tally = sides.tally | |
return false unless tally.size == 3 && tally.values.all?(2) | |
a, b, c = tally.keys.sort | |
[a, b, c].all? { |x| x < (a + b + c - x) } | |
end |
This file contains hidden or 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 minimum_assembly_time(parts) | |
parts | |
.map { |part| part.merge(arrival_hours: part[:arrival_days] * 24) } | |
.sort_by { |part| [part[:arrival_hours], part[:assembly_hours]] } | |
.reduce(0) { |time, part| [time, part[:arrival_hours]].max + part[:assembly_hours] } | |
end |
This file contains hidden or 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
# h for left, j for down, k for up, and l for right) | |
def get_char_after_vim_commands(string, commands) | |
lines = string.lines.map(&:chomp) | |
row, col = 0, 0 | |
commands.each_char do |cmd| | |
dr, dc = {"h" => [0, -1], "j" => [1, 0], "k" => [-1, 0], "l" => [0, 1]}[cmd] || [0, 0] | |
row = (row + dr).clamp(0, lines.size - 1) | |
col = (col + dc).clamp(0, [lines[row].size - 1, 0].max) |
This file contains hidden or 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 grand_finale_start(fireworks) | |
best = { start: nil, length: 0 } | |
(0...fireworks.size).each do |start_idx| | |
fireworks[start_idx..].each_with_index do |_, offset| | |
window = fireworks[start_idx..start_idx + offset] | |
next unless valid_window?(window) | |
if window.size > best[:length] | |
best[:start] = start_idx |
This file contains hidden or 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 non_repeat(str) | |
counts = Hash.new(0) | |
str.each_char { |char| counts[char] += 1 } | |
str.reverse.each_char do |char| | |
return char if counts[char] == 1 | |
end | |
"" | |
end |
This file contains hidden or 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
<snippet> | |
<content><![CDATA[ | |
binding.pry | |
]]></content> | |
<!-- Optional: Set a tabTrigger to define how to trigger the snippet --> | |
<tabTrigger>bp</tabTrigger> | |
<!-- Optional: Set a scope to limit where the snippet will trigger --> | |
<scope>source.ruby, source.ruby.rails, source.text.haml</scope> | |
<description>auto complete for binding.pry</description> | |
</snippet> |