Skip to content

Instantly share code, notes, and snippets.

View kklimuk's full-sized avatar

Kirill Klimuk kklimuk

View GitHub Profile
@kklimuk
kklimuk / 2023.11.12-cassidoo.ts
Last active November 13, 2023 19:09
2023.11.12 - Cassidoo's Interview Question of the Week
// Given a list of tasks, where each task has a duration, and a limited amount of available time to work,
// write a function to return the tasks that can be completed within the given time, without re-ordering
// the original list of tasks.
type Task = { name: string; duration: number };
const tasks = [
{ name: "Task 1", duration: 4 },
{ name: "Task 2", duration: 2 },
{ name: "Task 3", duration: 7 },
{ name: "Task 4", duration: 5 },
class Foo
def call
'hi'
end
end
foo = proc { 'hi' }
Foo.new.call # 'hi'
foo.call # 'hi'
class Notifier
attr_reader :notifier
def initialize(notifier)
raise ArgumentError, 'Expected the notifier to respond to `notify`' unless notifier.respond_to?(:notify)
@notifier = notifier
end
def notify(id, message)
notifier.notify(id, prettify(message))
class Notifier
attr_reader :notifier
def initialize(notifier)
raise ArgumentError, 'Expected the notifier to respond to `notify`' unless notifier.respond_to?(:notify)
@notifier = notifier
end
def notify(id, message)
notifier.notify(id, prettify(message))
module Notifier
 def self.notify(id, message)
  SlackClient.new.notify(id, prettify(message))
 end
def self.prettify(message)
 HTMLCleaner.clean(message.strip)
 end
end
# a proc, which I will insist on calling a function
notifier = proc { |id, message| SlackClient.new.notify(id, message.strip) }
notifier.call('@kirill', 'hello, world ')
def users_with_discounts
@users_with_discounts ||= {}
if @users_with_discounts.has_key?(:return_value)
@users_with_discounts[:return_value]
else
@users_with_discounts[:return_value] = computation_that_returns_nil
end
end
require 'benchmark'
Benchmark.bm do |measurer|
calculator = FibonacciCalculator.new
measurer.report 'with memoization' do
10.times { calculator.memoized_calculate(35) }
end
measurer.report 'without memoization' do
class FibonacciCalculator
include RubyMemoized
def calculate(n)
return n if (0..1).include?(n)
calculate(n - 1) + calculate(n - 2)
end
memoized
module RubyMemoized
class Memoizer
attr_reader :context, :method
def initialize(context, method)
@context = context
@method = method
end
def call(*args, &block)