Skip to content

Instantly share code, notes, and snippets.

View saadbinakhlaq's full-sized avatar
🕴️
up

Saad saadbinakhlaq

🕴️
up
View GitHub Profile
=begin
Write a function, largest_component, that takes in the adjacency list of an undirected graph.
The function should return the size of the largest connected component in the graph.
5
| \
| \
1-- 0 -- 8
4 -- 2
\ /
=begin
Write a function, connected_components_count, that takes in the adjacency list of an undirected graph.
The function should return the number of connected components within the graph.
=end
# 1 - 2
# 4
# |
# 5 -- 6 -- 8
# |
# 7
=begin
Write a function, undirected_path, that takes in a list of edges for an undirected graph and two nodes (node_A, node_B).
The function should return a boolean indicating whether or not there exists a path between node_A and node_B.
=end
edges = [
['i', 'j'],
['k', 'i'],
['m', 'k'],
['k', 'l'],
=begin
Write a function, has_path, that takes in a dictionary representing the adjacency list of a directed acyclic graph and two nodes (src, dst).
The function should return a boolean indicating whether or not there exists a directed path between the source and destination nodes.
=end
graph = {
'f' => ['g', 'i'],
'g' => ['h'],
'h' => [],
'i' => ['g', 'k'],
@saadbinakhlaq
saadbinakhlaq / apartment_search_berlin.md
Last active July 10, 2017 09:45
Overview of apartment search in Berlin
module BSON
class ObjectId
def to_json(*args)
to_s.to_json
end
def as_json(*args)
to_s.as_json
end
end
require 'spec_helper'
describe KV do
describe '#set' do
it 'assigns a value to the instance variable hash' do
kv = KV.new
kv.set('foo', 'bar')
expect(kv.hash['foo'][Time.now.to_i]).to eq('bar')
end
end
Dir[File.dirname(__FILE__) + '/application/*.rb'].each { |file| require file }
class KV
attr_reader :hash
def initialize
@hash = Hash.new { |h, k| h[k] = {} }
end
def set(key, value)
@hash[key][Time.now.to_i] = value
@saadbinakhlaq
saadbinakhlaq / attachment.rb
Created May 21, 2016 08:52 — forked from madwork/attachment.rb
Polymorphic attachments with CarrierWave and nested_attributes
class Attachment < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
# Associations
belongs_to :attached_item, polymorphic: true
# Validations
validates_presence_of :attachment