Description and usage:
require ''
.new
Install the rspec gem
| Integer::class_eval do | |
| def power_of_2?() (self & (self - 1)) == 0 end | |
| end |
| class PriorityQueue(object): | |
| def __init__(self): | |
| self.store = {} | |
| def enq(self, element, weight): | |
| if weight in self.store: | |
| self.store[weight].append(element) | |
| else: | |
| self.store[weight] = [element] |
| class PriorityQueue | |
| def initialize() @store = {} end | |
| # Takes element and it's priority. | |
| # Returns an array of elements with the same priority. | |
| def enq(el, p) | |
| @store[p] ? @store[p].push(el) : @store[p] = [el] | |
| return @store[p] | |
| end |
| #!/usr/bin/env python | |
| import sys | |
| try: | |
| word = sys.argv[1] | |
| except IndexError: | |
| print 'usage: anagrams.py <word>' | |
| exit(1) |
| require 'test/unit' | |
| Hash::class_eval do | |
| # Flip the Hash so that former keys become values and | |
| # former values become keys. The difference however is | |
| # that if one value is assigned to multiple keys, then | |
| # in the reversed Hash you get an array of values under | |
| # that key. | |
| def reverse |
| #!/usr/bin/env ruby | |
| =begin | |
| The concept here is to print out the current working | |
| directory (pwd) following i-nodes only. | |
| =end | |
| def pwd | |
| path = [] | |
| loop do |
| # Go upwards the path until the basename is equal | |
| # to your argument. | |
| function cdp { | |
| CURRENT=$(basename $PWD) | |
| until [ $CURRENT == "$*" ]; do | |
| cd .. | |
| CURRENT=$(basename $PWD) | |
| done | |
| } |
| package main | |
| import ( | |
| "fmt" | |
| "regexp" | |
| ) | |
| func GetNamedMatches(exp *regexp.Regexp, | |
| src string, limit int) map[string][]interface{} { | |
| result := make(map[string][]interface{}) |
| package main | |
| import "fmt" | |
| type SetNone struct{} | |
| type Set map[interface{}]SetNone | |
| func (s Set) Add(el interface{}) { | |
| s[el] = SetNone{} | |
| } |