Решение на Трета задача от Петър Иванов

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

Към профила на Петър Иванов

Резултати

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

Код

class Integer
def is_prime?
return false if self < 2
2.upto(pred).all? { |divisor| remainder(divisor).nonzero? }
end
end
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
@arr = []
end
def get_next_rational(i, j = 1)
while j < i
if i % 2 == 0 and not @arr.include?(Rational(j, i - j))
@arr << Rational(j, i - j)
return Rational(j, i - j)
elsif i % 2 != 0 and not @arr.include?(Rational(i - j, j))
@arr << Rational(i - j, j)
return Rational(i - j, j)
end
j += 1
end
end
def each
i, counter = 2, 0
while counter < @limit
if get_next_rational(i) != nil
yield @arr[counter]
counter += 1
else
i += 1
end
end
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
counter = 0
number = 2
while counter < @limit
if number.is_prime?
yield number
counter += 1
number += 1
else
number += 1
end
end
end
end
class FibonacciSequence
include Enumerable
def initialize(limit, first: 1, second: 1)
@limit = limit
@first = first
@second = second
end
def each
current, subsequent = @first, @second
counter = 0
while counter < @limit
yield current
current, subsequent = subsequent, current + subsequent
counter += 1
end
end
end
module DrunkenMathematician
module_function
include Enumerable
def is_odd?(number)
number % 2 != 0
end
def meaningless(n)
sequence = RationalSequence.new(n).to_a
group_one = sequence.select { |i| i.numerator.is_prime? or i.denominator.is_prime? }
group_two = sequence - group_one
first = group_one.inject { |z, x| z * x }
second = group_two.inject { |z, x| z * x }
(first == nil ? 1 : first) / (second == nil ? 1 : second)
end
def aimless(n)
sequence = PrimeSequence.new(n).to_a
if is_odd?(n)
sequence << 1
end
rational_numbers = []
sequence.each_slice(2).to_a.each do |a|
rational_numbers << Rational(a[0], a[1])
end
sum_rational_numbers = rational_numbers.inject { |sum, x| sum + x }
sum_rational_numbers == nil ? 0 : sum_rational_numbers
end
def worthless(n)
n_fibonacci = FibonacciSequence.new(n).to_a.last.to_r
sequence = RationalSequence.new(n).to_a
rational_section = []
index = 0
sum_rational = 0
while sum_rational < n_fibonacci
sum_rational += sequence[index]
if sum_rational <= n_fibonacci then rational_section << sequence[index] end
index += 1
end
rational_section
end
end

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

..................FF

Failures:

  1) Fifth task DrunkenMathematician #worthless can calculate for 8
     Failure/Error: expect(DrunkenMathematician.worthless(8)).to eq expected
     TypeError:
       nil can't be coerced into Rational
     # /tmp/d20151111-27349-98ou3b/solution.rb:123:in `+'
     # /tmp/d20151111-27349-98ou3b/solution.rb:123:in `worthless'
     # /tmp/d20151111-27349-98ou3b/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)>'

  2) Fifth task DrunkenMathematician #worthless can calculate for 15
     Failure/Error: expect(DrunkenMathematician.worthless(15)).to eq %w(
     TypeError:
       nil can't be coerced into Rational
     # /tmp/d20151111-27349-98ou3b/solution.rb:123:in `+'
     # /tmp/d20151111-27349-98ou3b/solution.rb:123:in `worthless'
     # /tmp/d20151111-27349-98ou3b/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.01955 seconds
20 examples, 2 failures

Failed examples:

rspec /tmp/d20151111-27349-98ou3b/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-98ou3b/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

Петър обнови решението на 24.10.2015 17:53 (преди над 8 години)

+class Integer
+ def is_prime?
+ return false if self < 2
+ 2.upto(pred).all? { |divisor| remainder(divisor).nonzero? }
+ end
+end
+
+class RationalSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ @arr = []
+ end
+
+ def get_next_rational(i, j = 1)
+ while j < i
+ if i % 2 == 0 and not @arr.include?(Rational(j, i - j))
+ @arr << Rational(j, i - j)
+ return Rational(j, i - j)
+ elsif i % 2 != 0 and not @arr.include?(Rational(i - j, j))
+ @arr << Rational(i - j, j)
+ return Rational(i - j, j)
+ end
+ j += 1
+ end
+ end
+
+ def each
+ i, counter = 2, 0
+ while counter < @limit
+ if get_next_rational(i) != nil
+ yield @arr[counter]
+ counter += 1
+ else
+ i += 1
+ end
+ end
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ counter = 0
+ number = 2
+ while counter < @limit
+ if number.is_prime?
+ yield number
+ counter += 1
+ number += 1
+ else
+ number += 1
+ end
+ end
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(limit, first: 1, second: 1)
+ @limit = limit
+ @first = first
+ @second = second
+ end
+
+ def each
+ current, subsequent = @first, @second
+ counter = 0
+ while counter < @limit
+ yield current
+ current, subsequent = subsequent, current + subsequent
+ counter += 1
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+ include Enumerable
+
+ def is_odd?(number)
+ number % 2 != 0
+ end
+
+ def meaningless(n)
+ sequence = RationalSequence.new(n).to_a
+ group_one = sequence.select { |i| i.numerator.is_prime? or i.denominator.is_prime? }
+ group_two = sequence - group_one
+
+ first = group_one.inject { |z, x| z * x }
+ second = group_two.inject { |z, x| z * x }
+
+ (first == nil ? 1 : first) / (second == nil ? 1 : second)
+ end
+
+ def aimless(n)
+ sequence = PrimeSequence.new(n).to_a
+ if is_odd?(n)
+ sequence << 1
+ end
+ rational_numbers = []
+ sequence.each_slice(2).to_a.each do |a|
+ rational_numbers << Rational(a[0], a[1])
+ end
+ sum_rational_numbers = rational_numbers.inject { |sum, x| sum + x }
+ sum_rational_numbers == nil ? 0 : sum_rational_numbers
+ end
+
+ def worthless(n)
+ n_fibonacci = FibonacciSequence.new(n).to_a.last.to_r
+ sequence = RationalSequence.new(n).to_a
+ rational_section = []
+ index = 0
+ sum_rational = 0
+ while sum_rational < n_fibonacci
+ sum_rational += sequence[index]
+ if sum_rational <= n_fibonacci then rational_section << sequence[index] end
+ index += 1
+ end
+ rational_section
+ end
+end