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 / 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
@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 / 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]
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 / 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
@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
---
git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'
@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