How to check N is a power of 2 ?
def is_a_power_of_2?(n)
puts "#{n} is a power of 2!" if n.bit_length != (n-1).bit_length
end| # SPOJ - PRIME1 http://www.spoj.com/problems/PRIME1/ | |
| # | |
| # *NOTE* | |
| # | |
| # 1. Presieve the primes under sqrt(1_000_000_000). | |
| # 2. Multiple the primes to flag numbers which are not primes. | |
| # 3. Sieve in given range. | |
| primes = [2] |
How to check N is a power of 2 ?
def is_a_power_of_2?(n)
puts "#{n} is a power of 2!" if n.bit_length != (n-1).bit_length
endfrom Binary and Bitwise Operations in Ruby by CooperPress http://goo.gl/4uQY7f
How to represent base 2, 8, 10, 16 in Integer?
| HOST = "" | |
| APP_KEY = "" | |
| APP_SECRET = "" | |
| REQUEST_TOKEN = "" | |
| REQUEST_TOKEN_SECRET = "" | |
| REQUEST_TOKEN_VERIFIER = "" | |
| ACCESS_TOKEN = "" | |
| ACCESS_TOKEN_SECRET = "" |
| git filter-branch -f --commit-filter ' | |
| if [ "$GIT_COMMITTER_NAME" = "<Old name>" ]; | |
| then | |
| GIT_COMMITTER_NAME="<New name>"; | |
| GIT_COMMITTER_EMAIL="<New email>"; | |
| GIT_AUTHOR_NAME="<New name>"; | |
| GIT_AUTHOR_EMAIL="<New email>"; | |
| else | |
| git commit-tree "$@"; | |
| fi' -- --all |
There is a weird situation I haven't noticed before:
class Counter
attr_accessor :processed, :processed_names
def initialize
@processed = 0
@processed_names = []
end| # API | |
| # | |
| # + push | |
| # + top | |
| # + pop | |
| # + empty | |
| # + empty? | |
| ArrayStack = Struct.new(:values) do | |
| def push(value) |
csrf.js
var csrftk = $("meta[name='csrf-token']").attr("content");
// Ajax set
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader("X-CSRF-Token", csrftk);
});from http://blog.honeybadger.io/advanced-ruby-hash-techniques/
h = Hash.new { |hash, key| raise ArgumentError.new("No hash key: #{ key }") }
h[:a]=1
h[:a] # 1
h[:x] # raises ArgumentError: No hash key: xhttp://stackoverflow.com/questions/3512471/non-capturing-group
str = "http://stackoverflow.com/questions/tagged/regex"
str =~ /(http|ftp):\/\/([^\/\r\n]+)(\/[^\r\n]*)?/
puts $1 # => http
# with ?: