Решение на Трета задача от Иван Станков

Обратно към всички решения

Към профила на Иван Станков

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 9 успешни тест(а)
  • 11 неуспешни тест(а)

Код

class Sequence
include Enumerable
def initialize(length)
@length = length
@sequence = Array.new(length)
end
def each
0.upto(@sequence.length - 1) do |x|
yield @sequence[x]
end
end
def rationalize (length)
(1..(length - 1)).each do |i|
(1..(length - 1)).each do |j|
@array[i-1][j-1] = Rational(i,j)
end
end
end
def add_prime
i=1
prime = 3
until i > @length - 1 do
if prime?(prime) then
@sequence[i] = prime
prime += 1
i += 1
else prime += 1
end
end
end
end
class RationalSequence < Sequence
def initialize(length)
super(length)
if length == 0 then @sequence = nil
else
@sequence[0] = Rational(1,1)
end
@array = Array.new(length-1){Array.new(length-1)}
last = @sequence[0]
if length > 1 then rationalize (length) end
i=1
@array.each do |x,y|
# Take only the needed number from @array into @Sequence
end
end
end
class PrimeSequence < Sequence
def initialize(length)
super(length)
@sequence[0] = 2
if length > 1
add_prime
end
end
end
class FibonacciSequence < Sequence
def initialize(length, initial = {first: 1, second: 1})
super(length)
if @length == 1
@sequence[0] = initial[:first]
elsif @length >= 2
@sequence= [initial[:first], initial[:second]]
i = 2
until i > @length - 1 do
@sequence[i] = @sequence[i-1] + @sequence[i-2]
i += 1
end
end
end
end
module DrunkenMathematician
module_function
def meaningless (n)
if n == 1 then
'1/1'.to_r
else
rational_sequence = RationalSequence.new(n)
array = rational_sequence.to_a
get_product(n,array)
'#{product_one.to_r}/#{product_two.to_r}'.to_r
end
end
def aimless (n)
sequence = PrimeSequence.new(n).to_a
if n % 2 == 1 then sequence << 1 end
sum = 0
for i in 0..((n-1)/2)
sum += Rational(sequence[i*2], sequence[i*2+1])
end
sum.to_r
end
def get_product(n,array)
product_one=1.to_r
product_two=1.to_r
for x in 0..(n-1)
if prime? array[x].to_r.denominator or prime? array[x].to_r.nomerator
product_one *= array[x].to_r
print product_one
else
product_two *= array[x].to_r
end
end
end
def worthless (n)
end
end
def prime?(number)
if number == 2 then true end
for n in 2..(number - 1)
if number % n == 0
return false
end
end
true
end

Лог от изпълнението

FFFF....F..F1/10/10/10/1F1/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/10/1F...FFF

