Skip to content

Instantly share code, notes, and snippets.

View enderahmetyurt's full-sized avatar
🦄

Ender Ahmet Yurt enderahmetyurt

🦄
View GitHub Profile
@enderahmetyurt
enderahmetyurt / can_form_hexagon.rb
Created July 28, 2025 11:08
Given an array of side lengths, write a function to determine they can form a hexagon with three side-length pairs (as in, three pairs of equal sides needed). Return true if possible.
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
@enderahmetyurt
enderahmetyurt / minimum_assembly_time.rb
Created July 21, 2025 12:55
Given an array of parts where each part is { name, arrivalDays, assemblyHours }, return the minimum total hours needed to finish assembling all parts, starting from hour 0.
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
@enderahmetyurt
enderahmetyurt / get_char_after_vim_commands.rb
Created July 14, 2025 09:11
Given a multi-line string and a sequence of Vim navigation commands
# 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)
@enderahmetyurt
enderahmetyurt / grand_finale.rb
Created July 7, 2025 08:56
Given an array of fireworks representing a series going off, write a function to find the "grand finale" of the show!
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
@enderahmetyurt
enderahmetyurt / non_repeat.rb
Created June 30, 2025 10:20
Find the last non-repeating character in a given string.
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
@enderahmetyurt
enderahmetyurt / binding_pry.sublime-snippet
Created April 2, 2019 13:27
Auto complete for binding.pry
<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>