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

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

Към профила на Андрея Костов

Резултати

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

Код

class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
count = 0
rational = Rational(1, 1)
numbers = [rational]
while count < @limit
yield rational
rational = next_r(rational, numbers)
count = count.next
end
end
private
def next_r(rational, numbers)
numerator = rational.numerator
denominator = rational.denominator
if (numerator % 2 == denominator % 2)
numerator = numerator.next
denominator = denominator.pred if (denominator > 1)
else
denominator = denominator.next
numerator = numerator.pred if (numerator > 1)
end
r = Rational(numerator, denominator)
numbers << r
r
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
count, prime = 0, 0
while count < @limit
prime = next_p(prime)
yield prime
count = count.next
end
end
private
def next_p(current)
next_p = current.next
while not next_p.prime?
next_p = next_p.next
end
next_p
end
end
class FibonacciSequence
include Enumerable
def initialize(limit, first: 1, second: 1)
@limit = limit
@first = first
@second = second
end
def each
current, previous = @second, @first
case @limit
when 0
return
when 1
yield @first
else
count = 2
yield @first
yield @second
while count < @limit
current, previous = current + previous, current
yield current
count = count.next
end
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
return 0 if n == 0
rational = RationalSequence.new(n)
first_group = rational.select { |r| r.numerator.prime? || r.denominator.prime? }
.inject { |m, r| m * r }
second_group = rational.select { |r| !r.numerator.prime? && !r.denominator.prime? }
.inject { |m, r| m * r }
if (first_group == nil)
second_group
else
first_group / second_group
end
end
def aimless(n)
primes = PrimeSequence.new(n).to_a
primes << 1 if (n % 2 != 0 && n != 1)
count = 0
rationals = []
while (count < n)
rationals << Rational(primes[count], primes[count.next])
count += 2
end
rationals.inject(0) { |sum, r| sum += r }
end
def worthless(n)
fibonacci = FibonacciSequence.new(n).to_a.last
count, sum = 0, 0
while sum < fibonacci
sum = RationalSequence.new(count).inject(0) { |sum, r| sum += r }
count = count.next
end
sum == fibonacci ? count -= 1 : count -= 2
RationalSequence.new(count).to_a
end
end
class Integer
def prime?
return false if self <= 1
root = sqrt(self)
(2.. root).none? { |divisor| self % divisor == 0 }
end
end

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

..FF.....FFFFFFFF.FF

Failures:

  1) 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), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3)]
       
       (compared using ==)
     # /tmp/d20151111-27349-1nlgrco/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)>'

  2) Fifth task RationalSequence is properly enumerable
     Failure/Error: expect(ones).to eq %w(1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9).map(&:to_r)
       
       expected: [(1/1), (1/2), (1/3), (1/4), (1/5), (1/6), (1/7), (1/8), (1/9)]
            got: [(1/1), (1/2), (1/3), (1/1), (1/2), (1/3), (1/1), (1/2), (1/3), (1/1), (1/2), (1/3), (1/1), (1/2), (1/3), (1/1), (1/2), (1/3), (1/1), (1/2), (1/3)]
       
       (compared using ==)
     # /tmp/d20151111-27349-1nlgrco/spec.rb:20: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 PrimeSequence can tell which the first two primes are
     Failure/Error: expect(PrimeSequence.new(2).to_a).to eq [2, 3]
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:60:in `next_p'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:50:in `each'
     # /tmp/d20151111-27349-1nlgrco/spec.rb:57:in `to_a'
     # /tmp/d20151111-27349-1nlgrco/spec.rb:57: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 PrimeSequence can tell the first 58 primes
     Failure/Error: expect(PrimeSequence.new(58).to_a).to eq [
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:60:in `next_p'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:50:in `each'
     # /tmp/d20151111-27349-1nlgrco/spec.rb:61:in `to_a'
     # /tmp/d20151111-27349-1nlgrco/spec.rb:61: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
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20151111-27349-1nlgrco/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)
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:105:in `block in meaningless'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:14:in `each'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:105:in `select'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:105:in `meaningless'
     # /tmp/d20151111-27349-1nlgrco/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)
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:105:in `block in meaningless'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:14:in `each'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:105:in `select'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:105:in `meaningless'
     # /tmp/d20151111-27349-1nlgrco/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 #aimless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.aimless(3)).to eq(Rational(2, 3) + Rational(5, 1))
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:60:in `next_p'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:50:in `each'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:118:in `to_a'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:118:in `aimless'
     # /tmp/d20151111-27349-1nlgrco/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)>'

  9) Fifth task DrunkenMathematician #aimless can calculate for 4
     Failure/Error: expect(DrunkenMathematician.aimless(4)).to eq(Rational(2, 3) + Rational(5, 7))
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:60:in `next_p'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:50:in `each'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:118:in `to_a'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:118:in `aimless'
     # /tmp/d20151111-27349-1nlgrco/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)>'

  10) Fifth task DrunkenMathematician #aimless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.aimless(42)).to eq expected
     NoMethodError:
       undefined method `sqrt' for 2:Fixnum
     # /tmp/d20151111-27349-1nlgrco/solution.rb:149:in `prime?'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:60:in `next_p'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:50:in `each'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:118:in `to_a'
     # /tmp/d20151111-27349-1nlgrco/solution.rb:118:in `aimless'
     # /tmp/d20151111-27349-1nlgrco/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)>'

  11) 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), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1)]
       
       (compared using ==)
     # /tmp/d20151111-27349-1nlgrco/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)>'

  12) 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), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3), (1/1), (2/1), (1/2), (1/3)]
       
       (compared using ==)
     # /tmp/d20151111-27349-1nlgrco/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.60877 seconds