Failures:

  1) Fifth task RationalSequence can calculate the first four rational numbers
     Failure/Error: expect(RationalSequence.new(4).to_a).to eq %w(1/1 2/1 1/2 1/3).map(&:to_r)
       
       expected: [(1/1), (2/1), (1/2), (1/3)]
            got: [(1/1), nil, nil, nil]
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:4:in `block (3 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) Fifth task RationalSequence returns an empty array when count is 0
     Failure/Error: expect(RationalSequence.new(0).to_a).to eq []
     ArgumentError:
       negative array size
     # /tmp/d20151111-27349-9ks6k6/solution.rb:42:in `initialize'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:42:in `new'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:42:in `initialize'
     # /tmp/d20151111-27349-9ks6k6/spec.rb:8:in `new'
     # /tmp/d20151111-27349-9ks6k6/spec.rb:8:in `block (3 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) Fifth task RationalSequence can calculate the first 28 rational numbers
     Failure/Error: expect(RationalSequence.new(28).to_a).to eq %w(
       
       expected: [(1/1), (2/1), (1/2), (1/3), (3/1), (4/1), (3/2), (2/3), (1/4), (1/5), (5/1), (6/1), (5/2), (4/3), (3/4), (2/5), (1/6), (1/7), (3/5), (5/3), (7/1), (8/1), (7/2), (5/4), (4/5), (2/7), (1/8), (1/9)]
            got: [(1/1), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:12:in `block (3 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) Fifth task RationalSequence is properly enumerable
     Failure/Error: ones = RationalSequence.new(28).select { |r| r.numerator == 1 }
     NoMethodError:
       undefined method `numerator' for nil:NilClass
     # /tmp/d20151111-27349-9ks6k6/spec.rb:19:in `block (4 levels) in <top (required)>'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:10:in `block in each'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:9:in `upto'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:9:in `each'
     # /tmp/d20151111-27349-9ks6k6/spec.rb:19:in `select'
     # /tmp/d20151111-27349-9ks6k6/spec.rb:19:in `block (3 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)>'

  5) Fifth task PrimeSequence returns an empty array for 0 primes
     Failure/Error: expect(PrimeSequence.new(0).to_a).to eq []
       
       expected: []
            got: [2]
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:53:in `block (3 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)>'

  6) Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
     Failure/Error: expect(DrunkenMathematician.meaningless(0)).to eq 1
     ArgumentError:
       negative array size
     # /tmp/d20151111-27349-9ks6k6/solution.rb:42:in `initialize'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:42:in `new'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:42:in `initialize'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:85:in `new'
     # /tmp/d20151111-27349-9ks6k6/solution.rb:85:in `meaningless'
     # /tmp/d20151111-27349-9ks6k6/spec.rb:73:in `block (4 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)>'

  7) Fifth task DrunkenMathematician #meaningless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.meaningless(4)).to eq Rational(1, 3)
       
       expected: (1/3)
            got: (0/1)
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:78:in `block (4 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)>'

  8) Fifth task DrunkenMathematician #meaningless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.meaningless(42)).to eq Rational(1, 11)
       
       expected: (1/11)
            got: (0/1)
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:82:in `block (4 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)>'

  9) Fifth task DrunkenMathematician #worthless can calculate for 2
     Failure/Error: expect(DrunkenMathematician.worthless(2)).to eq %w(1/1).map(&:to_r)
       
       expected: [(1/1)]
            got: nil
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:103:in `block (4 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)>'

  10) Fifth task DrunkenMathematician #worthless can calculate for 8
     Failure/Error: expect(DrunkenMathematician.worthless(8)).to eq expected
       
       expected: [(1/1), (2/1), (1/2), (1/3), (3/1), (4/1), (3/2), (2/3), (1/4), (1/5), (5/1)]
            got: nil
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:108:in `block (4 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)>'

  11) Fifth task DrunkenMathematician #worthless can calculate for 15
     Failure/Error: expect(DrunkenMathematician.worthless(15)).to eq %w(
       
       expected: [(1/1), (2/1), (1/2), (1/3), (3/1), (4/1), (3/2), (2/3), (1/4), (1/5), (5/1), (6/1), (5/2), (4/3), (3/4), (2/5), (1/6), (1/7), (3/5), (5/3), (7/1), (8/1), (7/2), (5/4), (4/5), (2/7), (1/8), (1/9), (3/7), (7/3), (9/1), (10/1), (9/2), (8/3), (7/4), (6/5), (5/6), (4/7), (3/8), (2/9), (1/10), (1/11), (5/7), (7/5), (11/1), (12/1), (11/2), (10/3), (9/4), (8/5), (7/6), (6/7), (5/8), (4/9), (3/10), (2/11), (1/12), (1/13), (3/11), (5/9), (9/5), (11/3), (13/1), (14/1), (13/2), (11/4), (8/7), (7/8), (4/11), (2/13), (1/14), (1/15), (3/13), (5/11), (7/9), (9/7), (11/5), (13/3), (15/1), (16/1), (15/2), (14/3), (13/4), (12/5), (11/6), (10/7), (9/8), (8/9), (7/10), (6/11), (5/12), (4/13), (3/14), (2/15), (1/16), (1/17), (5/13), (7/11), (11/7), (13/5), (17/1), (18/1), (17/2), (16/3), (15/4), (14/5), (13/6), (12/7), (11/8), (10/9), (9/10), (8/11), (7/12), (6/13), (5/14), (4/15), (3/16), (2/17), (1/18), (1/19), (3/17), (7/13), (9/11), (11/9), (13/7), (17/3), (19/1), (20/1), (19/2), (17/4), (16/5), (13/8), (11/10), (10/11), (8/13), (5/16), (4/17), (2/19), (1/20), (1/21), (3/19), (5/17), (7/15), (9/13), (13/9), (15/7), (17/5), (19/3), (21/1), (22/1), (21/2), (20/3), (19/4), (18/5), (17/6), (16/7), (15/8), (14/9), (13/10), (12/11), (11/12), (10/13), (9/14), (8/15), (7/16), (6/17), (5/18), (4/19), (3/20), (2/21), (1/22), (1/23), (5/19), (7/17), (11/13), (13/11), (17/7), (19/5), (23/1), (24/1), (23/2), (22/3), (21/4), (19/6), (18/7), (17/8), (16/9), (14/11), (13/12), (12/13), (11/14), (9/16), (8/17), (7/18), (6/19), (4/21), (3/22), (2/23), (1/24), (1/25), (3/23), (5/21), (7/19), (9/17), (11/15), (15/11), (17/9), (19/7), (21/5), (23/3)]
            got: nil
       
       (compared using ==)
     # /tmp/d20151111-27349-9ks6k6/spec.rb:112:in `block (4 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.01768 seconds
20 examples, 11 failures

Failed examples:

rspec /tmp/d20151111-27349-9ks6k6/spec.rb:3 # Fifth task RationalSequence can calculate the first four rational numbers
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:7 # Fifth task RationalSequence returns an empty array when count is 0
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:11 # Fifth task RationalSequence can calculate the first 28 rational numbers
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:18 # Fifth task RationalSequence is properly enumerable
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:52 # Fifth task PrimeSequence returns an empty array for 0 primes
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:77 # Fifth task DrunkenMathematician #meaningless can calculate for 3
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:81 # Fifth task DrunkenMathematician #meaningless can calculate for 42
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:102 # Fifth task DrunkenMathematician #worthless can calculate for 2
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-9ks6k6/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

История (1 версия и 0 коментара)

Иван обнови решението на 25.10.2015 14:22 (преди над 8 години)

+class Sequence
+ include Enumerable
+ def initialize(length)
+ @length = length
+ @sequence = Array.new(length)
+ end
+
+ def each
+ 0.upto(@sequence.length - 1) do |x|
+ yield @sequence[x]
+ end
+ end
+
+ def rationalize (length)
+ (1..(length - 1)).each do |i|
+ (1..(length - 1)).each do |j|
+ @array[i-1][j-1] = Rational(i,j)
+ end
+ end
+ end
+ def add_prime
+ i=1
+ prime = 3
+ until i > @length - 1 do
+ if prime?(prime) then
+ @sequence[i] = prime
+ prime += 1
+ i += 1
+ else prime += 1
+ end
+ end
+ end
+end
+
+class RationalSequence < Sequence
+ def initialize(length)
+ super(length)
+ if length == 0 then @sequence = nil
+ else
+ @sequence[0] = Rational(1,1)
+ end
+ @array = Array.new(length-1){Array.new(length-1)}
+ last = @sequence[0]
+ if length > 1 then rationalize (length) end
+ i=1
+ @array.each do |x,y|
+ # Take only the needed number from @array into @Sequence
+ end
+ end
+
+end
+
+class PrimeSequence < Sequence
+ def initialize(length)
+ super(length)
+ @sequence[0] = 2
+ if length > 1
+ add_prime
+ end
+ end
+end
+
+class FibonacciSequence < Sequence
+ def initialize(length, initial = {first: 1, second: 1})
+ super(length)
+ if @length == 1
+ @sequence[0] = initial[:first]
+ elsif @length >= 2
+ @sequence= [initial[:first], initial[:second]]
+ i = 2
+ until i > @length - 1 do
+ @sequence[i] = @sequence[i-1] + @sequence[i-2]
+ i += 1
+ end
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+ def meaningless (n)
+ if n == 1 then
+ '1/1'.to_r
+ else
+ rational_sequence = RationalSequence.new(n)
+ array = rational_sequence.to_a
+ get_product(n,array)
+ '#{product_one.to_r}/#{product_two.to_r}'.to_r
+ end
+ end
+
+ def aimless (n)
+ sequence = PrimeSequence.new(n).to_a
+ if n % 2 == 1 then sequence << 1 end
+ sum = 0
+ for i in 0..((n-1)/2)
+ sum += Rational(sequence[i*2], sequence[i*2+1])
+ end
+ sum.to_r
+ end
+ def get_product(n,array)
+ product_one=1.to_r
+ product_two=1.to_r
+ for x in 0..(n-1)
+ if prime? array[x].to_r.denominator or prime? array[x].to_r.nomerator
+ product_one *= array[x].to_r
+ print product_one
+ else
+ product_two *= array[x].to_r
+ end
+ end
+ end
+ def worthless (n)
+ end
+end
+
+def prime?(number)
+ if number == 2 then true end
+ for n in 2..(number - 1)
+ if number % n == 0
+ return false
+ end
+ end
+ true
+end