Skip to content

Instantly share code, notes, and snippets.

View bernEsp's full-sized avatar

Bernardo Espinoza bernEsp

View GitHub Profile
@bernEsp
bernEsp / quick_sort.py
Created September 7, 2017 21:23
python quick_sort
def quick_sort(array):
value = random.choice(array)
l = []
r = []
c = []
for i in array:
if i < value:
l.append(i)
elif i > value:
r.append(i)
@bernEsp
bernEsp / quick_sort.rb
Last active September 8, 2017 15:20
3 Quick sort versions mystery-> with each and validations, misterio_a -> selects and comparing always original array size and misterio -> initializing variable with selected list of elements
def misterio(array)
value = array.sample
l,r,c = array.select{|e| e < value}, array.select{|e| e > value}, []
l = misterio(l) if l.size > 1
r = misterio(r) if r.size > 1
c << value
(l + c + r).compact
end
def misterio_a(array)
@bernEsp
bernEsp / gist:6919f283761c92cc19c4
Created February 6, 2015 17:52
object collection wrap with a presenter that can be render by render method rails 3.0
class FooCollectionPresenter < SimpleDelegator
include Enumerable
def iterate_with_index
self.each_with_index do |foo, index|
yield(FooPresenter.new(foo), index)
end
end
def iterate
self.map do |foo|
@bernEsp
bernEsp / gist:954d4c9514e87300c84c
Last active August 29, 2015 14:09
write c extension
-gem mongo_kerberos-example
ruby data types (18) ruby core objects
t_nil
t_object
t_class
t_string -> RString(c data type) <- RSTRING(functions)
t_array -> RArrray <- RARRAY
t_false
t_symbol
@bernEsp
bernEsp / gist:baad9e63689f64a1ecb2
Created November 5, 2014 07:32
regex to validate email with names and delimiter
Regexp.new('\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})(\z|\s<\w+(?:>\z|\s\w+>\z))', true) |~
Regexp.new('(?:\A|,{1})(?:<{0,1})([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})(?:>{0,1})', true) |~
Regexp.new('(?:\s<|w+\s)([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})(?:>|,{0,1}\z)', true) |~
Regexp.new('(?:^([^<@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})|^(?:\w+\s)+<{1}([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})(?:>{1}$))', true)
Regexp.new('(?:^([^<@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z|^(?:\w+\s)+<{1}([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})(?:>{1}$))', true)
@bernEsp
bernEsp / gist:bbfaa630b0c96c7f052a
Last active August 29, 2015 14:08
Fibonacci algorithm
def self.fib(quantity)
start = [0,1]
quantity.times do
two_numbers = start.last(2)
start << two_numbers.inject{|sum, n| sum +n}
end
start
end
def self.fibonacci(quantity)
@bernEsp
bernEsp / gist:c2b4a6133f762548353b
Last active August 29, 2015 14:08
count all rows in rails app
(ActiveRecord::Base.send :subclasses).inject(0) do |sum, m|
begin
sum + m.all.size
rescue ActiveRecord::StatementInvalid
"no existe"
end
end
ActiveRecord::Base.each_model_class.inject .. add each model class method to active record module on base class
<ol>
<li><p class="P13" style="margin-left:0cm;"><span style="display:block;float:left;min-width:0.635cm;">1.</span>Probando listas numeradas<span class="odfLiEnd"/> </p>
</li>
<li><p class="P1" style="margin-left:0cm;"><span style="display:block;float:left;min-width:0.635cm;">2.</span>con 3 elementos<span class="odfLiEnd"/> </p></li>
<li><p class="P1" style="margin-left:0cm;"><span style="display:block;float:left;min-width:0.635cm;">3.</span>ultimo<span class="odfLiEnd"/> </p></li>
</ol>
<p class="Standard"> </p>
<ul>
<li><p class="P2" style="margin-left:0cm;"><span class="ListLabel_20_1" style="display:block;float:left;min-width:0.635cm;"></span>Probando listas con bullets<span class="odfLiEnd"/> </p>
First version 3 loops / every one populate one column
r = Peasant.new(1233, 3000)
r2 = Peasant.new(1344718,9539271)
r3 = Peasant.new(2345642233123, 1234567890987)
Benchmark.bm do |x|
x.report("4dig: "){ r.multiply }
x.report("7dig: "){ r2.multiply }
x.report("13dig: ") {r3.multiply}
@bernEsp
bernEsp / anagram
Created August 28, 2014 02:29
write a function that receives two list of strings and compare strings in the same index if they are anagram
def anagram(first_word, second_word)
anagrams = []
first_word.each_with_index do |element, index|
anagrams << {"#{element.downcase}" => "#{second_word[index].downcase}" } if element.downcase.sum == second_word[index].downcase.sum
end
anagrams
end