Skip to content

Instantly share code, notes, and snippets.

View amrdruid's full-sized avatar
🌌
Ruby is weird 💎

Druid amrdruid

🌌
Ruby is weird 💎
View GitHub Profile
@amrdruid
amrdruid / angularjs-providers-explained.md
Created December 4, 2015 19:16 — forked from demisx/angularjs-providers-explained.md
AngularJS Providers: Constant/Value/Service/Factory/Decorator/Provider
Provider Singleton Instantiable Configurable
Constant Yes No No
Value Yes No No
Service Yes No No
Factory Yes Yes No
Decorator Yes No? No
Provider Yes Yes Yes

Constant

git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'
@amrdruid
amrdruid / gist:c5586aff9a709aaf40d9ec5c20e6c3d5
Created December 22, 2016 03:05
What I learned : 15/12/2016 - 22/12/2016
Algorithms:
https://en.wikipedia.org/wiki/Pigeonhole_principle
---
Problems I liked:
http://codeforces.com/contest/743/problem/B
---
@amrdruid
amrdruid / linear_search.rb
Created December 23, 2016 09:35
Ruby: Searching an array starting from the last element
n, m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
index = arr.rindex(m)
puts index < 0 ? index : index + 1
def binary_search lowest, highest, m
mid = (lowest + highest) / 2
if $arr[mid] == m
mid + 1
elsif $arr[mid] < m
binary_search mid + 1, highest, m
else
binary_search lowest, mid - 1, m
end
end
@amrdruid
amrdruid / class_ancestors.rb
Created December 25, 2016 22:27
Know all the ancestors of a class in ruby
> a = Array.new
=> []
> a.class.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
@amrdruid
amrdruid / flatten.rb
Created December 26, 2016 11:28
Ruby - Flatten function for Arrays
def flatten arr
arr.each_with_object([]) do |element, result|
result.push *(element.is_a? Array ? flatten(element) : element)
end
end
@amrdruid
amrdruid / unique_in_order.rb
Created January 1, 2017 16:38
takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
def unique_in_order(iterable)
(iterable.instance_of?(String) ? iterable.chars : iterable).chunk { |x| x }.map(&:first)
end
class Node
attr_accessor :data, :next
def initialize(data)
@data = data
@next = nil
end
end
def length(node)
return 0 unless node
@amrdruid
amrdruid / directions_reduction.rb
Created January 5, 2017 21:29
["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"] You can immediatly see that going "NORTH" and then "SOUTH" is not reasonable, better stay to the same place! So the task is to give to the man a simplified version of the plan. A better plan in this case is simply: ["WEST"]
def dirReduc(arr)
check = {north: "SOUTH", south: "NORTH", east: "WEST", west: "EAST"}
res = []
for i in 0...arr.length
if res.last && check[res.last.downcase.to_sym] == arr[i]
res.pop
else
res << arr[i]
end