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 | |
def initialize(coeffs) | |
raise ArgumentError.new("Need at least 2 coefficients.") if coeffs.size < 2 | |
@coeffs = coeffs | |
end | |
def to_s | |
output = [] | |
coeffs = @coeffs.dup |
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 | |
def initialize(coefs) | |
if coefs.nil? || !coefs.kind_of?(Array) || coefs.size < 2 | |
raise ArgumentError, "Need at least 2 coefficients." | |
end | |
@coefs = coefs | |
end | |
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/ruby | |
class Polynomial | |
def initialize(coefficients) | |
if coefficients.length < 2 | |
raise ArgumentError, 'Need at least 2 coefficients.' | |
end | |
order = coefficients.length - 1 | |
@poly = "" |
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 | |
def initialize(polynomial) | |
if polynomial.length < 2 | |
raise ArgumentError.new("Need at least 2 coefficients") | |
else | |
@polynomial = [] | |
polynomial.reverse.each_with_index do |x, i| | |
@polynomial << Term.new(x, i, (i == (polynomial.size - 1))) | |
end | |
end |
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 | |
def initialize array | |
if array.length <= 1 | |
raise ArgumentError.new('Need at least 2 coefficients') | |
exit | |
else | |
@return_string = '' |
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
# Submission for http://rubylearning.com/blog/2009/11/26/rpcfn-rubyfun-4/ | |
# Author: Rohit Arondekar | |
# Author-URL: http://rohitarondekar.com | |
# Author-email: hello@rohitarondekar.com | |
# Notes: | |
# 1. Storing whether a term is the 1st non zero term as bool in the term | |
# 2. Added a unit test in polynomial_test.rb | |
class Polynomial |
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 | |
def initialize args=[] | |
raise ArgumentError, "Need at least 2 coefficients." if args.size < 2 | |
@max_exp = args.size - 1 | |
@poly = "" | |
args.each_with_index do |coef, index| | |
# Code reading 101 : 'coef' stands for 'coefficient'. | |
next if coef.zero? | |
@coef = coef |
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 | |
# --*-- encoding: UTF-8 --*-- | |
# RPCFN: Ruby Fun (#4) | |
# @author 梁智敏(Gimi Liang) liang.gimi at gmail dot com | |
class Polynomial | |
# Creates a new polynomial. | |
# @override | |
# @param [Array<Integer>] coefficients |
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 | |
def initialize(coefficients) | |
@coefficients = coefficients | |
@highest_exponent = coefficients.size-1 | |
raise ArgumentError, 'Need at least 2 coefficients' if @highest_exponent < 1 | |
@sprintf = ['%ix^%i'] + ['%+ix^%i']*(coefficients.size-2) << '%+i' | |
end | |
def to_s | |
@coefficients.to_enum(:each_with_index).inject([]) {|builder, (coefficient, index)| | |
builder.push coefficient == 0 ? |
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 | |
def initialize(arg) | |
raise ArgumentError.new('We expect an array at least') unless arg.is_a? Array | |
raise ArgumentError.new('Need at least 2 coefficients') if arg.length < 2 | |
@exponents = [] | |
arg.reverse.inject(0) do |index,item| | |
@exponents.push( { "factor" => item, "exponent" => index }) unless item == 0 | |
index + 1 | |
end |