# Полином

Краен срок
06.11.2013 12:00

Срокът за предаване на решения е отминал

# Полином

Напишете клас Polynomial, който представя полиноми в човешки вид, следвайки няколко прости правила:

• Ако коефициентът е 1, той не се отпечатва.

• Ако коефициентът е отрицателно число, пред него се отпечатва само знакът -. Например - 2x^3, а не + - 2x^3 или + (- 2x^3).

• Ако коефициентът е 0, едночленът не се добавя към полинома.

• При x^1 се пропуска ^1 и се отпечатва само x.

• Тъй като x^0 е единица, няма нужда да се отпечатва.

Примери:

Polynomial.new([-3, -4, 1, 0, 6]).to_s #=> '- 3x^4 - 4x^3 + x^2 + 6'
Polynomial.new([1, 0, 2]).to_s         #=> 'x^2 + 2'


Детайли:

• При празен списък от коефициенти, поведението на полинома е недефинирано.

• Ако всички коефициенти в списъка са нули, връща се '0'.

Подсказки:

• Трябва да предефинирате to_s.

## Решения

• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Иван Капукаранов
class Polynomial
def initialize(polynomial)
@polynomial = polynomial
end
def to_s
polynomial_string = ""
reversal = @polynomial.length - 1
@polynomial.each_with_index do |element, index|
x = "x"
sign = "+ "
power = reversal - index
coefficient = ""
string_power = ""
case power
when 0
string_power = ""
x = ""
when 1
string_power = " "
else
string_power = "^" + power.to_s + " "
end
if element < -1
sign = "- "
coefficient = element.abs.to_s
elsif element == -1 and power != 0
sign = "- "
elsif element == -1 and power == 0
sign = "- "
coefficient = element.abs.to_s
elsif element == 0 and power != 0
sign, x, string_power = "", "", ""
elsif element == 1 and power != 0
coefficient = ""
elsif element == 0 and power == 0
coefficient = element.to_s
sign = ""
else
coefficient = element.to_s
end
polynomial_string += sign + coefficient + x + string_power
end
polynomial_string == "+" ? polynomial_string.slice(2..-1).chomp(" 0") : polynomial_string.chomp(" 0")
end
end
....

Finished in 0.12504 seconds
4 examples, 0 failures
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Георги Ангелов
class Polynomial
def initialize(coefficients)
@coefficients = coefficients.drop_while { |coefficient| coefficient == 0 }
end
def to_s
return '0' if @coefficients.empty?
coefficient_strings = @coefficients.map.with_index do |coefficient, index|
next if coefficient.zero?
exponent = @coefficients.size - index - 1
exponent_string = "^#{exponent}" if exponent != 1
sign = coefficient > 0 ? '+' : '-' if index > 0 or coefficient < 0
x_to_exponent = "x#{exponent_string}" if exponent > 0
coefficient_string = coefficient.abs if exponent.zero? or coefficient != 1
"#{sign} #{coefficient_string}#{x_to_exponent}"
end
coefficient_strings.compact.join(' ').strip
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-e1prvm/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.08624 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-e1prvm/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Владимир Кузмов
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def to_s
return nil if @coefficients.empty?
return 0 if @coefficients.all? { |coef| coef.zero? }
polynom = []
@coefficients.each_with_index do |number, i|
if number.abs > 0
monomial = ''
if number >= 0 and i > 0
monomial += '+ '
elsif number < 0
monomial += '- '
end
monomial += number.abs >= 2 ? number.abs.to_s : ''
power = @coefficients.size - 1 - i
monomial += power > 0 ? 'x' : ''
monomial += power > 1 ? '^' + power.to_s : ''
polynom << monomial
end
end
polynom.join(' ')
end
end
...F

Failures:

1) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: 0

(compared using ==)
# /tmp/d20131106-4393-htcan3/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.08829 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-htcan3/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Кристиан Ташков
class Polynomial
def initialize(coefficents)
@coefficents = coefficents
end
def to_s
power_of_x = lambda do |n|
case n
when 0 then ""
when 1 then "x"
else "x^#{n}"
end
end
coeff_sign = lambda do |coeff, index|
index.nonzero? || coeff < 0 ? "#{coeff > 0 ? '+' : '-'} " : ""
end
coeff_value = lambda do |coeff, is_last|
is_last || coeff.abs != 1 ? "#{coeff.abs}" : ""
end
polynomial = @coefficents.each_with_index.select { |coeff, index| coeff.nonzero? }
polynomial_as_strings = polynomial.map do |coeff, index|
sign = coeff_sign.call(coeff, index)
value = coeff_value.call(coeff, (index + 1) == @coefficents.size)
power = power_of_x.call(@coefficents.size - (index + 1))
"#{sign}#{value}#{power}"
end
polynomial_as_strings.empty? ? "0" : polynomial_as_strings.join(' ')
end
end
....

Finished in 0.09389 seconds
4 examples, 0 failures
• Некоректно
• 0 успешни тест(а)
• 0 неуспешни тест(а)
Радослав Даскалов
class Polynomial
def initialize coefficients
@coeff = coefficients
end
def to_s
s = ''
n = @coeff.length
@coeff.each_with_index { |c, i|
next if c == 0
s << ' ' unless s.empty?
if c < 0 then
s << '- '
else
s << '+ ' unless s.empty?
end
s << c.abs.to_s unless c.abs == 1
s << 'x' unless i >= n - 1
s << '^' << (n - i - 1).to_s unless i >= n - 2
}
return '0' if s.empty?
return s
end
/data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in require': /tmp/d20131106-4393-1os4bq8/solution.rb:24: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
end
^
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in block in setup_load_path_and_require'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in each'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in setup_load_path_and_require'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration_options.rb:25:in configure'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:21:in run'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in run'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in block in autorun'
• Некоректно
• 0 успешни тест(а)
• 4 неуспешни тест(а)
Виктор Кетипов
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def to_s()
non_zero_coefficient = 0
return_string = ""
degree = @coefficients.length() - 1
@coefficients.each do |coef|
if coef != 0
if coef > 0 and non_zero_coefficient > 0
return_string += "+ "
elsif coef < 0
return_string += "- "
end
return_string += degree > 0 ? "#{coef.abs()}x^#{degree} " : "#{coef.abs()}"
non_zero_coefficient += 1
end
degree -= 1
end
if non_zero_coefficient == 0
return_string = "0"
end
puts return_string
end
end
1x^2 + 2
F- 3x^4 - 4x^3 + 1x^2 + 6
F- 1x^3 - 2x^2 + 3x^1
F0
F

Failures:

1) Polynomial formats a simple polynomial
Failure/Error: Polynomial.new([1, 0, 2]).to_s.should eq 'x^2 + 2'

expected: "x^2 + 2"
got: nil

(compared using ==)
# /tmp/d20131106-4393-4lr9sq/spec.rb:3:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: nil

(compared using ==)
# /tmp/d20131106-4393-4lr9sq/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: nil

(compared using ==)
# /tmp/d20131106-4393-4lr9sq/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

4) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: nil

(compared using ==)
# /tmp/d20131106-4393-4lr9sq/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.006 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20131106-4393-4lr9sq/spec.rb:2 # Polynomial formats a simple polynomial
rspec /tmp/d20131106-4393-4lr9sq/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-4lr9sq/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-4lr9sq/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Стефан Василев
class Polynomial
def initialize(coefficients)
@array = coefficients
end
def to_s
return 0 if @array.all? { |item| item == 0 }
expression = ""
@array.each_index do |index|
if @array[index] != 0
number_sign = if @array[index] < 0 then "- " else "+ " end
number_sign = "" if expression == "" and number_sign == "+ "
power = "^#{@array.size - index - 1}" unless @array.size - index - 1 == 1
number = "#{@array[index].abs}" if @array[index].abs != 1 or index == @array.size - 1
expression += "#{number_sign}#{number}x#{power} "
end
end
if @array[@array.size - 1] == 0 then expression.chop else expression.chop.chop.chop.chop end
end
end
...F

Failures:

1) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: 0

(compared using ==)
# /tmp/d20131106-4393-1osdzum/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.09122 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1osdzum/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 2 успешни тест(а)
• 2 неуспешни тест(а)
Слав Керемидчиев
class Polynomial
attr_accessor :coefficients
def initialize(coefficients)
@coefficients = coefficients
end
def to_s
coefficient = ->(number) { "#{number > 0 ? "+" : "-" } #{ number.abs if number.abs != 1 }" }
exponent = ->(number) { number > 0 ? "x^#{number} " : "" }
output_string = ""
ziped_array = @coefficients.zip((@coefficients.size-1).downto(0))
ziped_array.each_with_index do |element|
if element.first != 0
output_string += coefficient.(element.first) + exponent.(element.last)
end
end
if output_string.slice(0) == "+"
output_string.slice(2..output_string.size).strip
else
output_string.strip
end
end
end
..FF

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x^1"

(compared using ==)
# /tmp/d20131106-4393-15rk1p0/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-15rk1p0/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.10248 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20131106-4393-15rk1p0/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-15rk1p0/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Илиян Бобев
class Polynomial
def initialize(args)
@args = args
end
def to_s
result = ""
@args.each_with_index do |term, index|
power = @args.size - index - 1
sign = coefficient = variable = power_str = ""
sign = " - " if term < 0
sign = " + " if term > 0 and index != 0
coefficient = term.abs.to_s if term != 0 and term != 1
coefficient = "0" if power== 0 and result == ""
variable = "x" if term != 0 and power != 0
power_str = '^'+power.to_s if term != 0 and power > 1
result << sign + coefficient + variable + power_str
end
result.strip
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-2e0xgj/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.06571 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-2e0xgj/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 2 успешни тест(а)
• 2 неуспешни тест(а)
Георги Пурнаров
class Polynomial
:odds
def initialize polynom
@odds=polynom
end
def to_s
size,result=@odds.size,""
if @odds.all? {|odd| odd==0}
result<< "0"
elsif size==1
result << "#{@odds}"
else
@odds.each_with_index do |odd,index|
case
when (index==size-1 and odd==1)
result<<" + 1"
when (index==size-1 and odd==-1)
result<<" - 1"
when (index==size-1 and odd<0)
result<<" #{odd}"
when (index==size-1 and odd>0)
result<<" + #{odd}"
when (index==0 and odd ==1 and size==2)
result<<"x"
when (index==0 and odd ==-1 and size==2)
result<<"-x"
when (index==0 and odd != 0 and size==2)
result<<"#{odd}x^#{size-index-1}"
when (index==0 and odd ==1)
result<<"x^#{size-index-1}"
when (index==0 and odd ==-1)
result<<"-x^#{size-index-1}"
when (index==0 and odd != 0)
result<<"#{odd}x^#{size-index-1}"
when (index==size-2 and odd==1)
result<<"+ x"
when (index==size-2 and odd==-1)
result<<"- x"
when (index==size-2 and odd != 0)
result<<"#{odd}x"
when odd==1
result<<" + x^#{size-index-1}"
when odd==-1
result<<" - x^#{size-index-1}"
when odd<0
result<<" #{odd}x^#{size-index-1}"
when odd>0
result<<"+ #{odd}x^#{size-index-1}"
end
end
end
result
end
end
.FF.

Failures:

1) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: "-3x^4 -4x^3 + x^2 + 6"

(compared using ==)
# /tmp/d20131106-4393-1bs3zrq/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "-x^3 -2x^23x"

