View singleton.py
class Singleton(object): | |
def __new__(cls): | |
if not cls.singleton: | |
cls.singleton = object.__new__(cls) | |
return cls.singleton | |
class Foo(Singleton): | |
singleton = None | |
pass |
View hook.py
class Hooks(object): | |
@staticmethod | |
def before(name): | |
def decorator(func): | |
def wrap(self, *args): | |
self.__getattribute__(name)() | |
func(self, *args) | |
return wrap | |
return decorator |
View recurring.rb
# Project Euler 26 | |
def recurring(number) | |
remainders = Hash.new(0) | |
value = 1 | |
position = 0 | |
length = 0 | |
while value != 0 && remainders[value] == 0 | |
remainders[value] = position |
View anagram.rb
def anagram?(s1, s2) | |
hash = Hash.new(0) | |
s1.each_char do |c| | |
hash[c] += 1 | |
end | |
s2.each_char do |c| | |
hash[c] -= 1 | |
end | |
hash.values.uniq == [0] | |
end |
View multiple-string.rb
#!/usr/bin/env ruby | |
class String | |
def *(str) | |
maxshift = self.length + str.length - 2 | |
results = Array.new(maxshift+1, 0) | |
self.split(//).map(&:to_i).each_with_index do |i, at1| |
View double-square-number.rb
#!/usr/bin/env ruby | |
def integer?(number) | |
(number % 1) == 0 | |
end | |
def double_square_number?(number) | |
sqrt = Math.sqrt(number) | |
if integer?(sqrt) | |
[sqrt.to_i, 0] |
View anagrams.rb
#!/usr/bin/env ruby | |
def hash(string) | |
hash = {} | |
string.each_char do |c| | |
hash[c] = hash[c].to_i + 1 | |
end | |
return hash | |
end |
View print-edge.rb
#!/usr/bin/env ruby | |
class Node | |
attr_accessor :data, :left, :right | |
def initialize(data, left = nil, right = nil) | |
@data = data | |
@left = left | |
@right = right | |
end |
View hash.elisp
(setq h (make-hash-table :test 'equal)) | |
(message "%s" h) | |
(puthash 'hello "world" h) | |
(message "%s" h) | |
(message "%s" (gethash 'hello h)) |
View extconf.rb
require "mkmf" | |
$libs += " -lstdc++ " | |
create_makefile("human") |
NewerOlder