Решение на Трета задача от Кристъфър Коруев

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

Към профила на Кристъфър Коруев

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 6 успешни тест(а)
  • 14 неуспешни тест(а)

Код

class PrimeSequence
include Enumerable
def initialize(length)
@length_of_seq = length
end
def each
counter = 0
while counter < @length_of_seq
yield counter
counter+=1
end
end
def to_a
get_first_n_prime_numbers
end
private
def get_first_n_prime_numbers
prime_numbers = enum_for(:each_number).
lazy.
select { |x| is_prime?(x) }.
take(@length_of_seq).
to_a
prime_numbers
end
def is_prime?(n)
array_up_to_n = 2.upto(n).to_a
divided_numbers = array_up_to_n.select { |x| n.remainder(x) == 0 }
divided_numbers.length == 1
end
def each_number
n = 0
loop do
n+=1
yield n
end
end
end
class FibonacciSequence
include Enumerable
def initialize(length, first: 1, second: 1)
@length_of_seq = length
@first_number = first
@second_number = second
end
def to_a
get_first_n_fib_numbers
end
private
def get_first_n_fib_numbers
fib_numbers = enum_for(:each_fib_number).
lazy.
select { |x| x >= 0 }.
take(@length_of_seq).
to_a
fib_numbers
end
def each_fib_number
first_number = @first_number
yield first_number
second_number = @second_number
yield second_number
next_number = 0
loop do
next_number = first_number + second_number
first_number = second_number
second_number = next_number
yield next_number
end
end
end

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

FFFF...F...FFFFFFFFF

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)
     NameError:
       uninitialized constant RationalSequence
     # /tmp/d20151111-27349-m5kjg7/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 []
     NameError:
       uninitialized constant RationalSequence
     # /tmp/d20151111-27349-m5kjg7/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(
     NameError:
       uninitialized constant RationalSequence
     # /tmp/d20151111-27349-m5kjg7/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 }
     NameError:
       uninitialized constant RationalSequence
     # /tmp/d20151111-27349-m5kjg7/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 FibonacciSequence is properly enumerable
     Failure/Error: expect(FibonacciSequence.new(20).select { |x| x.even? }).to eq [
     NoMethodError:
       undefined method `each' for #<FibonacciSequence:0x007f87e222a2d8>
     # /tmp/d20151111-27349-m5kjg7/spec.rb:45:in `select'
     # /tmp/d20151111-27349-m5kjg7/spec.rb:45: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
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/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)
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/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)
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/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 #aimless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.aimless(3)).to eq(Rational(2, 3) + Rational(5, 1))
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/spec.rb:88: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 #aimless can calculate for 4
     Failure/Error: expect(DrunkenMathematician.aimless(4)).to eq(Rational(2, 3) + Rational(5, 7))
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/spec.rb:92: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 #aimless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.aimless(42)).to eq expected
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/spec.rb:97: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)>'

  12) Fifth task DrunkenMathematician #worthless can calculate for 2
     Failure/Error: expect(DrunkenMathematician.worthless(2)).to eq %w(1/1).map(&:to_r)
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/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)>'

  13) Fifth task DrunkenMathematician #worthless can calculate for 8
     Failure/Error: expect(DrunkenMathematician.worthless(8)).to eq expected
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/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)>'

  14) Fifth task DrunkenMathematician #worthless can calculate for 15
     Failure/Error: expect(DrunkenMathematician.worthless(15)).to eq %w(
     NameError:
       uninitialized constant DrunkenMathematician
     # /tmp/d20151111-27349-m5kjg7/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.04439 seconds
20 examples, 14 failures

Failed examples:

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

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

Кристъфър обнови решението на 26.10.2015 00:54 (преди над 8 години)

+class PrimeSequence
+ include Enumerable
+
+ def initialize(length)
+ @length_of_seq = length
+ end
+
+ def each
+ counter = 0
+
+ while counter < @length_of_seq
+ yield counter
+ counter+=1
+ end
+ end
+
+ def to_a
+ get_first_n_prime_numbers
+ end
+
+ private
+
+ def get_first_n_prime_numbers
+ prime_numbers = enum_for(:each_number).
+ lazy.
+ select { |x| is_prime?(x) }.
+ take(@length_of_seq).
+ to_a
+
+ prime_numbers
+ end
+
+ def is_prime?(n)
+ array_up_to_n = 2.upto(n).to_a
+ divided_numbers = array_up_to_n.select { |x| n.remainder(x) == 0 }
+ divided_numbers.length == 1
+ end
+
+ def each_number
+ n = 0
+ loop do
+ n+=1
+ yield n
+ end
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(length, first: 1, second: 1)
+ @length_of_seq = length
+ @first_number = first
+ @second_number = second
+ end
+
+ def to_a
+ get_first_n_fib_numbers
+ end
+
+ private
+
+ def get_first_n_fib_numbers
+ fib_numbers = enum_for(:each_fib_number).
+ lazy.
+ select { |x| x >= 0 }.
+ take(@length_of_seq).
+ to_a
+
+ fib_numbers
+ end
+
+ def each_fib_number
+ first_number = @first_number
+ yield first_number
+ second_number = @second_number
+ yield second_number
+ next_number = 0
+ loop do
+ next_number = first_number + second_number
+ first_number = second_number
+ second_number = next_number
+ yield next_number
+ end
+ end
+end