Skip to content

Instantly share code, notes, and snippets.

View astropanic's full-sized avatar

Wojciech Pietrzak astropanic

View GitHub Profile
CHARS64 = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + ["-", "_"]
# Return a 22 byte URL-safe string, encoded six bits at a time using 64 characters
def to_s22
integer = self.to_i # UUID as a raw integer
rval = ""
22.times do
c = (integer & 0x3F)
rval += CHARS64[c]
integer = integer >> 6
end
#Time limit exceeded
class Palindrome
def compute(value)
@input_array = value
@size = @input_array.size
@even = @size % 2 == 0
if @even
@pos1 = (@size/2)-1
@pos2 = @pos1+1
#Time limit exceeded
class Palindrome
def compute(value)
@input_array = value
@size = @input_array.size
@even = @size % 2 == 0
if @even
@pos1 = (@size/2)-1
@pos2 = @pos1+1
class Array
def <=(value)
@self_size = self.size
@value_size= value.size
return false if @self_size > @value_size
tmp = true
for iterator in @self_size-1..0
tmp = false if self[iterator] > value[iterator]
end
#SIGABRT
class Palindrome
def compute(value)
@size = value.size
@even = @size % 2 == 0
if @even
@pos1 = (@size/2)-1
@pos2 = @pos1+1
else
class String
def compute
@size = self.size
@even = @size % 2 == 0
if @even
@pos1 = (@size/2)-1
@pos2 = @pos1+1
else
@pos1 = (((@size+1)/2)-1)
class String
def compute
@size = self.size
@even = @size % 2 == 0
if @even
@pos1 = (@size/2)-1
@pos2 = @pos1+1
else
@pos1 = (((@size+1)/2)-1)
desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :routes => :environment do
all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes
routes = all_routes.collect do |route|
name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s
verb = route.conditions[:method].to_s.upcase
segs = route.segments.inject("") { |str,s| str << s.to_s }
segs.chop! if segs.length > 1
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => name, :verb => verb, :segs => segs, :reqs => reqs}
desc 'Print out all unused routes'
task :unused => :environment do
all_routes = ActionController::Routing::Routes.routes
routes = all_routes.collect do |route|
name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s
verb = route.conditions[:method].to_s.upcase
segs = route.segments.inject("") { |str,s| str << s.to_s }
segs.chop! if segs.length > 1
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => name, :verb => verb, :segs => segs, :reqs => reqs}
class Array
def <=(value)
size_self = self.size
size_value = value.size
return false if self.size > value.size
return true if self.size < value.size
value.each_with_index do |value, i|
return false if value < self[i]
end
return true