Skip to content

Instantly share code, notes, and snippets.

View marcinwyszynski's full-sized avatar

Marcin Wyszynski marcinwyszynski

View GitHub Profile
@marcinwyszynski
marcinwyszynski / power_of_two.rb
Created January 12, 2012 01:09
Checking if an integer is a power of 2
Integer::class_eval do
def power_of_2?() (self & (self - 1)) == 0 end
end
@marcinwyszynski
marcinwyszynski / priority_queue.py
Created January 12, 2012 12:47
A simple PriorityQueue in Python
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]
@marcinwyszynski
marcinwyszynski / priority_queue.rb
Created January 12, 2012 12:50
A simple PriorityQueue in Ruby
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
@marcinwyszynski
marcinwyszynski / anagrams.py
Created January 12, 2012 15:12
Anagram finder for Unix-like systems
#!/usr/bin/env python
import sys
try:
word = sys.argv[1]
except IndexError:
print 'usage: anagrams.py <word>'
exit(1)
@marcinwyszynski
marcinwyszynski / array_hash_extras.rb
Created February 19, 2012 15:29
Hash::reverse and Array::summarize
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
@marcinwyszynski
marcinwyszynski / README.md
Created February 24, 2012 10:02
PriorityQueue

Description and usage:

require ''
.new

Running the tests

Install the rspec gem

@marcinwyszynski
marcinwyszynski / pwd.rb
Created February 24, 2012 17:35
Shell built-in 'pwd' implemented in Ruby
#!/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
@marcinwyszynski
marcinwyszynski / cdp.sh
Created April 12, 2012 13:57
Shell function to skip to a parent directory
# 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
}
@marcinwyszynski
marcinwyszynski / named_captures.go
Created October 14, 2012 19:28
Named captures in Go
package main
import (
"fmt"
"regexp"
)
func GetNamedMatches(exp *regexp.Regexp,
src string, limit int) map[string][]interface{} {
result := make(map[string][]interface{})
@marcinwyszynski
marcinwyszynski / set.go
Created October 17, 2012 12:34
Set in Go
package main
import "fmt"
type SetNone struct{}
type Set map[interface{}]SetNone
func (s Set) Add(el interface{}) {
s[el] = SetNone{}
}