Skip to content

Instantly share code, notes, and snippets.

class Array
def bsearch(e, l = 0, u = length - 1)
if l <= u
m = (l + u)/2
e < self[m] ? u = m - 1 : l = m + 1
e == self[m] ? m : bsearch(e, l, u)
end
end
end
require 'nokogiri'
xml = <<EOS
<html>
<body>
<h1><a href="#">This is title</a></h1>
<p>
Hello <i>world</i>!
This is <em>another</em> line.
<p><h3>And a paragraph <em>with</em> a heading.</h3></p>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:param name="indent-increment" select="' '"/>
<xsl:template name="newline">
<xsl:text disable-output-escaping="yes">
</xsl:text>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
# encoding: UTF-8
class HolidayInfo
def initialize date
@date = date
@is_national = false
@name = ""
@is_substitute = false
end
attr_accessor :date, :is_national, :name, :is_substitute
@ogijun
ogijun / gist:1869060
Created February 20, 2012 12:48
Guessing the encoding of given string by trying only Japanese major encodings.
def ugly_guess_jp str
%w[UTF-8 EUC-JP Shift_JIS eucJP-ms Windows-31J].find do |encoding|
begin
tested = str.force_encoding(encoding)
tested.valid_encoding? && tested.encode('UTF-8').valid_encoding?
rescue Encoding::UndefinedConversionError => e
nil
end
end
end
aj = [[0,1,2],[2,0,1],[1,2,0]]
n = 100
m = 10
while (n > 0)
hm = m/2
(1..(m-6)/4).each { |a| a3 = a**3
((a+1)..(((m2=m-a)-3)/3)).each { |b| b3 = b**3
# a+b+c+d == m; a+d == b+c (mod 6) => c == m/2-b (mod 3)
c = b+1; c += aj[c%3][(hm-b)%3]
upper_c = ((m3=m2-b)-1)/2
-- Ramanujan Numbers:
-- c.f. http://www.ipl.t.u-tokyo.ac.jp/~onoue/pub/jssst01.pdf
module Main where
main = print (take 10 (ramanujan (\(a,b) -> a^3 + b^3)))
ramanujan f = [(x,y) | (x,y)<- zip s (tail s), f x == f y] where s = fsort f 1
fsort f k = (k,k) : fmerge f [(k,b) | b<-[k+1..]] (fsort f (k+1))
fmerge f (x:xs) (y:ys)
| f x <= f y = x : fmerge f xs (y:ys)
| otherwise = y : fmerge f (x:xs) ys
require 'curb'
require 'net/http'
require 'open-uri'
require 'benchmark'
Net::HTTP.version_1_2
n = 100
Benchmark.bm do |x|
#compdef heroku
local state ret=1
local -a cmds
cmds=(
"version:show the gem version" \
"list:list your apps" \
"create:create a new app" \
"keys:show your user's public keys" \
"keys\:add:add a public key" \