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

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

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

Резултати

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

Код

class PrimeSequence
include Enumerable
attr_reader :limit
def initialize(limit)
@limit = limit
end
def to_a
enum_for(:each_number).lazy.select{ |x| prime?(x) }.take(@limit).to_a
end
private
def each_number
n = 0
loop do
n += 1
yield n
end
end
def prime?(n)
return false if n < 2
(2..n/2).none? {|i| n % i == 0}
end
end
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
@k = []
@s = collect_numbers
end
def to_a
@k.take(@limit)
end
def collect_numbers
n = Rational(1)
@k << n
while @k.size < @limit
n = denominator(n) if n.denominator == 1
n = numerator(n) if n.numerator == 1
end
end
def numerator(n)
n = Rational(n.numerator, n.denominator + 1)
@k << n
target = n.denominator
while n.numerator < target
n = next_rational_numerator(n.numerator, n.denominator)
@k << n
end
n
end
def denominator(n)
n = Rational(n.numerator + 1, n.denominator)
@k << n
target = n.numerator
while n.denominator < target
n = next_rational_denominator(n.numerator, n.denominator)
@k << n
end
n
end
def next_rational_denominator(n_numerator, n_denominator)
i = 1
while @k.include?(Rational(n_numerator-i, n_denominator+i))
i += 1
end
Rational(n_numerator-i, n_denominator+i)
end
def next_rational_numerator(n_numerator, n_denominator)
i = 1
while @k.include?(Rational(n_numerator+i, n_denominator-i))
i += 1
end
Rational(n_numerator+i, n_denominator-i)
end
end
class FibonacciSequence
include Enumerable
attr_reader :limit
def initialize(limit, initial_values= {first: 1, second: 1})
@limit = limit
@initial_values = initial_values
end
def to_a
enum_for(:each_number).lazy.take(@limit).to_a
end
private
def each_number
b, a = @initial_values[:first], @initial_values[:second]
loop do
yield b
a, b = a + b, a
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
first_n = RationalSequence.new(n).to_a
second_group = second_group(first_n)
first_group = first_n - second_group
product(first_group) / product(second_group)
end
def aimless(n)
first_n = PrimeSequence.new(n).to_a
array_rationals = make_pairs(first_n).map {|x| Rational(x[0], x[1])}
sum_rationals(array_rationals)
end
def worthless(n)
fibonacci_n = FibonacciSequence.new(n).to_a.last
first_n_rationals = RationalSequence.new(n).to_a
biggest_slice(fibonacci_n, first_n_rationals)
end
def sum_rationals(rationals)
rationals.reduce{|sum, x| sum + x}
end
def biggest_slice(number, rationals)
while sum_rationals(rationals) > number
rationals = rationals - [rationals.last]
end
rationals
end
def split(first_n, equal_to)
s = []
first_n.each_index {|x| s << first_n[x] if x % 2 == equal_to }
s
end
def make_pairs(first_n)
first_group = split(first_n, 0)
second_group = split(first_n, 1)
first_group.zip(second_group)
end
def product(group)
group.reduce{|sum, x| sum * x}
end
def second_group(first_n)
first_n.select { |x| !prime?(x.numerator) && !prime?(x.denominator) }
end
def prime?(n)
return false if n < 2
(2..n/2).none? {|i| n % i == 0}
end
end

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

...F...F...F..F...FF

Failures:

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

  2) Fifth task FibonacciSequence is properly enumerable
     Failure/Error: expect(FibonacciSequence.new(20).select { |x| x.even? }).to eq [
     NoMethodError:
       undefined method `each' for #<FibonacciSequence:0x007f3477e47250>
     # /tmp/d20151111-27349-1hdjtf5/spec.rb:45:in `select'
     # /tmp/d20151111-27349-1hdjtf5/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)>'

  3) Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
     Failure/Error: expect(DrunkenMathematician.meaningless(0)).to eq 1
     NoMethodError:
       undefined method `/' for nil:NilClass
     # /tmp/d20151111-27349-1hdjtf5/solution.rb:123:in `meaningless'
     # /tmp/d20151111-27349-1hdjtf5/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)>'

  4) Fifth task DrunkenMathematician #aimless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.aimless(3)).to eq(Rational(2, 3) + Rational(5, 1))
     TypeError:
       can't convert nil into Rational
     # /tmp/d20151111-27349-1hdjtf5/solution.rb:128:in `convert'
     # /tmp/d20151111-27349-1hdjtf5/solution.rb:128:in `Rational'
     # /tmp/d20151111-27349-1hdjtf5/solution.rb:128:in `block in aimless'
     # /tmp/d20151111-27349-1hdjtf5/solution.rb:128:in `map'
     # /tmp/d20151111-27349-1hdjtf5/solution.rb:128:in `aimless'
     # /tmp/d20151111-27349-1hdjtf5/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)>'

  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-1hdjtf5/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-1hdjtf5/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.01514 seconds
