Skip to content

Instantly share code, notes, and snippets.

View rafaelbernard's full-sized avatar

Rafael Bernard Araújo rafaelbernard

View GitHub Profile
<?php
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
// difference |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
function solution($A) {
// write your code in PHP5.5
$n = count($A);
<?php
function solution($A) {
// write your code in PHP5.5
$count = count($A);
if ($count < 1) return 1;
$end = $count + 1;
$ordered = range(1, $end);
<?php
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$a_count_values = array_count_values($A);
$values = array_keys($a_count_values, 1);
if (is_array($values))
return $values[0];
<?php
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($X, $Y, $D) {
// write your code in PHP5.5
if ($X == $Y) return 0;
$jumps = intval(ceil(($Y - $X) / $D));
return $jumps;
<?php
function solution($N) {
//print "N: $N\n";
// write your code in PHP5.5
$bin = base_convert($N, 10, 2);
$sbin = trim((string) $bin);
//print "sbin: $sbin\n";
$any_zero = strpos($sbin, '0');
<?php
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A, $K) {
// write your code in PHP5.5
$count = count($A);
$length_offset = $count - $K;
if ($length_offset < 0) {
$quo = intval($K / $count);
-- Function: desacentua(character varying)
-- DROP FUNCTION desacentua(character varying);
CREATE OR REPLACE FUNCTION desacentua(texto character varying)
RETURNS character varying AS
$BODY$
SELECT translate( $1,'ÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛÄËÏÖÜÇÑáéíóúàèìòùãõâêîôûäëïöüçñ','AEIOUAEIOUAOAEIOUAEIOUCNaeiouaeiouaoaeiouaeioucn')
$BODY$
LANGUAGE sql IMMUTABLE
@rafaelbernard
rafaelbernard / get_locks.sql
Last active January 25, 2016 11:43
get_locks
select locktype, database, relation,page,tuple,virtualxid,transactionid,classid,objid,objsubid,virtualtransaction,pid
, mode,granted,datname,datdba,datctype,datistemplate,datallowconn,datconnlimit,datlastsysoid,datfrozenxid,dattablespace,datacl
, c.relname as table_name
from pg_locks l
join pg_database d
on d.oid = l.database
join pg_class c
on c.oid = l.relation;
@rafaelbernard
rafaelbernard / tables_at_buffercache.sql
Last active August 29, 2015 14:07
Tabelas com maior quantidade de registros em cache - PostgreSQL
SELECT d.datname, c.relname
, pg_size_pretty(count(*) * 8192) as buffered
, round(100.0 * count(*) / ( SELECT setting FROM pg_settings WHERE name='shared_buffers')::integer,1) AS buffers_percent
, round(100.0 * count(*) * 8192 / pg_relation_size(c.oid),1) AS percent_of_relation
FROM pg_class c
INNER JOIN pg_buffercache b ON b.relfilenode = c.relfilenode
INNER JOIN pg_database d ON (b.reldatabase = d.oid)
WHERE pg_relation_size(c.oid) > 0
GROUP BY c.oid, d.datname, c.relname
ORDER BY 4 DESC
@rafaelbernard
rafaelbernard / recover-privileges.sh
Created June 4, 2014 02:47
Recover privileges for cPanel account
#!/bin/bash
user=$1
if [ "$user" != "" ]; then
echo "Starting recovering privileges"
echo "1. Recovering /home/${user} ${user}:${user}"
chown -R ${user}:${user} /home/${user}
echo "2. Recovering /home/${user}/etc ${user}:mail"
chown -R ${user}:mail /home/${user}/etc
echo "3. Recovering /home/${user}/.htpasswds ${user}:nobody"