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

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

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

Резултати

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

Код

class Integer
def prime?
return false if self < 2
return true if self == 2
[*2...self].all? { |number| self % number != 0 }
end
end
class RationalSequence
include Enumerable
def initialize(count)
@count = count
end
def each
i = j = 1
direction = 1
changed = false
unique_numbers = []
@count.times do |x|
new_number = Rational(i, j)
while unique_numbers.include? new_number
i, j, direction, changed = *increment_direction(i, j, direction, changed)
new_number = Rational(i, j)
end
unique_numbers << new_number
yield new_number
end
end
private
def increment_direction(i, j, direction, direction_changed)
if j == 1 and not direction_changed
direction_changed = true
direction = -direction
i += 1
elsif i == 1 and not direction_changed
direction_changed = true
direction = -direction
j += 1
else
direction_changed = false
i += direction
j -= direction
end
[i, j, direction, direction_changed]
end
end
class PrimeSequence
include Enumerable
def initialize(count)
@count = count
end
def each
prime_numbers = (2..Float::INFINITY).lazy.select { |number| number.prime? }.take(@count)
prime_numbers.each { |number| yield number }
end
end
class FibonacciSequence
include Enumerable
def initialize(count, first: 1, second: 1)
@count = count
@first_number = first
@second_number = second
end
def each
i = @first_number
j = @second_number
@count.times do |x|
i, j = j, i + j
yield i
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
rational = RationalSequence.new(n)
groups = rational.partition { |number| number.numerator.prime? or number.denominator.prime? }
groups[0].reduce(1, :*) / groups[1].reduce(1, :*)
end
def aimless(n)
prime_numbers = PrimeSequence.new(n).to_a
prime_numbers << 1 if n.odd?
rational_numbers = []
prime_numbers.each_slice(2) { |array| rational_numbers << Rational(array[0], array[1]) }
rational_numbers.reduce(0, :+)
end
def worthless(n)
fibonacci_number = FibonacciSequence.new(n).to_a.last
rational_numbers = RationalSequence.new(n).to_a
sum = 0
rational_numbers.take_while do |number|
sum += number
sum <= fibonacci_number
end
end
end

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

....FFFF..........FF

Failures:

  1) Fifth task FibonacciSequence can return the first two Fibonacci numbers
     Failure/Error: expect(FibonacciSequence.new(2).to_a).to eq [1, 1]
       
       expected: [1, 1]
            got: [1, 2]
       
       (compared using ==)
     # /tmp/d20151111-27349-14n8fo9/spec.rb:26: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 FibonacciSequence can return the first 20 Fibonacci numbers
     Failure/Error: expect(FibonacciSequence.new(20).to_a).to eq [
       
       expected: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
            got: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]
       
       (compared using ==)
     # /tmp/d20151111-27349-14n8fo9/spec.rb:30: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 FibonacciSequence can be used to calculate the Lucas numbers
     Failure/Error: expect(FibonacciSequence.new(31, first: 2, second: 1).to_a).to eq [
       
       expected: [2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843, 1364, 2207, 3571, 5778, 9349, 15127, 24476, 39603, 64079, 103682, 167761, 271443, 439204, 710647, 1149851, 1860498]
            got: [1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843, 1364, 2207, 3571, 5778, 9349, 15127, 24476, 39603, 64079, 103682, 167761, 271443, 439204, 710647, 1149851, 1860498, 3010349]
       
       (compared using ==)
     # /tmp/d20151111-27349-14n8fo9/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)>'

  4) Fifth task FibonacciSequence is properly enumerable
     Failure/Error: expect(FibonacciSequence.new(20).select { |x| x.even? }).to eq [
       
       expected: [2, 8, 34, 144, 610, 2584]
            got: [2, 8, 34, 144, 610, 2584, 10946]
       
       (compared using ==)
     # /tmp/d20151111-27349-14n8fo9/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)>'

  5) 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: [(1/1), (2/1), (1/2), (1/3), (3/1), (4/1), (3/2), (2/3)]
       
       (compared using ==)
     # /tmp/d20151111-27349-14n8fo9/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)>'

  6) 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: [(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)]
       
       (compared using ==)
     # /tmp/d20151111-27349-14n8fo9/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.02529 seconds