(compared using ==)
# /tmp/d20131106-4393-1bs3zrq/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.09496 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20131106-4393-1bs3zrq/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-1bs3zrq/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 2 успешни тест(а)
• 2 неуспешни тест(а)
Валентин Ейткен
class Polynomial
def initialize coefficients
@coefficients = coefficients
end
def to_s
@coefficients.each_with_index.map do |coefficient, idx|
normalize(coefficient, @coefficients.length - idx - 1)
end.join.strip.sub /^\+ /, ''
end
private
def normalize(c, p)
c == 0 && '' || "#{normalize_coefficient(c, p)}#{normalize_p(p)}"
end
def normalize_coefficient(c, p)
" #{c > 0 ? '+' : '-'} #{p != 0 && c ==1 ? '' : c.abs}"
end
def normalize_p(p)
if p == 0
''
elsif p == 1
"x"
else
"x^#{p}"
end
end
end
..FF

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-k12e3a/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-k12e3a/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.09596 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20131106-4393-k12e3a/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-k12e3a/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Николай Хубанов
class Polynomial
def initialize(coefficients)
@coefficients = coefficients.dup.reverse
end
def self.format_monomial(coeff, power)
sign = (coeff < 0) ? '-' : '+'
coefficient = (coeff.abs != 1 || power == 0) ? coeff.abs.to_s : ''
exponent = case power
when 0 then ''
when 1 then 'x'
else 'x^' + power.to_s
end
sign + ' ' + coefficient + exponent
end
def to_s
return @string_value if @string_value != nil
@string_value = raw_string_representation
@string_value = @string_value[2..-1] if @string_value.start_with?('+ ')
@string_value = '0' if @string_value.empty?
@string_value
end
private
def raw_string_representation
@coefficients.each_with_index
.select { |coeff, power| coeff != 0 }
.map { |coeff, power| self.class.format_monomial(coeff, power)}
.reverse
.join(' ')
end
end
....

Finished in 0.12068 seconds
4 examples, 0 failures
• Некоректно
• 1 успешни тест(а)
• 3 неуспешни тест(а)
Калоян Калудов
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def to_s
output = ""
@coefficients.each_with_index do |element, index|
output += transform(element, @coefficients.size - index - 1, index == 0)
end
output
end
def transform(coefficient, power, first)
return "" if coefficient.zero?
sign_string = coefficient > 0 ? " + " : " - "
sign_string = "" if first and sign_string == " + "
coefficient_string = coefficient.abs != 1 ? "#{coefficient.abs}" : ""
return sign_string + coefficient_string if power == 0
power_string = power != 1 ? "^#{power}" : ""
sign_string + coefficient_string + "x" + power_string
end
end
.FFF

Failures:

1) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: " - 3x^4 - 4x^3 + x^2 + 6"

(compared using ==)
# /tmp/d20131106-4393-1lhnle/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: " - x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-1lhnle/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-1lhnle/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.1055 seconds
4 examples, 3 failures

Failed examples:

rspec /tmp/d20131106-4393-1lhnle/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-1lhnle/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-1lhnle/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 1 успешни тест(а)
• 3 неуспешни тест(а)
Александър Тахчиев
class Polynomial < String
def initialize(array)
@array = array.map { |i| i < 0 ? '- ' + i.abs.to_s : i.to_s }
@poly_degrees = 0.upto(@array.length - 1).map(&:to_s)
end
def to_s
a = @array.zip(@poly_degrees.reverse).map { |i| ' ' + i + 'x^' + i + ' '}.
select{|i| not i.include? ('0x')}.map{|i| i.sub('x^0', '')}.map do |i|
i.sub(' 1x', ' x')
end.map{|i| i.sub('x^1', 'x')}.each_with_index.map do |obj, i|
(not obj.include? ('-') and i != 0) ? '+ ' + obj : obj
end.map { |i| i.sub(' ', ' ') }.join('')[1..-2]
a == nil ? 0 : a
end
end
.FFF

Failures:

1) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: "- 3x^4  - 4x^3 + x^2 + 6"

(compared using ==)
# /tmp/d20131106-4393-1qgm8sg/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3  - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-1qgm8sg/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: 0

(compared using ==)
# /tmp/d20131106-4393-1qgm8sg/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.11534 seconds
4 examples, 3 failures

Failed examples:

rspec /tmp/d20131106-4393-1qgm8sg/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-1qgm8sg/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-1qgm8sg/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Йордан Пулов
class Polynomial
def initialize(coeficients)
@eq = ""
@coef = coeficients
@size = coeficients.size - 1
end
def to_s()
@eq << (@coef < 0 ? "- " : "" ) << ( @coef.abs == 1 ? "" : @coef.abs.to_s )<< "x^" << @size.to_s if @coef != 0
1. upto(@coef.size - 2) do |var|
@eq<<( @coef[var] < 0 ? " - " : " + " ) << ( @coef[var].abs == 1 ? "" : @coef[var].abs.to_s )<< "x^" << (@size - var).to_s if @coef[var] != 0
end
@eq <<(@coef[@size] < 0 ? " - " : " + " ) << ( @coef[@size].abs == 1 ? "" : @coef[@size].abs.to_s ) if @coef[@size] != 0
return "0" if @eq == ""
return @eq
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x^1"

(compared using ==)
# /tmp/d20131106-4393-1jypty8/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.12174 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1jypty8/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Георги Гърдев
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def to_s
return '0'f if @coefficients.all?(&:zero?)
@coefficients.each_with_index.reduce('') do |to_string, (coefficient, i)|
unless coefficient.zero?
if coefficient < 0
to_string << '- 'f
else
to_string << '+ 'f unless i.zero?
end
exponent = @coefficients.size - i - 1
unless coefficient.abs == 1 && i != @coefficients.size - 1
to_string << coefficient.abs.to_s
end
to_string << case exponent
when 0 then ''f
when 1 then 'x'f
else "x^#{exponent} "
end
end
to_string
end
end
end
....

