Skip to content

Instantly share code, notes, and snippets.

View nazgob's full-sized avatar
🎯
Focusing

Przemek Owczarek nazgob

🎯
Focusing
View GitHub Profile
@nazgob
nazgob / birds_classic.cpp
Created February 27, 2011 15:35
"classic" OO solution for birds problem
#include <iostream>
class Bird
{
public:
virtual void Fly() const = 0;
virtual void Speak() const = 0;
};
class Eagle : public Bird
@nazgob
nazgob / timer.rb
Created December 7, 2010 21:45
timer in ruby shoes
Shoes.app :height => 150, :width => 250 do
background rgb(240, 250, 208)
stack :margin => 10 do
button "Start" do
@time = Time.now
@label.replace "Stop watch started at #@time"
end
button "Stop" do
@label.replace "Stopped, ", strong("#{Time.now - @time}"), " seconds elapsed."
end
@nazgob
nazgob / Rakefile_for_cpp.rb
Created November 25, 2010 10:22
sample Rakefile for building c++ project / inc lib / .rb extension just for GH to color stuff
require 'rake/clean'
PROG = "foo"
LIBNAME = PROG
LIBFILE = "lib#{LIBNAME}.a"
SRC = FileList['**/*.cpp']
OBJDIR = 'obj'
OBJ = SRC.collect { |fn| File.join(OBJDIR, File.basename(fn).ext('o')) }
@nazgob
nazgob / Rakefile
Created November 14, 2010 00:30
simple/sample Rakefile for C++
# constants
COMPILER = "g++"
EXEC = "hello"
FLAGS = "-Wall -Wextra"
OBJECTS = ['hello.o', 'add.o']
file 'hello' => OBJECTS
# tasks
TEST_F(SExpTest, SExpWithOperator)
{
// (2 3)
SExp* valuePair = new SExp(a, b);
// (+, (2 3)) = 5
SExp* addOpWithValue = new SExp(new AddOperator(), valuePair);
EXPECT_EQ(5, addOpWithValue->Evaluate(context));
delete addOpWithValue;
@nazgob
nazgob / roman_num.rb
Created July 11, 2010 13:55
Roman numbers / method missing
class Roman
def self.method_missing name, *args
roman = name.to_s
roman.gsub!("IV" , "IIII" )
roman.gsub!("IX" , "VIIII" )
roman.gsub!("XL" , "XXXX" )
roman.gsub!("XC" , "LXXXX" )
(roman.count("I" ) +
roman.count("V" ) * 5 +
roman.count("X" ) * 10 +
@nazgob
nazgob / to_file.rb
Created July 1, 2010 13:57
module snippet supporting "serialization"
#!/usr/bin/ruby
module ToFile
def filename
"object_#{self.object_id}.txt"
end
def to_f
File.open(filename, 'w') {|f| f.write(to_s)}
end
@nazgob
nazgob / ruby_tree.rb
Created July 1, 2010 12:54
tree like structure with blocks and recursion
#!/usr/bin/ruby
class Tree
attr_accessor :children, :node_name
def initialize(name, children)
@children = children
@node_name = name
end
@nazgob
nazgob / atomic.c
Created June 3, 2010 17:31
atomic variables on gcc / linux
// http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables
// gcc -lpthread -D_GNU_SOURCE -o atomic atomic.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
@nazgob
nazgob / Stirling number of the second kind
Created March 18, 2010 21:38
Stirling number of the second kind
#include <iostream>
size_t S(size_t n, size_t k)
{
if(k > n)
{
return 0;
}
else if(k == 1)
{