20 examples, 6 failures

Failed examples:

rspec /tmp/d20151111-27349-1hdjtf5/spec.rb:18 # Fifth task RationalSequence is properly enumerable
rspec /tmp/d20151111-27349-1hdjtf5/spec.rb:44 # Fifth task FibonacciSequence is properly enumerable
rspec /tmp/d20151111-27349-1hdjtf5/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-1hdjtf5/spec.rb:87 # Fifth task DrunkenMathematician #aimless can calculate for 3
rspec /tmp/d20151111-27349-1hdjtf5/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-1hdjtf5/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

Даяна обнови решението на 26.10.2015 11:31 (преди над 8 години)

+class PrimeSequence
+ include Enumerable
+
+ attr_reader :limit
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def to_a
+ enum_for(:each_number).lazy.select{ |x| prime?(x) }.take(@limit).to_a
+ end
+
+ private
+
+ def each_number
+ n = 0
+ loop do
+ n += 1
+ yield n
+ end
+ end
+
+ def prime?(n)
+ return false if n < 2
+ (2..n/2).none? {|i| n % i == 0}
+ end
+end
+
+class RationalSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ @k = []
+ @s = collect_numbers
+ end
+
+ def to_a
+ @k.take(@limit)
+ end
+
+ def collect_numbers
+ n = Rational(1)
+ @k << n
+ while @k.size < @limit
+ n = denominator(n) if n.denominator == 1
+ n = numerator(n) if n.numerator == 1
+ end
+ end
+
+ def numerator(n)
+ n = Rational(n.numerator, n.denominator + 1)
+ @k << n
+ target = n.denominator
+ while n.numerator < target
+ n = next_rational_numerator(n.numerator, n.denominator)
+ @k << n
+ end
+ n
+ end
+
+ def denominator(n)
+ n = Rational(n.numerator + 1, n.denominator)
+ @k << n
+ target = n.numerator
+ while n.denominator < target
+ n = next_rational_denominator(n.numerator, n.denominator)
+ @k << n
+ end
+ n
+ end
+
+ def next_rational_denominator(n_numerator, n_denominator)
+ i = 1
+ while @k.include?(Rational(n_numerator-i, n_denominator+i))
+ i += 1
+ end
+ Rational(n_numerator-i, n_denominator+i)
+ end
+
+ def next_rational_numerator(n_numerator, n_denominator)
+ i = 1
+ while @k.include?(Rational(n_numerator+i, n_denominator-i))
+ i += 1
+ end
+ Rational(n_numerator+i, n_denominator-i)
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ attr_reader :limit
+
+ def initialize(limit, initial_values= {first: 1, second: 1})
+ @limit = limit
+ @initial_values = initial_values
+ end
+
+ def to_a
+ enum_for(:each_number).lazy.take(@limit).to_a
+ end
+
+ private
+
+ def each_number
+ b, a = @initial_values[:first], @initial_values[:second]
+ loop do
+ yield b
+ a, b = a + b, a
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ first_n = RationalSequence.new(n).to_a
+ second_group = second_group(first_n)
+ first_group = first_n - second_group
+ product(first_group) / product(second_group)
+ end
+
+ def aimless(n)
+ first_n = PrimeSequence.new(n).to_a
+ array_rationals = make_pairs(first_n).map {|x| Rational(x[0], x[1])}
+ sum_rationals(array_rationals)
+ end
+
+ def worthless(n)
+ fibonacci_n = FibonacciSequence.new(n).to_a.last
+ first_n_rationals = RationalSequence.new(n).to_a
+ biggest_slice(fibonacci_n, first_n_rationals)
+ end
+
+ def sum_rationals(rationals)
+ rationals.reduce{|sum, x| sum + x}
+ end
+
+ def biggest_slice(number, rationals)
+ while sum_rationals(rationals) > number
+ rationals = rationals - [rationals.last]
+ end
+ rationals
+ end
+
+ def split(first_n, equal_to)
+ s = []
+ first_n.each_index {|x| s << first_n[x] if x % 2 == equal_to }
+ s
+ end
+
+ def make_pairs(first_n)
+ first_group = split(first_n, 0)
+ second_group = split(first_n, 1)
+ first_group.zip(second_group)
+ end
+
+ def product(group)
+ group.reduce{|sum, x| sum * x}
+ end
+
+ def second_group(first_n)
+ first_n.select { |x| !prime?(x.numerator) && !prime?(x.denominator) }
+ end
+
+ def prime?(n)
+ return false if n < 2
+ (2..n/2).none? {|i| n % i == 0}
+ end
+end