Решение на Трета задача от Димитър Ангелов

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

Към профила на Димитър Ангелов

Резултати

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

Код

class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def divisors_of number
(1..number).select { |x| number % x == 0}
end
def each
count, number = 0, 2
while count < @limit do
if divisors_of(number).length == 2 then
count += 1
yield number
end
number += 1
end
end
end
class FibonacciSequence
include Enumerable
def initialize(limit, first: 1, second: 1)
@limit = limit
@previous = first
@current = second
end
def each
count = 0
case @limit
when 1 then yield @previous
when 2 then
yield @previous
yield @current
count += 1
else yield @previous
end
count += 1
while count < @limit do
yield @current
count += 1
@current, @previous = @current + @previous, @current
end
end
end
module DrunkenMathematician
module_function
def divisors_of number
(1..number).select { |x| number % x == 0}
end
def is_prime? number
divisors_of(number).length == 2
end
def meaningless n
drunk_array = RationalSequence.new(n).to_a
first_array = [1]
second_array = [1]
second_array = drunk_array.reject{ |number| if(is_prime?((number).numerator) or
is_prime?((number).denominator)) then first_array<<number end }
(first_array.reduce { |a, b| a * b }).to_r / (second_array.reduce { |a, b| a * b }).to_r
end
def aimless n
drunk_array = PrimeSequence.new(n).to_a
wanted_array = [[0,1]]
if n.odd? then last = drunk_array.pop
wanted_array<<[last, 1]
end
drunk_array.each_slice(2) { |x| wanted_array << x }
(wanted_array.map{ |x| x[0]/x[1].to_r }).reduce { |a, b| a+b }
end
def worthless n
last_fibonacci_number = FibonacciSequence.new(n).to_a.pop
current_sum, counter = 0, 1
while current_sum <= last_fibonacci_number
current_sum = RationalSequence.new(counter).reduce{ |a, b| a + b }
counter += 1
end
RationalSequence.new(counter - 2).to_a
end
end

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

FFFF.......FFF...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)
     NameError:
       uninitialized constant RationalSequence
     # /tmp/d20151111-27349-1y02fcp/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-1y02fcp/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-1y02fcp/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-1y02fcp/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 DrunkenMathematician #meaningless can calculate for 0 and 1
     Failure/Error: expect(DrunkenMathematician.meaningless(0)).to eq 1
     NameError:
       uninitialized constant DrunkenMathematician::RationalSequence
     # /tmp/d20151111-27349-1y02fcp/solution.rb:56:in `meaningless'
     # /tmp/d20151111-27349-1y02fcp/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)>'

  6) Fifth task DrunkenMathematician #meaningless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.meaningless(4)).to eq Rational(1, 3)
     NameError:
       uninitialized constant DrunkenMathematician::RationalSequence
     # /tmp/d20151111-27349-1y02fcp/solution.rb:56:in `meaningless'
     # /tmp/d20151111-27349-1y02fcp/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)>'

  7) Fifth task DrunkenMathematician #meaningless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.meaningless(42)).to eq Rational(1, 11)
     NameError:
       uninitialized constant DrunkenMathematician::RationalSequence
     # /tmp/d20151111-27349-1y02fcp/solution.rb:56:in `meaningless'
     # /tmp/d20151111-27349-1y02fcp/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)>'

  8) 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::RationalSequence
     # /tmp/d20151111-27349-1y02fcp/solution.rb:76:in `worthless'
     # /tmp/d20151111-27349-1y02fcp/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)>'

  9) Fifth task DrunkenMathematician #worthless can calculate for 8
     Failure/Error: expect(DrunkenMathematician.worthless(8)).to eq expected
     NameError:
       uninitialized constant DrunkenMathematician::RationalSequence
     # /tmp/d20151111-27349-1y02fcp/solution.rb:76:in `worthless'
     # /tmp/d20151111-27349-1y02fcp/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)>'

  10) Fifth task DrunkenMathematician #worthless can calculate for 15
     Failure/Error: expect(DrunkenMathematician.worthless(15)).to eq %w(
     NameError:
       uninitialized constant DrunkenMathematician::RationalSequence
     # /tmp/d20151111-27349-1y02fcp/solution.rb:76:in `worthless'
     # /tmp/d20151111-27349-1y02fcp/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.02546 seconds
20 examples, 10 failures

Failed examples:

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

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

Димитър обнови решението на 26.10.2015 17:10 (преди над 8 години)

+class PrimeSequence
+ include Enumerable
+ def initialize(limit)
+ @limit = limit
+ end
+ def divisors_of number
+ (1..number).select { |x| number % x == 0}
+ end
+ def each
+ count, number = 0, 2
+ while count < @limit do
+ if divisors_of(number).length == 2 then
+ count += 1
+ yield number
+ end
+ number += 1
+ end
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+ def initialize(limit, first: 1, second: 1)
+ @limit = limit
+ @previous = first
+ @current = second
+ end
+ def each
+ count = 0
+ case @limit
+ when 1 then yield @previous
+ when 2 then
+ yield @previous
+ yield @current
+ count += 1
+ else yield @previous
+ end
+ count += 1
+ while count < @limit do
+ yield @current
+ count += 1
+ @current, @previous = @current + @previous, @current
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+ def divisors_of number
+ (1..number).select { |x| number % x == 0}
+ end
+ def is_prime? number
+ divisors_of(number).length == 2
+ end
+ def meaningless n
+ drunk_array = RationalSequence.new(n).to_a
+ first_array = [1]
+ second_array = [1]
+ second_array = drunk_array.reject{ |number| if(is_prime?((number).numerator) or
+ is_prime?((number).denominator)) then first_array<<number end }
+ (first_array.reduce { |a, b| a * b }).to_r / (second_array.reduce { |a, b| a * b }).to_r
+ end
+ def aimless n
+ drunk_array = PrimeSequence.new(n).to_a
+ wanted_array = [[0,1]]
+ if n.odd? then last = drunk_array.pop
+ wanted_array<<[last, 1]
+ end
+ drunk_array.each_slice(2) { |x| wanted_array << x }
+ (wanted_array.map{ |x| x[0]/x[1].to_r }).reduce { |a, b| a+b }
+ end
+ def worthless n
+ last_fibonacci_number = FibonacciSequence.new(n).to_a.pop
+ current_sum, counter = 0, 1
+ while current_sum <= last_fibonacci_number
+ current_sum = RationalSequence.new(counter).reduce{ |a, b| a + b }
+ counter += 1
+ end
+ RationalSequence.new(counter - 2).to_a
+ end
+end

Защото така и не можах да се сетя как да го направя така че да го кача не съм си качил вариянта на RationalSequence, имах проблем с depth-a на RationalSequence направих го по 2 начина и винаги имах поне 1 layer повече.