Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Last active February 22, 2016 22:09
Show Gist options
  • Save joelibaceta/04be119254c9b01a5fe0 to your computer and use it in GitHub Desktop.
Save joelibaceta/04be119254c9b01a5fe0 to your computer and use it in GitHub Desktop.
Fragmentos
# Generar hashtag de un texto
# " Hello World " => "#HelloWorld"
def generateHashtag(str)
str = "#" << str.split.map(&:capitalize).join
str.size <= 140 && str.size > 1 ? str : false
end
def alphanumeric?(string)
return false if string == ""
string =~ /\A[[:alnum:]]+\z/ ? true : false
end
# Listar nombres con el formato "Joel, Mario, Jason y Carlos"
def list *names
names.join(', ').gsub(/, (\w+)$/, " y \\1")
end
# One line form
def list *names; names.join(', ').gsub(/, (\w+)$/, " y \\1"); end
# Obtener el siguiente numero con los mismos digitos
def next_bigger(n)
next_bigger = 0
values = n.to_s.chars.to_a.permutation.to_a.map{|a| a.join}.uniq.sort
values.map.with_index{|v, i| next_bigger = values[i+1].to_i if v.to_i == n }
return next_bigger
end
# Obtener los primeros x numeros primos de forma optima
def get_primes x
primes=[]
(2..x).each{ |n| primes.any?{|l| n%l==0 } ? nil : primes.push(n) }
return primes
end
def rgb(r, g, b)
"%.2X%.2X%.2X" % [r,g,b].map{|i| [[i,255].min,0].max}
end
#Valida soluciones de sudoku con la forma
validSolution([[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]])
def validSolution(board)
(0..8).map{|i| board[i].reduce(:+) == board.transpose[i].reduce(:+) && board.transpose[i].uniq.count == 9 && board.transpose != board}.flatten.all?
end
def validate(email)
(email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) != nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment