Commits

Steven! Ragnarök committed 24e5fa8

A bunch of little Ruby things.

Comments (0)

Files changed (26)

 *.rbc
 
 # Ignore actual project directories.
+bikeshop
 bonk
 dat_redis
 enumerable_attributes
 thebestworstideaever.com
 xmlparty
 youtube_it
+wopr
+notable_scotches
+parker_sb
+nowmov
+monk
 
-

CollatzConjecture/collatz.rb

+require 'three_n_plus_one.rb'
+
+#puts "Testing this now"
+p = ThreeNPlusOne.new
+#puts "Testing 22"
+#puts p.cycle_length 22
+
+#p.max_cycle(100000, 250000)
+
+#100000.upto(250000) do |i|
+#	puts "#{i}'s cycle length is #{p.cycle_length(i)}"
+#end
+
+puts p.max_n_on_cycle(837799)
+puts p.max_n_on_cycle(22)
+
+#puts p.max_cycle 1, 1000000
+#hsh = p.stored
+#ihsh = hsh.invert
+#puts ihsh[525]
+#puts p.max_cycle 100000, 1000000
+#puts p.max_cycle 100000, 125000

CollatzConjecture/three_n_plus_one.rb

+class ThreeNPlusOne
+
+	def initialize
+		@stored = {} 
+	end
+
+	def cycle_length(n)
+		nprime = n
+		c = 0
+		return @stored[n] unless @stored[n].nil?
+
+		while nprime != 1 do
+			if nprime % 2 == 0 then
+				nprime /= 2
+			else
+				nprime = nprime * 3 + 1
+			end
+
+			c+=1
+		end
+
+		@stored[n] = c + 1
+		return c + 1
+	end
+
+	def max_cycle(i, j)
+		max = -1
+		i.upto(j + 1) do |k|
+#			puts "max is #{max}" ## DEBUG
+			n = cycle_length(k)
+#			puts "n is #{n}" ## DEBUG
+			if n > max then
+				max = n
+#				puts "now max is #{max}" ## DEBUG
+			end
+		end
+		return max
+	end
+
+	def max_n_on_cycle(n)
+		nprime = n
+		max = n
+		c = 0
+
+		until nprime == 1 do
+			if nprime > max then
+				max = nprime
+			end
+
+			if nprime.even? then
+				nprime /= 2
+			else
+				nprime = 3 * nprime + 1
+			end
+			c += 1
+		end
+		c += 1
+
+		@stored[n] = c
+		return max
+	end
+
+	def stored
+		@stored
+	end
+
+end
+
+def bsearch(a,v)
+  if(v == a[a.length / 2])
+    return a.length / 2
+  end
+
+  if(v < a[a.length/2])
+    bsearch(a[0..(a.length/2)],v)
+  else
+    bsearch(a[((a.length/2) + 1)..(a.length - 1)],v)
+  end
+  return "Result not found."
+end
+
+
+a = [ 1, 2, 4, 5, 6, 8, 9 ,12, 15, 25, 26, 30, 31, 50, 80, 88, 90, 100, 102, 103, 105, 110, 112, 120 ]
+
+puts bsearch(a,80)
+
+SHELL = /bin/sh
+
+#### Start of system configuration section. ####
+
+srcdir = .
+topdir = /home/steven/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1
+hdrdir = /home/steven/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1
+arch_hdrdir = /home/steven/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/$(arch)
+VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
+prefix = $(DESTDIR)/home/steven/.rvm/rubies/ruby-1.9.2-p180
+rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
+exec_prefix = $(prefix)
+vendorhdrdir = $(rubyhdrdir)/vendor_ruby
+sitehdrdir = $(rubyhdrdir)/site_ruby
+rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
+vendordir = $(rubylibprefix)/vendor_ruby
+sitedir = $(rubylibprefix)/site_ruby
+ridir = $(datarootdir)/$(RI_BASE_NAME)
+mandir = $(datarootdir)/man
+localedir = $(datarootdir)/locale
+libdir = $(exec_prefix)/lib
+psdir = $(docdir)
+pdfdir = $(docdir)
+dvidir = $(docdir)
+htmldir = $(docdir)
+infodir = $(datarootdir)/info
+docdir = $(datarootdir)/doc/$(PACKAGE)
+oldincludedir = $(DESTDIR)/usr/include
+includedir = $(prefix)/include
+localstatedir = $(prefix)/var
+sharedstatedir = $(prefix)/com
+sysconfdir = $(prefix)/etc
+datadir = $(datarootdir)
+datarootdir = $(prefix)/share
+libexecdir = $(exec_prefix)/libexec
+sbindir = $(exec_prefix)/sbin
+bindir = $(exec_prefix)/bin
+rubylibdir = $(rubylibprefix)/$(ruby_version)
+archdir = $(rubylibdir)/$(arch)
+sitelibdir = $(sitedir)/$(ruby_version)
+sitearchdir = $(sitelibdir)/$(sitearch)
+vendorlibdir = $(vendordir)/$(ruby_version)
+vendorarchdir = $(vendorlibdir)/$(sitearch)
+
+CC = gcc
+CXX = g++
+LIBRUBY = $(LIBRUBY_SO)
+LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
+LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)
+LIBRUBYARG_STATIC = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
+OUTFLAG = -o 
+COUTFLAG = -o 
+
+RUBY_EXTCONF_H = 
+cflags   =  $(optflags) $(debugflags) $(warnflags)
+optflags = -O3
+debugflags = -ggdb
+warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long
+CFLAGS   = -fPIC $(cflags)  -fPIC 
+INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
+DEFS     = 
+CPPFLAGS =   $(DEFS) $(cppflags)
+CXXFLAGS = $(CFLAGS) $(cxxflags)
+ldflags  = -L.  -rdynamic -Wl,-export-dynamic
+dldflags = 
+ARCH_FLAG = 
+DLDFLAGS = $(ldflags) $(dldflags)
+LDSHARED = $(CC) -shared
+LDSHAREDXX = $(CXX) -shared
+AR = ar
+EXEEXT = 
+
+RUBY_BASE_NAME = ruby
+RUBY_INSTALL_NAME = ruby
+RUBY_SO_NAME = ruby
+arch = x86_64-linux
+sitearch = $(arch)
+ruby_version = 1.9.1
+ruby = /home/steven/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
+RUBY = $(ruby)
+RM = rm -f
+RM_RF = $(RUBY) -run -e rm -- -rf
+RMDIRS = $(RUBY) -run -e rmdir -- -p
+MAKEDIRS = /bin/mkdir -p
+INSTALL = /bin/install -c
+INSTALL_PROG = $(INSTALL) -m 0755
+INSTALL_DATA = $(INSTALL) -m 644
+COPY = cp
+
+#### End of system configuration section. ####
+
+preload = 
+
+libpath = . $(libdir)
+LIBPATH =  -L. -L$(libdir) -Wl,-R$(libdir)
+DEFFILE = 
+
+CLEANFILES = mkmf.log
+DISTCLEANFILES = 
+DISTCLEANDIRS = 
+
+extout = 
+extout_prefix = 
+target_prefix = 
+LOCAL_LIBS = 
+LIBS = $(LIBRUBYARG_SHARED)  -lpthread -lrt -ldl -lcrypt -lm   -lc
+SRCS = mytest.c
+OBJS = mytest.o
+TARGET = mytest
+DLLIB = $(TARGET).so
+EXTSTATIC = 
+STATIC_LIB = 
+
+BINDIR        = $(bindir)
+RUBYCOMMONDIR = $(sitedir)$(target_prefix)
+RUBYLIBDIR    = $(sitelibdir)$(target_prefix)
+RUBYARCHDIR   = $(sitearchdir)$(target_prefix)
+HDRDIR        = $(rubyhdrdir)/ruby$(target_prefix)
+ARCHHDRDIR    = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
+
+TARGET_SO     = $(DLLIB)
+CLEANLIBS     = $(TARGET).so 
+CLEANOBJS     = *.o  *.bak
+
+all:    $(DLLIB)
+static: $(STATIC_LIB)
+.PHONY: all install static install-so install-rb
+.PHONY: clean clean-so clean-rb
+
+clean-rb-default::
+clean-rb::
+clean-so::
+clean: clean-so clean-rb-default clean-rb
+		@-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
+
+distclean-rb-default::
+distclean-rb::
+distclean-so::
+distclean: clean distclean-so distclean-rb-default distclean-rb
+		@-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
+		@-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
+		@-$(RMDIRS) $(DISTCLEANDIRS)
+
+realclean: distclean
+install: install-so install-rb
+
+install-so: $(RUBYARCHDIR)
+install-so: $(RUBYARCHDIR)/$(DLLIB)
+$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
+	@-$(MAKEDIRS) $(@D)
+	$(INSTALL_PROG) $(DLLIB) $(@D)
+install-rb: pre-install-rb install-rb-default
+install-rb-default: pre-install-rb-default
+pre-install-rb: Makefile
+pre-install-rb-default: Makefile
+$(RUBYARCHDIR):
+	$(MAKEDIRS) $@
+
+site-install: site-install-so site-install-rb
+site-install-so: install-so
+site-install-rb: install-rb
+
+.SUFFIXES: .c .m .cc .cxx .cpp .C .o
+
+.cc.o:
+	$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cxx.o:
+	$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cpp.o:
+	$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.C.o:
+	$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.c.o:
+	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
+
+$(DLLIB): $(OBJS) Makefile
+	@-$(RM) $(@)
+	$(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
+
+
+
+$(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h

exttest/extconf.rb

+require 'mkmf'
+
+extension_name = 'mytest'
+
+dir_config(extension_name)
+
+create_makefile(extension_name)
+#include "ruby.h"
+
+VALUE MyTest = Qnil;
+
+void Init_mytest();
+
+VALUE method_test1(VALUE self);
+
+void Init_mytest()
+{
+	MyTest = rb_define_module("MyTest");
+	rb_define_method(MyTest, "test1", method_test1, 0);
+}
+
+VALUE method_test1(VALUE self)
+{
+	int x = 10;
+	return INT2NUM(x);
+}

exttest/mytest.o

Binary file added.

exttest/mytest.so

Binary file added.
+require 'gtk2'
+
+class Rubyapp < Gtk::Window
+  def initialize
+    super
+
+    set_title "Center"
+    signal_connect "destroy" do
+      Gtk.main_quit
+    end
+
+    set_default_size 250, 200
+    set_window_position Gtk::Window::POS_CENTER
+
+    show
+  end
+end
+
+Gtk.init
+  window = Rubyapp.new
+Gtk.main

guard_print/Guardfile

+# A sample Guardfile
+#
+# More info at https://github.com/guard/guard#readme
+require 'guard/guard'
+module ::Guard
+	class PrintGuard < Guard
+		def run_on_change paths
+			paths.each do |path|
+				puts "The change in #{path} was picked up by PrintGuard"
+			end
+		end
+	end
+end
+
+guard 'printguard' do
+	watch %r{.*}
+end
+hell

guard_print/foo

Empty file added.

guard_print/foobar

+hell

guard_print/printer/bar

+poll

guard_print/printer/foobar

+foo
+
+## Solves Problem 11.3-4 ##
+=begin
+A = (Math.sqrt(5) - 1)/2
+
+def hash(key)
+  x = A*key
+  x %= 1
+  x *=1000
+  return x.floor
+end
+
+61.upto 65 do |k|
+  puts hash(k)
+end
+=end
+
+C1 = 1
+C2 = 3
+M = 11
+
+def hashprime(key)
+  return key
+end
+
+def hash_1(key)
+  return key
+end
+
+def hash_2(key)
+  return 1 + (key % (M - 1))
+end
+
+def linear_hash(key,i)
+  return (hashprime(key) + i) % M
+end
+
+def quadratic_hash(key,i)
+  return (hashprime(key) + C1*i + C2*i*i) % M
+end
+
+def double_hash(key,i)
+  return (hash_1(key) + i * hash_2(key)) % M
+end
+
+
+def linear_insert(hash_t,key)
+  i = 0
+  until i == M do
+    j = (linear_hash(key,i))
+    if hash_t[j] == nil
+      hash_t[j] = key
+      return j
+    else 
+      i += 1
+    end
+  end
+  puts "Error! Hash Table Overflow!!!"
+end
+  
+def quad_insert(hash_t,key)
+  i = 0
+  until i == M do
+    j = (quadratic_hash(key,i))
+    if hash_t[j] == nil
+      hash_t[j] = key
+      return j
+    else 
+      i += 1
+    end
+  end
+  puts "Error! Hash Table Overflow!!!"
+end
+
+def double_insert(hash_t,key)
+  i = 0
+  until i == M do
+    j = (double_hash(key,i))
+    if hash_t[j] == nil
+      hash_t[j] = key
+      return j
+    else 
+      i += 1
+    end
+  end
+  puts "Error! Hash Table Overflow!!!"
+end
+
+keys = [ 10, 22, 31, 4, 15, 28, 17, 88, 59 ]
+
+puts "Hash using linear probing"
+ht = Array.new(M)
+keys.each do |k|
+  linear_insert(ht,k)
+end
+puts ht.to_s
+
+puts "Hash using quadratic probing"
+ht = Array.new(M)
+keys.each do |k|
+  quad_insert(ht,k)
+end
+puts ht.to_s
+
+puts "Hash using double hashing"
+ht = Array.new(M)
+keys.each do |k|
+  double_insert(ht,k)
+end
+puts ht.to_s
+require 'Qt4'
+
+app = Qt::Application.new(ARGV)
+
+hello = Qt::PushButton.new('Hello World!')
+hello.resize(100, 30)
+hello.show
+
+app.exec
+def isort array
+	# From 2nd to Last element of 0 indexed array
+	for j in 1 .. array.length - 1
+		key = array[j]
+		# Now insert the key into the sorted sequence A[1..j - 1]
+		i = j - 1
+		# -1 because we are 0 indexed.
+		while i > -1 and array[i] > key
+			array[i + 1] = array[i]
+			i -= 1
+		end
+		array[i+1] = key
+	end
+end
+def msort a
+	if a.length == 1
+		return a
+	else
+		left = msort a[0..a.length/2]
+		right = msort a[a.length/2+1..a.length -1]
+		return merge left, right
+	end
+end
+
+def merge ls, rs
+	a = Array.new
+	while ls.length > 0 and rs.length > 0
+		if ls.first 
+puts "enter a binary number."
+:wq
+def quicksort(list, p, r)
+  if p < r then
+    q = partition(list, p, r)
+    quicksort(list, p, q-1)
+    quicksort(list, q+1, r)
+  end
+end
+ 
+def partition(list, p, r)
+  pivot = list[r]
+    i = p - 1
+    p.upto(r-1) do |j|
+        if list[j] <= pivot
+            i = i+1
+            list[i], list[j] = list[j],list[i]
+        end
+    end
+    list[i+1],list[r] = list[r],list[i+1]
+    return i + 1
+end
+
+# No longer needed because you can swap array values natively.
+# Gosh Ruby is swell.
+def exch a,i,j
+	temp = a[i]
+	a[i] = a[j]
+end
+
+def hoare_part a, p, r
+	pivot = a[p]
+	i = p - 1
+	j = r + 1
+
+	while true
+		until a[j] <= pivot
+			j -= 1
+		end
+		until a[i] >= pivot
+			i += 1
+		end
+		if i < j
+			exch a, i, j
+		else return j
+		end
+	end
+end
+
+def partition a, p , r
+	pivot = a[r]
+	i = p - 1
+	for j in p .. r - 1
+		if a[j] <= pivot
+			i += 1
+			a[i], a[j] = a[j], a[i]
+		end
+	end
+	a[i+1], a[r] = a[r], a[i+1]
+	return i + 1
+end
+
+def qsort a, p, r
+	if p < r then
+		q = partition a, p, r
+		qsort a, p, q - 1
+		qsort a, q + 1, r
+	end
+end
+
+def random_part a, p, r
+
+
+class SLib
+	def SLib.avg(a)
+		sum = 0
+		a.each do |i|
+			sum += i
+		end
+		return (sum * 1.0)/ a.count
+	end
+end
+class SortedSet
+	def initialize
+		@hash = Hash.new
+		@order = Array.new
+	end
+
+	def insert elt
+		obj_hash = elt.hash
+		unless @hash.has_key? obj_hash
+			@hash[obj_hash] = elt
+			@order << obj_hash
+		else
+			take_error_action elt
+		end
+	end
+
+	def take_error_action elt
+		nil
+	end
+end
+class V2D
+	def initialize(params= nil)
+		if params[:magnitude] && params[:theta]
+			@magnitude = params[:magnitude]
+			@theta = params[:theta]
+		else
+			@magnitude = Math.sqrt(params[:x]**2 + params[:y]**2)
+			@theta = to_deg(Math.atan2(params[:y],params[:x]))
+		end
+	end
+
+	def initialize(x,y)
+		@magnitude = Math.sqrt(x**2 + y**2)
+		@theta = to_deg(Math.atan2(y,x))
+	end
+
+	attr_accessor :magnitude, :theta
+
+	def x
+		@magnitude * Math.cos(to_rad(@theta))
+	end
+	
+	def y
+		@magnitude * Math.sin(to_rad(@theta))
+	end
+
+	def to_rad(deg)
+		deg * Math::PI / 180
+	end
+
+	def to_deg(rad)
+		rad * 180 / Math::PI
+	end
+
+	def +(vector2d)
+		newx = x + vector2d.x
+		newy = y + vector2d.y
+		@magnitude = Math.sqrt(newx**2 + newy**2)
+		@theta = to_deg(Math.atan2(newy,newx))
+	end
+
+	def -(vector2d)
+		newx = x - vector2d.x
+		newy = y - vector2d.y
+		@magnitude = Math.sqrt(newx**2 + newy**2)
+		@theta = to_deg(Math.atan2(newy,newx))
+	end
+	
+	def dot (v2d)
+		x * v2d.x + y * v2d.y
+	end
+
+end