Finished in 0.06034 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Деян Хаджиев
class Term
attr_accessor :coefficient, :degree
def initialize(coefficient, degree)
@coefficient = coefficient
@degree = degree
end
def to_s
@coefficient.zero? ? '' : sign + coeff + variable + ' '
end
private
def sign
@coefficient > 0 ? '+ ' : '- '
end
def coeff
@coefficient.abs == 1 && @degree != 0 ? '' : @coefficient.abs.to_s
end
def variable
case degree
when 0 then ''
when 1 then 'x'
else "x^#{@degree}"
end
end
end
class Polynomial
def initialize(coefficient_array)
@polynomial = Array.new(coefficient_array.size) do |index|
Term.new(coefficient_array[index], coefficient_array.size - index - 1)
end
end
def to_s
if @polynomial.all? { |term| term.coefficient.zero? }
'0'
else
formated = @polynomial.map { |term| term.to_s }.join
stripped = formated.strip
stripped == '+' ? stripped[2..-1] : stripped
end
end
end
....

Finished in 0.07843 seconds
4 examples, 0 failures
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Никола Ненков
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def to_s
return "0" if coefficients.all?(&:zero?)
addendents = coefficients.each_with_index.map do |coefficient, index|
single_addend(coefficient, coefficients.count - index - 1)
end
so_far + addendent + (addendent.empty? ? "" : " ")
end
first_non_zero_coefficient_negative? ? result.strip : result[2..-1].strip
end
return "" if coefficient.zero?
sign = coefficient > 0 ? "+ " : "- "
actual_coefficient = coefficient == 1 ? "" : coefficient.abs.to_s
x_powered = power == 1 ? "x" : "x^#{power}"
power.zero? ? sign + actual_coefficient : sign + actual_coefficient + x_powered
end
def first_non_zero_coefficient_negative?
coefficients.each { |coefficient| return coefficient < 0 unless coefficient.zero? }
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-nw6iiv/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.0912 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-nw6iiv/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 2 успешни тест(а)
• 2 неуспешни тест(а)
Георги Шопов
class Polynomial
attr_accessor :coeficients, :degree
def initialize(coeficients)
@coeficients = coeficients
@degree = coeficients.length - 1
end
def to_s
end
private
coeficients.each_with_index.map do |coeficient, index|
if coeficient == 0
''
elsif index.zero?
coeficient < 0 ? '- ' : ''
else
coeficient < 0 ? ' - ' : ' + '
end
end
end
signs.zip(coeficients).map do |sign, coeficient|
[-1, 1, 0].include?(coeficient) ? sign : sign + coeficient.abs.to_s
end
end
signs_and_coeficients.each_with_index.map do |prefix, index|
if index == signs_and_coeficients.length - 1 or coeficients[index].zero?
prefix
else
prefix + 'x'
end
end
end
polynomials.each_with_index.map do |polynomial, index|
if (0...degree).include? index and not coeficients[index].zero?
polynomial + "^#{degree - index}"
else
polynomial
end
end
end
end
..FF

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x^1"

(compared using ==)
# /tmp/d20131106-4393-1aaojai/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-1aaojai/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.14561 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20131106-4393-1aaojai/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-1aaojai/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Сияна Славова
class Polynomial
def initialize (polynomial)
@polynomial = polynomial
end
def to_s
length = @polynomial.length
monomials = []
return '0' if @polynomial.all? { |i| i == 0}
@polynomial.each_with_index do |coefficient, index|
power = length - index - 1
coefficient = coefficient.to_s.start_with?("-") ? ("- ").concat(coefficient.to_s.delete("-")) : coefficient
monomial = if power == 1
monomial = "x"
elsif power == 0
monomial = ""
else
monomial = "x^#{power}"
end
if coefficient == 1 or coefficient == "- 1"
monomials << "#{monomial}"
elsif coefficient != 0
monomials << "#{coefficient}#{monomial}"
end
end
make_polynomial(monomials)
end
def make_polynomial (monomials)
polynomial = monomials.first.concat(" ")
monomials[1,monomials.length].each do |monomial|
if monomial.start_with?("- ")
polynomial = polynomial.concat("#{monomial} ")
else
polynomial = polynomial.concat("+ #{monomial} ")
end
end
polynomial.chomp(" ")
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-1sb6j2n/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.00606 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1sb6j2n/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Любомир Георгиев
class Polynomial
attr_accessor :polynom
def initialize(polynom)
@polynom = polynom
end
def to_s
polynom_size = @polynom.size
return nil if polynom_size.zero?
polynom_string = ''
@polynom.each.with_index do |p, i|
case
when p > 0
polynom_string += ' + ' if i > 0
when p < 0
polynom_string += ' - '
when p.zero?
next
end
polynom_string += "#{p.abs}" if p.abs > 1
polynom_string += "x" if polynom_size-1- i != 0
polynom_string += "^#{polynom_size-i-1}" if polynom_size-i-1 > 1
end
polynom_string.strip.empty? ? 0.to_s : polynom_string.strip
end
end
....

Finished in 0.1281 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Владимир Конушлиев
class Polynomial
def initialize(constants)
@constants = constants
@power = constants.size - 1
end
def to_s
return '0' if @constants.any? && @constants.all?(&:zero?)
@constants.map.with_index do |constant, index|
next if constant.zero?
[get_sign(constant, index), get_coefficient(constant), get_monomial(index)].join
end.compact.join(' ')
end
private
def get_sign(constant, index)
if constant < 0
'- '
elsif index.zero?
''
else
'+ '
end
end
def get_coefficient(constant)
constant.abs == 1 ? '' : constant.abs
end
def get_monomial(index)
case (@power - index)
when 0
''
when 1
'x'
else
"x^#{@power - index}"
end
end
end
....

Finished in 0.08583 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Георги Урумов
class Polynomial
def initialize(args)
@coefficients = args
@polynom = ""
end
def signed_number(n)
return "+ " if n == 1
return "- " if n == -1
if n > 0 then return "+ #{n}" else return "- #{n.abs}" end
end
def to_s
@coefficients.each_with_index do |n, i|
if n != 0
if i == @coefficients.size - 1
@polynom += "#{signed_number(n)} "
elsif i == @coefficients.size - 2
@polynom += "#{signed_number(n)}x "
else
@polynom += "#{signed_number(n)}x^#{@coefficients.size - 1 - i} "
end
end
end
if @polynom.empty?
return "0"
else
return @polynom[2..@polynom.size] if @polynom.strip!.start_with?("+")
return @polynom
end
end
end
....

