This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Maze | |
WALL = '#' | |
EMPTY = ' ' | |
attr_reader :steps | |
def initialize(spec) | |
@spec = spec.split("\n").map { |line| line.split('') } | |
solve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
NewerOlder