Skip to content

Instantly share code, notes, and snippets.

View RLGGHC's full-sized avatar

RubyLearning Git & GitHub Course RLGGHC

View GitHub Profile
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 ?
#!/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
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
# 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
class Polynomial
def initialize array
if array.length <= 1
raise ArgumentError.new('Need at least 2 coefficients')
exit
else
@return_string = ''
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
#!/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 = ""
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
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
#!/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