Finished in 0.17413 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Красимира Божанова
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def max_power
@coefficients.size - 1
end
def numbers_to_s
@coefficients.each_with_index.map do |number, index|
sign = number > 0 ? ' + ' : ' - '
if number == 0
""
elsif number.abs == 1 and index != max_power
sign
elsif number < 0
sign + number.abs.to_s
else
sign + number.to_s
end
end
end
def variables
max_power.downto(1).map { |current| current > 1 ? "x^#{current}" : "x" }
end
end
def to_s
return "0" if @coefficients.all?(&:zero?)
end
end
....

Finished in 0.08624 seconds
4 examples, 0 failures
• Некоректно
• 1 успешни тест(а)
• 3 неуспешни тест(а)
Диан Николов
class Monome
attr_accessor :string
def initialize(coef, degree)
xDegreeString = ""
@string = ""
if degree == 1
xDegreeString = "x "
else
if degree != 0
xDegreeString = "x^" + degree.to_s + " "
end
end
if coef != 0 then @string = coefToString(coef) + xDegreeString end
end
def coefToString(coef)
if coef == 1 then return "+ " end
if coef == -1 then return "- " end
if coef > 0 then return "+ " + coef.to_s end
if coef < 0 then return "- " + coef.to_s[1..-1] end
end
end
class Polynomial
def initialize(coefficients)
@monomes = []
(coefficients.size - 1).downto(0).each { |i| @monomes << Monome.new(coefficients[coefficients.size - 1 - i], i) }
end
def to_s
if monomes.string.length > 0 and (@monomes.string == "-" or @monomes.string == "+")
@monomes.string = @monomes.string[2..-1]
end
polynome = ""
@monomes.each { |monome| polynome += monome.string }
polynome
end
end
.FFF

Failures:

1) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: "3x^4 - 4x^3 + x^2 + 6"

(compared using ==)
# /tmp/d20131106-4393-aa2x2s/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "x^3 - 2x^2 + 3x "

(compared using ==)
# /tmp/d20131106-4393-aa2x2s/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-aa2x2s/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.08835 seconds
4 examples, 3 failures

Failed examples:

rspec /tmp/d20131106-4393-aa2x2s/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-aa2x2s/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-aa2x2s/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Емануела Моллова
class Polynomial
attr_accessor :coefficients
def initialize(coefficients)
if coefficients.size == 0 or coefficients.nil? or not coefficients.kind_of?(Array)
raise "Invalid coefficients."
end
@coefficients = coefficients
end
def to_s
count = @coefficients.size
polynomial = @coefficients.map.with_index do |coefficient, index|
unless coefficient.zero?
(coefficient < 0 ? ' - ' : ' + ') +
((coefficient.abs.equal? 1 and index != count - 1) ? '' : "#{coefficient.abs}") +
((count - index - 1).zero? ? '' : 'x') +
((count - index - 1) < 2 ? '' : "^#{count - index - 1}")
end
end.compact.join('')[1..-1]
!polynomial ? '0' : (polynomial[0,1] == '+' ? polynomial[2..-1] : polynomial)
end
end
....

Finished in 0.09391 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Ясен Трифонов
#!/usr/local/bin/ruby -w
class Polynomial
attr_accessor :coefficients
def initialize(coefficients)
@coefficients = coefficients
end
def all_zeros?
@coefficients.all? { |coefficient| coefficient == 0 }
end
def to_s
return '0' if all_zeros?
has = false
polynom = ''
coefficients.each_with_index do |coefficient, id|
if has && coefficient != 0 then polynom += ' ' end
if coefficient > 0 && has then polynom += '+ ' end
if coefficient < 0 then polynom += '- ' end
if coefficient.abs > 1 then polynom += "#{coefficient.abs}" end
if coefficient.abs > 0
power = coefficients.size - id - 1
polynom += 'x' if power > 0
polynom += "^#{power}" if power > 1
has = true
end
end
polynom
end
end
....

Finished in 0.05814 seconds
4 examples, 0 failures
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Лилия Любенова
class Polynomial
attr_accessor :polynomial
def initialize(args)
@polynomial = args
end
alias original_to_s to_s
def to_s
return '0' if polynomial.all? {|coef| coef == 0}
string = ""
pow = polynomial.length - 1
polynomial.each do |coef|
string << ' + ' if coef > 0
string << ' - ' if coef < 0
case coef
when 0 then string << ""
when 1
case pow
when 0 then string << ""
when 1 then string << "x"
else string << "x^#{pow}"
end
else
case pow
when 0 then string << "#{coef.abs}"
when 1 then string << "#{coef.abs}x"
else string << "#{coef.abs}x^#{pow}"
end
end
pow -= 1
end
polynomial.first <= 0 ? string.slice(1..string.length) : string.slice(3..string.length)
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-1wgvjyn/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.01242 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1wgvjyn/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Давид Петров
class Polynomial
attr_accessor :coefficients
def initialize(coefficients)
@coefficients=coefficients
end
def to_s
if @coefficients.empty? or @coefficients.all? {|c| c.zero?}
"0"
else
representation=""
@coefficients.reverse.each_with_index do |c,i|
if c==0
monom=""
elsif c.abs==1
if i==0
monom= c>0 ? " + 1" : " - 1"
elsif i==1
monom= c>0 ? " + x" : " - x"
else
monom=c>0 ? " + x^#{i}" : " - x^#{i}"
end
elsif c<0
if i==0
monom=" - #{c.abs}"
elsif i==1
monom=" - #{c.abs}x"
else
monom=" - #{c.abs}x^#{i}"
end
else
if i==0
monom=" + #{c}"
elsif i==1
monom=" + #{c}x"
else
monom=" + #{c}x^#{i}"
end
end
representation.insert(0,monom)
end
representation=representation.strip
size=representation.size
representation.start_with?("+ ") ? representation.slice(2,size-2) : representation
end
end
end
....

