Skip to content

Instantly share code, notes, and snippets.

View thdaraujo's full-sized avatar
🧡
coding

Thiago Araujo thdaraujo

🧡
coding
View GitHub Profile
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MostFrequentSubstringApp
{
@thdaraujo
thdaraujo / cyclic_rotation.rb
Created April 7, 2018 18:17
Cyclic rotation of array k times
def cyclic_rotation(a, k)
size = a.size
return a if k == size || k == 0
result = a.clone
a.each_with_index{|e, i|
result[(i + k) % size] = e
}
result
end
@thdaraujo
thdaraujo / camera-plugin-ionic.js
Created January 25, 2016 18:40
Add camera plugin on ionic project
/* WARNING: it won`t work on your browser/development, only works on your phone! */
//sources:
// http://stackoverflow.com/questions/27845345/ionic-cordova-camera-not-working
/* install ionic and cordova */
$ sudo npm install -g ionic cordova
/* create ionic app */
$ ionic start myApp
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def to_s
"(#{@x}, #{@y})"
end
end
@thdaraujo
thdaraujo / rails_pg_timezone_lookup.sql
Created April 5, 2018 23:14
Mapping between rails timezones (names) and their ISO definition on postgres.
-- see http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html MAPPING
WITH timezone_lookup(rails_timezone, pg_timezone)
AS (
VALUES
('International Date Line West', 'Pacific/Midway'),
('Midway Island', 'Pacific/Midway'),
('American Samoa', 'Pacific/Pago_Pago'),
('Hawaii', 'Pacific/Honolulu'),
('Alaska', 'America/Juneau'),
('Pacific Time (US & Canada)', 'America/Los_Angeles'),
@thdaraujo
thdaraujo / max_binary_gap.rb
Last active April 8, 2018 02:07
Calculates the max binary gap of an integer.
# Returns the maximum binary gap of a number.
# e.g.: max binary gap of 529 (1000010001 in binary) is 4.
#
def max_binary_gap(n)
return 0 if (n < 1)
result = n.to_s(2).scan(/10+1/).map{|m|
m.gsub('1', '').size
}.select{|gap|
gap >= 0
# find the shortest knight path
# in a game of chess on an infinite board
def knight_moves(a, b)
ratio = a.to_f / b.to_f
unless (((ratio >= 0.5 && ratio <= 2.0) || (ratio <= -0.5 && ratio >= -2.0)) && ((a+b)% 3 == 0))
puts 'unreachable'
-1
else
(a+b)/3
@thdaraujo
thdaraujo / tc_tweetstorm.rb
Last active April 8, 2018 02:07
Tweet Storm generator: divides a giant text into small tweets of 140 characters.
#unit test
#how to run: $ ruby tc_tweetstorm.rb
require_relative "tweetstorm"
require "minitest/autorun"
class TestTweetStorm < Minitest::Test
def setup
@thdaraujo
thdaraujo / inbox.sol
Last active April 8, 2018 02:07
A simple smart contract with Solidity.
pragma solidity ^0.4.17;
contract Inbox{
string public message;
function Inbox(string initialMessage) public {
message = initialMessage;
}
function setMessage(string newMessage) public {
@thdaraujo
thdaraujo / spacemacs-keybindings
Created July 11, 2017 12:52 — forked from adham90/spacemacs-keybindings
spacemacs keybindings that i need to learn
SPC s c remove highlight
**** Files manipulations key bindings
Files manipulation commands (start with ~f~):
| Key Binding | Description |
|-------------+----------------------------------------------------------------|
| ~SPC f c~ | copy current file to a different location |
| ~SPC f C d~ | convert file from unix to dos encoding |
| ~SPC f C u~ | convert file from dos to unix encoding |