20 examples, 6 failures

Failed examples:

rspec /tmp/d20151111-27349-14n8fo9/spec.rb:25 # Fifth task FibonacciSequence can return the first two Fibonacci numbers
rspec /tmp/d20151111-27349-14n8fo9/spec.rb:29 # Fifth task FibonacciSequence can return the first 20 Fibonacci numbers
rspec /tmp/d20151111-27349-14n8fo9/spec.rb:36 # Fifth task FibonacciSequence can be used to calculate the Lucas numbers
rspec /tmp/d20151111-27349-14n8fo9/spec.rb:44 # Fifth task FibonacciSequence is properly enumerable
rspec /tmp/d20151111-27349-14n8fo9/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-14n8fo9/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

Мариян обнови решението на 26.10.2015 16:46 (преди над 8 години)

+class Integer
+
+ def prime?
+ return false if self < 2
+ return true if self == 2
+ [*2...self].all? { |number| self % number != 0 }
+ end
+
+end
+
+class RationalSequence
+
+ include Enumerable
+
+ def initialize(count)
+ @count = count
+ end
+
+ def each
+ i = j = 1
+ direction = 1
+ changed = false
+ unique_numbers = []
+ @count.times do |x|
+ new_number = Rational(i, j)
+ while unique_numbers.include? new_number
+ i, j, direction, changed = *increment_direction(i, j, direction, changed)
+ new_number = Rational(i, j)
+ end
+ unique_numbers << new_number
+ yield new_number
+ end
+ end
+
+ private
+
+ def increment_direction(i, j, direction, direction_changed)
+ if j == 1 and not direction_changed
+ direction_changed = true
+ direction = -direction
+ i += 1
+ elsif i == 1 and not direction_changed
+ direction_changed = true
+ direction = -direction
+ j += 1
+ else
+ direction_changed = false
+ i += direction
+ j -= direction
+ end
+ [i, j, direction, direction_changed]
+ end
+
+end
+
+class PrimeSequence
+
+ include Enumerable
+
+ def initialize(count)
+ @count = count
+ end
+
+ def each
+ prime_numbers = (2..Float::INFINITY).lazy.select { |number| number.prime? }.take(@count)
+ prime_numbers.each { |number| yield number }
+ end
+
+end
+
+class FibonacciSequence
+
+ include Enumerable
+
+ def initialize(count, first: 1, second: 1)
+ @count = count
+ @first_number = first
+ @second_number = second
+ end
+
+ def each
+ i = @first_number
+ j = @second_number
+ @count.times do |x|
+ i, j = j, i + j
+ yield i
+ end
+ end
+
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ rational = RationalSequence.new(n)
+ groups = rational.partition { |number| number.numerator.prime? or number.denominator.prime? }
+ groups[0].reduce(1, :*) / groups[1].reduce(1, :*)
+ end
+
+ def aimless(n)
+ prime_numbers = PrimeSequence.new(n).to_a
+ prime_numbers << 1 if n.odd?
+ rational_numbers = []
+ prime_numbers.each_slice(2) { |array| rational_numbers << Rational(array[0], array[1]) }
+ rational_numbers.reduce(0, :+)
+ end
+
+ def worthless(n)
+ fibonacci_number = FibonacciSequence.new(n).to_a.last
+ rational_numbers = RationalSequence.new(n).to_a
+ sum = 0
+ rational_numbers.take_while do |number|
+ sum += number
+ sum <= fibonacci_number
+ end
+ end
+
+end

Без да искам съм разменил 2 реда докато съм писал останалата част от кода и сега голяма част от тестовете може и да се провалят. Надявам се, че няма да се счете за грешка. Говоря за тези два реда в метода each в класа FibonacciSequence:
yield i
i, j = j, i + j

Марияне, счита се за грешка - така научаваш много ценен урок на цената на 1-2 точки от задачата :)

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

Освен това, имаш стилови проблеми на места, трябва да поработиш над стила си. Направи справка с ръководството за стил. А i и j са неща, които бих очаквал да видя в C код, но не и в Ruby.

Не оставяй празен ред преди end или след начало на дефиниция на клас/модул.