Finished in 0.15116 seconds
4 examples, 0 failures
• Некоректно
• 2 успешни тест(а)
• 2 неуспешни тест(а)
Антонио Николов
class Polynomial
def initialize(polynomial_coefficients)
@polynomial_coefficients = polynomial_coefficients
end
def to_s
array = ""
@polynomial_coefficients.each_with_index do |coefficient, index|
if index == @polynomial_coefficients.count - 2 and coefficient < 0
coefficient.abs == 1 ? array += "- " + "x" + " " : array += "- " + "x" + " "
next
elsif index == @polynomial_coefficients.count - 2 and coefficient > 0
coefficient.abs == 1 ? array += "+ " + "x" + " " : array += "+ " + "x" + " "
next
end
case coefficient
when 1
array += "+ x^" + (@polynomial_coefficients.count - index - 1).to_s + " "
when -1
array += "- x^" + (@polynomial_coefficients.count - index - 1).to_s + " "
when 0
next
else
if coefficient < 0 then
array += "- " + coefficient.abs.to_s + "x^" + (@polynomial_coefficients.count - index - 1).to_s + " "
else
array += "+ " + coefficient.abs.to_s + "x^" + (@polynomial_coefficients.count - index - 1).to_s + " "
end
end
end
@polynomial_coefficients[@polynomial_coefficients.count - 1] != 0 ? array = array.chop.chop.chop.chop : array = array.chop
if @polynomial_coefficients > 0 then
array = array.reverse.chop.chop
array = array.reverse
end
array += "1" if @polynomial_coefficients[@polynomial_coefficients.count - 1].abs == 1
array == "" ? 0 : array
end
end
..FF

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + x"

(compared using ==)
# /tmp/d20131106-4393-wjmbqd/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: 0

(compared using ==)
# /tmp/d20131106-4393-wjmbqd/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.1386 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20131106-4393-wjmbqd/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-wjmbqd/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Цани Проданов
class Polynomial
attr_accessor :coefficients
def initialize array
@coefficients = array.drop_while {|coef| coef == 0}.reverse
end
def to_s
@coefficients.each_with_index { |item, index| readablePoly = parsePower(index, item) + readablePoly}
readablePoly.slice!(/^ \+ |^ /)
end
private
def parsePower(power, coefficient)
if coefficient==0
return ""
end
coefficient > 0 ? sign = " + " : sign = " - "
coefficient.abs == 1 ? coeffstr = sign : coeffstr = sign + coefficient.abs.to_s
if power == 0
coeffstr
elsif power == 1
coeffstr + "x"
else
coeffstr + "x^"+power.to_s
end
end
end
...F

Failures:

1) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-1o6f9fq/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.14434 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1o6f9fq/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Георги Кръстев
class Polynomial
def initialize(coefficients)
@coefficients = coefficients.reverse
end
def to_s
terms = @coefficients.map.with_index { |c, e| term c, e }
expression = terms.reverse.join.lstrip
normalized = expression.sub(/^\+ /, '').sub('x^0', '').sub('^1 ', ' ')
normalized.empty? ? '0' : normalized
end
def term(coefficient, exponent)
sign = coefficient < 0 ? '-' : '+'
multiplier = coefficient.abs == 1 ? '' : coefficient.abs
" #{sign} #{multiplier}x^#{exponent}" unless coefficient.zero?
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x^1"

(compared using ==)
# /tmp/d20131106-4393-bo2c2b/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.1168 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-bo2c2b/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 0 успешни тест(а)
• 4 неуспешни тест(а)
Веселин Генадиев
class Polynomial
def initialize(coefficients)
@coefficients = Array(coefficients)
end
def degree
coefficients.size - 1
end
def to_s
coefficients[0..degree].map.with_index { |coefficient, index|
coefficient > 0 && index != 0 ? sign = '+' : sign = '-'
[sign, " #{coefficient.abs}x^#{degree - index} "].join unless coefficient == 0
}.compact.join.gsub(/x\^0/, '').gsub(/ 1x/, ' x')
end
end
FFFF

Failures:

1) Polynomial formats a simple polynomial
Failure/Error: Polynomial.new([1, 0, 2]).to_s.should eq 'x^2 + 2'

expected: "x^2 + 2"
got: "- x^2 + 2 "

(compared using ==)
# /tmp/d20131106-4393-e9yy03/spec.rb:3:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: "- 3x^4 - 4x^3 + x^2 + 6 "

(compared using ==)
# /tmp/d20131106-4393-e9yy03/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x^1 "

(compared using ==)
# /tmp/d20131106-4393-e9yy03/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

4) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: ""

