Skip to content

Instantly share code, notes, and snippets.

View ox's full-sized avatar
👹
what is this, AIM?

Artem Titoulenko ox

👹
what is this, AIM?
View GitHub Profile
@ox
ox / Anagram.java
Created October 18, 2011 17:58
See if 2 words are anagrams
// compile it with:
// javac Anagram.java
// run it with:
// java Anagram word1 word2
// where word1 and word2 are words of your choosing
public class Anagram {
public static boolean is_anagram(String a, String b) {
System.out.println("a: " + a + ", b: " + b );
//if a == b or if their lengths are 0 then we are done and can return true
@ox
ox / Palindrome.java
Created October 18, 2011 18:05
See if a word is a palindrome
// compile with:
// javac Palindrome.java
// run it with:
// java Palindrome word
// where word is a word of your choosing
public class Palindrome {
public static boolean is_palindrome(String s) {
// if s is an empty string or s is a single character, return true
if(s == "" || s.length() == 1) return true;
@ox
ox / recursion.md
Created October 18, 2011 19:03
How recursion works

Recursion

Recursion is really easy to understand. It is essentially a function calling itself. Just imagine writing a function that calculates the factorial of a number n (n!). Lets see how it would expand for 4!. Remember that 1! = 1.

(factorial 4)
(4 * (factorial 3))
(4 * (3 * (factorial 3)))
(4 * (3 * (2 * (factorial 1))))
(4 * (3 * (2 * 1)))
@ox
ox / hijack1.rb
Created November 4, 2011 03:40
A minimal layout and explanation of Sinatra inner workings, regarding compactness, and hooking.
module Aleph
class Application
def self.run!
puts "hello world"
end
end
# here is where the trick with sinatra is!
# all of your path defenitions and such have built up a hash
# of routes. When ruby reaches EOF in your script, it calls this
@ox
ox / ping_pong.rb
Created November 29, 2011 23:41
A better Ping-Pong example using Actors in Rubinius2.0.0dev. Ends only when you press enter or something
require 'actor'
Ping = Struct.new :sender
Pong = Struct.new :sender
Actor.spawn do
Actor.register(:ping, Actor.current)
loop do
Actor.receive do |msg|
msg.when(Ping) do |p|
@ox
ox / s3con.rb
Created December 17, 2011 20:36
A ruby-driven utility for light interaction with an amazon S3 server
#!/usr/bin/env ruby
require 'aws/s3'
def help
puts "S3Console usage:\n"
puts "s3con <bucket>"
exit
end
help if ARGV.empty?
@ox
ox / main.c
Created January 21, 2012 15:11
sorting C structs, an example
typedef struct stump {
char * name;
int data;
} stump;
typedef struct container {
stump ** node_array;
} container;
int compare_stumps(const void *a, const void *b) {
@ox
ox / symmetric_tree.rb
Created February 10, 2012 17:48
A function which, when given a root node, will determine if the tree is symmetric. AKA Breadth-first search
#tell whether the tree is visually symmetric (it is)
class Node
attr_reader :value, :left, :right
def initialize(value, left, right)
@value, @left, @right = value, left, right
end
#added in to see output
def to_s; @value.to_s; end
def inspect; @value.to_s; end
@ox
ox / config.ru
Created March 4, 2012 08:14
Flea web server using Rack and my own flea fork
require 'rack'
require 'flea'
interpreter = Flea::Interpreter.new
run Proc.new { |env|
interpreter.run(File.read(File.new("server.scm")))
a = interpreter.evaluate([:call, env]).map! {|x|
interpreter.evaluate(x)
}
@ox
ox / rack_router.rb
Created March 6, 2012 18:52
A rack application that allows for some rudimentary routing using procs
class Router
attr_accessor :routes
def initialize
@routes = {}
end
def add_route(path, &block)
@routes[path] = block
end