20 examples, 12 failures

Failed examples:

rspec /tmp/d20151111-27349-1nlgrco/spec.rb:11 # Fifth task RationalSequence can calculate the first 28 rational numbers
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:18 # Fifth task RationalSequence is properly enumerable
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:56 # Fifth task PrimeSequence can tell which the first two primes are
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:60 # Fifth task PrimeSequence can tell the first 58 primes
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:77 # Fifth task DrunkenMathematician #meaningless can calculate for 3
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:81 # Fifth task DrunkenMathematician #meaningless can calculate for 42
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:87 # Fifth task DrunkenMathematician #aimless can calculate for 3
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:91 # Fifth task DrunkenMathematician #aimless can calculate for 4
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:95 # Fifth task DrunkenMathematician #aimless can calculate for 42
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-1nlgrco/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

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

+class RationalSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ count = 0
+ rational = Rational(1, 1)
+ numbers = [rational]
+
+ while count < @limit
+ yield rational
+ rational = next_r(rational, numbers)
+ count = count.next
+ end
+ end
+
+ private
+
+ def next_r(rational, numbers)
+ numerator = rational.numerator
+ denominator = rational.denominator
+ if (numerator % 2 == denominator % 2)
+ numerator = numerator.next
+ denominator = denominator.pred if (denominator > 1)
+ else
+ denominator = denominator.next
+ numerator = numerator.pred if (numerator > 1)
+ end
+ r = Rational(numerator, denominator)
+ numbers << r
+ r
+ end
+
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ count, prime = 0, 0
+
+ while count < @limit
+ prime = next_p(prime)
+ yield prime
+ count = count.next
+ end
+ end
+
+ private
+
+ def next_p(current)
+ next_p = current.next
+ while not next_p.prime?
+ next_p = next_p.next
+ end
+ next_p
+ end
+
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(limit, first: 1, second: 1)
+ @limit = limit
+ @first = first
+ @second = second
+ end
+
+ def each
+ current, previous = @second, @first
+
+ case @limit
+ when 0
+ return
+ when 1
+ yield @first
+ else
+ count = 2
+ yield @first
+ yield @second
+ while count < @limit
+ current, previous = current + previous, current
+ yield current
+ count = count.next
+ end
+ end
+ end
+
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ return 0 if n == 0
+ rational = RationalSequence.new(n)
+ first_group = rational.select { |r| r.numerator.prime? || r.denominator.prime? }
+ .inject { |m, r| m * r }
+ second_group = rational.select { |r| !r.numerator.prime? && !r.denominator.prime? }
+ .inject { |m, r| m * r }
+
+ if (first_group == nil)
+ second_group
+ else
+ first_group / second_group
+ end
+ end
+
+ def aimless(n)
+ primes = PrimeSequence.new(n).to_a
+ primes << 1 if (n % 2 != 0 && n != 1)
+
+ count = 0
+ rationals = []
+ while (count < n)
+ rationals << Rational(primes[count], primes[count.next])
+ count += 2
+ end
+
+ rationals.inject(0) { |sum, r| sum += r }
+ end
+
+ def worthless(n)
+ fibonacci = FibonacciSequence.new(n).to_a.last
+ count, sum = 0, 0
+
+ while sum < fibonacci
+ sum = RationalSequence.new(count).inject(0) { |sum, r| sum += r }
+ count = count.next
+ end
+
+ sum == fibonacci ? count -= 1 : count -= 2
+ RationalSequence.new(count).to_a
+ end
+
+end
+
+class Integer
+ def prime?
+ return false if self <= 1
+ root = sqrt(self)
+ (2.. root).none? { |divisor| self % divisor == 0 }
+ end
+end