(compared using ==)
# /tmp/d20131106-4393-e9yy03/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.08929 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20131106-4393-e9yy03/spec.rb:2 # Polynomial formats a simple polynomial
rspec /tmp/d20131106-4393-e9yy03/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-e9yy03/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-e9yy03/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Некоректно
• 0 успешни тест(а)
• 0 неуспешни тест(а)
Методи Димитров
class Polynomial
def initialize(coefficients)
@coefficients = coefficients
end
def to_s
expression = @coefficients.reverse.each_with_index.map do |coefficient, index|
if coefficient.nonzero?
into_humans_language = case coefficient
when 1
"+ " + (index.zero? ? coefficient.to_s : "")
when -1
"- " + (index.zero? ? coefficient.abs.to_s : "")
else
if coefficient < -1
"- " + coefficient.abs.to_s
else
"+ " + coefficient.to_s
end
end
into_humans_language + case index
when 0
" "
when 1
"x "
else
"x^" + index.to_s + " "
end
else
""
end
end
expression = expression.reverse.reduce(:+).chop
if !expression.empty?
expression = expression.chars
expression = ""
expression = expression == '+' ? "" : expression
expression.reduce(:+)
else
"0"
end
end
/data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in require': /tmp/d20131106-4393-2bl0st/solution.rb:42: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
end
^
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in block in setup_load_path_and_require'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in each'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration_options.rb:25:in configure'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:21:in run'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in run'
from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in block in autorun'
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Петър Добрев
class Polynomial
attr_accessor :members
def initialize(coefficients)
if coefficients.all? { |x| x == 0 } then @members = ['0']
else @members = create_polynominal(coefficients).reverse
end
@members
end
def create_polynominal(coefficients)
parts = []
coefficients.each_index do |x|
parts.push(create_member coefficients.reverse[x], x, coefficients.size-1)
end
parts
end
def create_member(coefficient, power, max_power)
part = ''
if coefficient != 0 then
case power
when 0
part = coefficient.abs.to_s unless coefficient.abs == 1
when 1
part = coefficient.abs.to_s unless coefficient.abs == 1
part += 'x '
else
part = coefficient.abs.to_s unless coefficient.abs == 1
part += 'x^' + power.to_s + ' '
end
if coefficient < 0 then part = '- ' + part
elsif power != max_power then part = '+ ' + part
end
end
part
end
def to_s
@members.inject { |sum,x| sum + x}
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x "

(compared using ==)
# /tmp/d20131106-4393-yd7ccq/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.10935 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-yd7ccq/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Марио Даскалов
class Polynomial
def initialize(factors)
@factors= factors
end
def to_s
nonzero_factors = @factors.each_with_index.select do |factor, _|
factor.nonzero?
end
polynomial_string = nonzero_factors.map do |factor, index|
"#{factor > 0 ? '+' : '-'} #{factor.abs}x^#{@factors.length.pred - index}"
end.join(' ').gsub(/ 1x|^1x/,' x').gsub(/^\+ |x\^0|\^1/, '')
polynomial_string.empty? ? '0' : polynomial_string
end
end
....

Finished in 0.08668 seconds
4 examples, 0 failures
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Александър Попов
class Polynomial
def initialize(coefficients)
raise ArgumentError unless coefficients.instance_of? Array
@coefficients = coefficients
end
def to_s
return '0' if @coefficients.all?(&:zero?)
@coefficients.map!.with_index do |coefficient, index|
case
when coefficient < 0 then " - #{coefficient.abs}x^#{@coefficients.size - index - 1}"
when coefficient == -1 then " - x^#{@coefficients.size - index - 1}"
when coefficient == 0 then ''
when coefficient == 1 then " + x^#{@coefficients.size - index - 1}"
else " + #{coefficient.abs}x^#{@coefficients.size - index - 1}"
end
end
@coefficients.join('').strip.sub(/\A[+]\s/, '').sub("x^1", "x").sub("x^0", '')
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x"

(compared using ==)
# /tmp/d20131106-4393-1b4r97i/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.07688 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1b4r97i/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Борислава Аладжова
class Polynomial
def initialize(coefficients)
raise ArgumentError, 'Not enough arguments' if coefficients.size < 2
@coefficients = coefficients
end
def to_s
power = @coefficients.size - 1
polynomial = []
@coefficients.each do |coefficient|
unless coefficient.to_i.zero?
monom = check_arguments(coefficient.to_i, power) + check_power(power)
polynomial << monom
end
power -= 1
end
polynomial.empty? ? '0' : (polynomial.compact * ' + ').gsub('+ -', '- ')
end
def check_arguments(coefficient, power)
if (power.zero? || coefficient.abs != 1)
coefficient.to_s
else
coefficient < 0 ? '- ' : ''
end
end
def check_power(power)
if power.zero?
''
else
(power == 1) ? 'x' : "x^#{power}"
end
end
end
.F..

Failures:

1) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: "-3x^4 - 4x^3 + x^2 + 6"

(compared using ==)
# /tmp/d20131106-4393-ya1i3s/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.07096 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-ya1i3s/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Иван Георгиев
module Monomial
def get_coefficient (coefficient, exponent)
return coefficient if exponent == 0
return "" if (0..1).include? coefficient
coefficient
end
def get_variable (coefficient, exponent)
return "" if coefficient * exponent == 0
return "x" if exponent == 1
"x^" + exponent.to_s
end
def get_monomial (coefficient, exponent)
get_coefficient(coefficient, exponent).to_s + get_variable(coefficient, exponent)
end
end
class Polynomial
include Monomial
def initialize (coefficients)
@coefficients = coefficients
end
def to_s
result = coefficients.each_with_index.map{ |coefficient, index| get_monomial coefficient, coefficients.length - index - 1 }.map{|elem| elem == "" ? "" : '+ ' + elem}.map{|elem| elem.gsub '+ -', '- '}.reject(&:empty?).join(' ')
result = result[2..-1] if(result == '+')
result
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x + 0"

(compared using ==)
# /tmp/d20131106-4393-1nl075r/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.07178 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1nl075r/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Аделина Рудова
class Polynomial
attr_accessor :polynomial_power
attr_accessor :coefficients_array
def initialize(coefficients_array)
@polynomial_power = coefficients_array.length - 1
@coefficients_array = coefficients_array
end
def to_s
polynomial_to_s = ""
monomial = ""
sign = ""
actual_power = 0
return "0" if @coefficients_array.all? { |coefficient| coefficient.zero? }
@coefficients_array.each_with_index do |coefficient, index|
actual_power = @polynomial_power - index
if coefficient == 0
monomial = ""
sign = " "
elsif coefficient.abs == 1 and actual_power != 0
monomial = "x^#{actual_power}"
elsif actual_power == 0
monomial = "#{coefficient.abs}"
else
monomial = "#{coefficient.abs}x^#{actual_power}"
end
sign = "+" if polynomial_to_s != ""
sign = "-" if coefficient < 0
polynomial_to_s += sign + " " + monomial + " " unless monomial == ""
end
polynomial_to_s.strip
end
end
..F.

