Решение на Трета задача от Мария Османлиева

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

Към профила на Мария Османлиева

Резултати

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

Код

# само върху това имам въпроси последно, съжалявам за другите версии
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def line current
dividend = 1
array_helper = []
while dividend <= current do
array_helper << Rational(dividend, (current + 1 - dividend))
dividend = dividend + 1
end
array_helper.reverse_each if current % 2 == 0
return array_helper
end
def to_a
return [] if @limit == 0
array = []
current = 1
while array.count <= @limit do
array = array | line(current)
current += 1
end
return array.take(@limit)
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def prime?(number)
(2..number/2).none?{ |i| number % i == 0}
end
def to_a
current, amount = 2, 0
array_of_primes = []
while amount < @limit do
if prime?(current)
array_of_primes << current
amount += 1
end
current += 1
end
return array_of_primes
end
end
class FibonacciSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def to_a
number = @limit
fibonacci = ->(number, i = 0, j = 1){ (1..number).map{i = j + j = i}}
fibonacci[number]
end
end

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

F.FF..FF...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)
       
       expected: [(1/1), (2/1), (1/2), (1/3)]
            got: [(1/1), (1/2), (2/1), (1/3)]
       
       (compared using ==)
     # /tmp/d20151111-27349-ii3v8x/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 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), (1/2), (2/1), (1/3), (3/1), (1/4), (2/3), (3/2), (4/1), (1/5), (5/1), (1/6), (2/5), (3/4), (4/3), (5/2), (6/1), (1/7), (3/5), (5/3), (7/1), (1/8), (2/7), (4/5), (5/4), (7/2), (8/1), (1/9)]
       
       (compared using ==)
     # /tmp/d20151111-27349-ii3v8x/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)>'

  3) Fifth task RationalSequence is properly enumerable
     Failure/Error: ones = RationalSequence.new(28).select { |r| r.numerator == 1 }
     NoMethodError:
       undefined method `each' for #<RationalSequence:0x007fbe456f7420 @limit=28>
     # /tmp/d20151111-27349-ii3v8x/spec.rb:19:in `select'
     # /tmp/d20151111-27349-ii3v8x/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)>'

  4) Fifth task FibonacciSequence can be used to calculate the Lucas numbers
     Failure/Error: expect(FibonacciSequence.new(31, first: 2, second: 1).to_a).to eq [
     ArgumentError:
       wrong number of arguments (2 for 1)
     # /tmp/d20151111-27349-ii3v8x/solution.rb:60:in `initialize'
     # /tmp/d20151111-27349-ii3v8x/spec.rb:37:in `new'
     # /tmp/d20151111-27349-ii3v8x/spec.rb:37: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:0x007fbe456a29e8 @limit=20>
     # /tmp/d20151111-27349-ii3v8x/spec.rb:45:in `select'
     # /tmp/d20151111-27349-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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-ii3v8x/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.02824 seconds
20 examples, 14 failures

Failed examples:

rspec /tmp/d20151111-27349-ii3v8x/spec.rb:3 # Fifth task RationalSequence can calculate the first four rational numbers
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:11 # Fifth task RationalSequence can calculate the first 28 rational numbers
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:18 # Fifth task RationalSequence is properly enumerable
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:36 # Fifth task FibonacciSequence can be used to calculate the Lucas numbers
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:44 # Fifth task FibonacciSequence is properly enumerable
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:77 # Fifth task DrunkenMathematician #meaningless can calculate for 3
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:81 # Fifth task DrunkenMathematician #meaningless can calculate for 42
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:87 # Fifth task DrunkenMathematician #aimless can calculate for 3
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:91 # Fifth task DrunkenMathematician #aimless can calculate for 4
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:95 # Fifth task DrunkenMathematician #aimless can calculate for 42
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:102 # Fifth task DrunkenMathematician #worthless can calculate for 2
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-ii3v8x/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

История (3 версии и 4 коментара)

Мария обнови решението на 24.10.2015 02:15 (преди над 8 години)

+# Май не всичко от условието ми е ясно. Моля за коментар.
+# Мога ли да си използвам класовете в DrunkenMathematician ?
+
+class RationalSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def line current
+ x = 1
+ b = []
+ while x <= current do
+ b << Rational(x, (current+1-x))
+ x = x + 1
+ end
+ b.reverse_each if current % 2 == 0
+ return b
+ end
+
+ def to_a
+ return [] if @limit == 0
+ array = []
+ i = 1
+ while array.count <= @limit do
+ array = array | line(i)
+ i = i + 1
+ end
+ return array.take(@limit)
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def to_a
+ require 'prime'
+ Prime.take(@limit)
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ current, previous = 1, 0
+
+ while current <= @limit
+ yield current
+ current, previous = current + previous, current
+ end
+ end
+end

Мария обнови решението на 24.10.2015 02:36 (преди над 8 години)

# Май не всичко от условието ми е ясно. Моля за коментар.
# Мога ли да си използвам класовете в DrunkenMathematician ?
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def line current
x = 1
b = []
while x <= current do
b << Rational(x, (current+1-x))
x = x + 1
end
b.reverse_each if current % 2 == 0
return b
end
def to_a
return [] if @limit == 0
array = []
i = 1
while array.count <= @limit do
array = array | line(i)
i = i + 1
end
return array.take(@limit)
end
-end
-
-class PrimeSequence
- include Enumerable
-
- def initialize(limit)
- @limit = limit
- end
-
- def to_a
- require 'prime'
- Prime.take(@limit)
- end
-end
-
-class FibonacciSequence
- include Enumerable
-
- def initialize(limit)
- @limit = limit
- end
-
- def each
- current, previous = 1, 0
-
- while current <= @limit
- yield current
- current, previous = current + previous, current
- end
- end
end

Мария обнови решението на 24.10.2015 03:29 (преди над 8 години)

-# Май не всичко от условието ми е ясно. Моля за коментар.
-# Мога ли да си използвам класовете в DrunkenMathematician ?
-
+# само върху това имам въпроси последно, съжалявам за другите версии
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def line current
- x = 1
- b = []
- while x <= current do
- b << Rational(x, (current+1-x))
- x = x + 1
+ dividend = 1
+ array_helper = []
+ while dividend <= current do
+ array_helper << Rational(dividend, (current + 1 - dividend))
+ dividend = dividend + 1
end
- b.reverse_each if current % 2 == 0
- return b
+ array_helper.reverse_each if current % 2 == 0
+ return array_helper
end
def to_a
return [] if @limit == 0
array = []
- i = 1
+ current = 1
while array.count <= @limit do
- array = array | line(i)
- i = i + 1
+ array = array | line(current)
+ current += 1
end
return array.take(@limit)
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def prime?(number)
+ (2..number/2).none?{ |i| number % i == 0}
+ end
+
+ def to_a
+ current, amount = 2, 0
+ array_of_primes = []
+ while amount < @limit do
+ if prime?(current)
+ array_of_primes << current
+ amount += 1
+ end
+ current += 1
+ end
+ return array_of_primes
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def to_a
+ number = @limit
+ fibonacci = ->(number, i = 0, j = 1){ (1..number).map{i = j + j = i}}
+ fibonacci[number]
end
end

Напиши си въпросите като коментар.

Като цяло идейният проблем е, че следваше да използвате функционалността, която ви дава Enumerable. Може да погледнеш тук за пример, в който се показва всичко нужно, за да се държи класът ти като Enumerable.

Не че ще ти вземем 6-те точки. Написала си, че имаш въпроси, но не си уточнила какви. Предположих, че са свързани с използването на Enumerable.

Като цяло няма смисъл да include Enumerable, ако не имплементираш each. И рядко има нужда експлицитно да дефинираш to_a, ако имаш include Enumerable.

Мария, неволно беше оставила последната дума от коментара си с въпроса в началото на решението на нов ред и затова ти гърмяха всички тестове и получаваше нула точки.

Малко е тъпо да нямаш никакъв шанс за точки от тази задача заради това, че си питала в кода, а не в полето за коментар, затова ти го редактирах.

НО - това няма да се повтори повече. Вземи си бележка какво става като предаваш код, който не си тествала.