Skip to content

Instantly share code, notes, and snippets.

View RLGGHC's full-sized avatar

RubyLearning Git & GitHub Course RLGGHC

View GitHub Profile
@RLGGHC
RLGGHC / maze.rb
Created January 9, 2010 01:15 — forked from jurgens/maze.rb
#
# Copyright (c) Yuri Omelchuk <jurgen@upscript.com>
#
# January 8, 2009
#
# Maze class written for RubyLearning contest
# http://rubylearning.com/blog/2009/12/27/rpcfn-mazes-5/
#
# solution is based on find shortest path algorithm
#
@RLGGHC
RLGGHC / maze.rb
Created January 5, 2010 02:17 — forked from anonymous/maze.rb
require 'tree'
class Maze
WALL_CHAR = '#'
START_CHAR = 'A'
GOAL_CHAR = 'B'
def initialize(maze)
@maze = maze.lines.map { |l| l.chomp.split '' }
determine_start
@RLGGHC
RLGGHC / maze.rb
Created December 30, 2009 00:58 — forked from rhyhann/maze.rb
# maze.rb, by Othmane Benkirane.
# only tested with ruby 1.9.1p376 (2009-12-07 revision 26041).
# will definitely not work with ruby 1.8.
=begin
Algorithm for steps:
- @steps is populated with the starting cell in the beginnign ('A')
- Until a solution is found, @steps replaces its content by the
possible next steps
- If a solution is found, the number is returned, if a solution isn't found
(if @steps is empty), an error is raised (the loop is broken) and rescued
class Maze
require 'set'
def initialize maze, args={}
raise "No maze given, can't amaze!" if maze.nil?
@options = {:start => "A", :destination => "B", :wall => "#", :open => " "}
@options.merge! args
@start, @end = nil, nil
@pretty_map = maze
@path = Set.new # One path to find them and in the darkness bind them.
@RLGGHC
RLGGHC / maze.rb
Created December 29, 2009 04:20 — forked from zliang-min/maze.rb
# RPCFN #5: Mazes
# @author 梁智敏(Gimi Liang) [gimi.liang at gamil dot com]
# @date 2009/12/29
class Maze
START_POINT_MARKER = 'A'.freeze
END_POINT_MARKER = 'B'.freeze
INFINITE = (1.0 / 0.0).freeze
class Cell
WALL = '#'.unpack('c').first.freeze
@RLGGHC
RLGGHC / maze.rb
Created December 29, 2009 01:07 — forked from alg/maze.rb
class Maze
WALL = '#'
EMPTY = ' '
attr_reader :steps
def initialize(spec)
@spec = spec.split("\n").map { |line| line.split('') }
solve
@RLGGHC
RLGGHC / maze.rb
Created December 28, 2009 10:25 — forked from kejadlen/maze.rb
class Maze
def initialize(s)
@m = s.dup
@d = r(@m.index('A'),@m.index("\n")+1)
end
def r(a,w)
n = [[1,a]]
until n.empty? do
d,i = n.shift
#This class can generate Polynomial for any number of arguments passed
#Comments are welcome
class Polynomial
attr_accessor :expression
def initialize(coefficients)
s = false; p = 0; e = "";
#Generate Expression in reverse direction
coefficients.reverse_each do |c|
class Polynomial
attr_reader :coeffs
def initialize(coeffs)
raise ArgumentError, "Need at least 2 coefficients", caller if coeffs.length <= 2
@coeffs = coeffs
end
def to_s
polynom = "" #initialize empty string to create polynomial expression
@coeffs.each_with_index do |coeff,index|
exponent = @coeffs.length - index - 1 #countdown exponent value starting from 1 less than number of coefficients passed
#!/usr/bin/env ruby
# Polynomial beautifier, by Othmane Benkirane
# The code passes the test but why is @p4 considered as a polynomial ?
# An ArgumentError should be thrown.
# Oh, and... it's got a lot of unseen features. Just see my personal
# specs. It's possible to have a beautiful output (2x³+3x²+10x-1)
# and also several compression types: (x + 5) or (2x² -3x +5)
# Also, shouldn't we do that: puts @polynomial.to_s