Failures:

1) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- x^3 - 2x^2 + 3x^1"

(compared using ==)
# /tmp/d20131106-4393-1s6g8xr/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.05838 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131106-4393-1s6g8xr/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
• Некоректно
• 0 успешни тест(а)
• 4 неуспешни тест(а)
Наталия Пацовска
class Polynomial
def initialize(coefficients)
raise ArgumentError if coefficients.empty?
@coefficients = coefficients
self
end
def to_s
s = @coefficients.map.with_index do |c, i|
"#{sign_at_index i}#{c.abs if c != 1}#{expr_at_index i}" if c != 0
end
s.any? ? s * " " : 0
end
private
def sign_at_index(index)
case
when @coefficients[index] < 0
"- "
when @coefficients[index] > 0 && index > 0
"+ "
else
""
end
end
def expr_at_index(index)
degree = @coefficients.count - index - 1
degree.zero? ? "" : "x^#{degree}"
end
end
FFFF

Failures:

1) Polynomial formats a simple polynomial
Failure/Error: Polynomial.new([1, 0, 2]).to_s.should eq 'x^2 + 2'

expected: "x^2 + 2"
got: "x^2  + 2"

(compared using ==)
# /tmp/d20131106-4393-ozi4md/spec.rb:3:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: "- 3x^4 - 4x^3 + x^2  + 6"

(compared using ==)
# /tmp/d20131106-4393-ozi4md/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: "- 1x^3 - 2x^2 + 3x^1 "

(compared using ==)
# /tmp/d20131106-4393-ozi4md/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

4) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: 0

(compared using ==)
# /tmp/d20131106-4393-ozi4md/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.00657 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20131106-4393-ozi4md/spec.rb:2 # Polynomial formats a simple polynomial
rspec /tmp/d20131106-4393-ozi4md/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-ozi4md/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-ozi4md/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Иван Латунов
class Polynomial
def to_s
output = []
@coefficients.reverse!
@coefficients.each_with_index do |coef,index|
sign = (coef < 0) ? "" : "+"
unless coef == 0
x = index != 0 ? (index == 1 ? "x" : "x^#{index}") : ""
case
when coef == -1 then coef = "-"
when (coef == 1 && index != 0) then coef = ""
end
output << "#{sign}#{coef}#{x}"
end
end
output = output.reverse
return "0" if output.empty?
output = output.gsub("+","")
output = output.join(" ")
output.gsub!('-', '- ')
output.gsub!('+', '+ ')
output
end
def initialize(list)
@coefficients = list.dup
end
end
....

Finished in 0.00612 seconds
4 examples, 0 failures
• Некоректно
• 0 успешни тест(а)
• 4 неуспешни тест(а)
Десислав Илиев
#!/usr/bin/ruby
class Polynomial
def initialize(list_of_coefficients)
@coefficients = []
head, *tail = list_of_coefficients
while head != nil
if head > 0
@coefficients << ["+ ", "#{head.abs}x", "^#{tail.size}"]
elsif head < 0
@coefficients << ["- ", "#{head.abs}x", "^#{tail.size}"]
end
head, *tail = tail
end
end
def to_s
return 0 if @coefficients == []
@coefficients.first = "" if @coefficients.first == "+ "
@coefficients.each do |sign, coefficient, power|
coefficient = "x" if coefficient == "1x"
power = "" if power == "^1"
if power == "^0"
power = ""
coefficient.gsub!('x','')
end
print sign + coefficient + power + " "
end
end
end
x^2 + 2 F- 3x^4 - 4x^3 + x^2 + 6 F- x^3 - 2x^2 + 3x FF

Failures:

1) Polynomial formats a simple polynomial
Failure/Error: Polynomial.new([1, 0, 2]).to_s.should eq 'x^2 + 2'

expected: "x^2 + 2"
got: [["", "1x", "^2"], ["+ ", "2", "^0"]]

(compared using ==)
# /tmp/d20131106-4393-1xbogoo/spec.rb:3:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

2) Polynomial formats a polynomial which contains negative and zero coefficients
Failure/Error: Polynomial.new([-3, -4, 1, 0, 6]).to_s.should eq '- 3x^4 - 4x^3 + x^2 + 6'

expected: "- 3x^4 - 4x^3 + x^2 + 6"
got: [["- ", "3x", "^4"], ["- ", "4x", "^3"], ["+ ", "1x", "^2"], ["+ ", "6", "^0"]]

(compared using ==)
# /tmp/d20131106-4393-1xbogoo/spec.rb:7:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

3) Polynomial formats a polynomial with first coefficient minus one
Failure/Error: Polynomial.new([-1, -2, 3, 0]).to_s.should eq '- x^3 - 2x^2 + 3x'

expected: "- x^3 - 2x^2 + 3x"
got: [["- ", "1x", "^3"], ["- ", "2x", "^2"], ["+ ", "3x", "^1"]]

(compared using ==)
# /tmp/d20131106-4393-1xbogoo/spec.rb:11:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

4) Polynomial formats a polynomial which contains only zero coefficients
Failure/Error: Polynomial.new([0, 0, 0]).to_s.should eq '0'

expected: "0"
got: 0

(compared using ==)
# /tmp/d20131106-4393-1xbogoo/spec.rb:15:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:5:in block (2 levels) in <top (required)>'

Finished in 0.00665 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20131106-4393-1xbogoo/spec.rb:2 # Polynomial formats a simple polynomial
rspec /tmp/d20131106-4393-1xbogoo/spec.rb:6 # Polynomial formats a polynomial which contains negative and zero coefficients
rspec /tmp/d20131106-4393-1xbogoo/spec.rb:10 # Polynomial formats a polynomial with first coefficient minus one
rspec /tmp/d20131106-4393-1xbogoo/spec.rb:14 # Polynomial formats a polynomial which contains only zero coefficients