Решение на Трета задача от Алекс Николов

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

Към профила на Алекс Николов

Резултати

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

Код

class Numeric
def prime?
(1..self / 2).one? { |remainder| self % remainder == 0 }
end
end
module DrunkenMathematician
module_function
def meaningless(n)
parts = RationalSequence.new(n).partition { |x| x.numerator.prime? or x.denominator.prime? }
parts[0].reduce(1, :*) / parts[1].reduce(1, :*)
end
def aimless(n)
prime_pairs = PrimeSequence.new(n).each_slice(2).to_a
if n % 2 == 1
prime_pairs[n / 2].push(1)
end
prime_pairs.collect { |pair| Rational(pair[0], pair[1]) }.reduce(0, :+)
end
def worthless(n)
fibonacci_n, sum = FibonacciSequence.new(n).to_a.last, 0
sequence = RationalSequence.new((1.5**n).floor)
sequence.take_while do |x|
sum += x
sum <= fibonacci_n
end
end
end
class RationalSequence
include Enumerable
def initialize(length)
@length = length
end
def get_next(rational_array)
if rational_array[0] == 1 and rational_array[1].even?
[rational_array[0], rational_array[1] + 1]
elsif rational_array[1] == 1 and rational_array[0].odd?
[rational_array[0] + 1, rational_array[1]]
elsif (rational_array[0] + rational_array[1]).odd?
[rational_array[0] - 1, rational_array[1] + 1]
elsif (rational_array[0] + rational_array[1]).even?
[rational_array[0] + 1, rational_array[1] - 1]
end
end
def last
self.to_a.last
end
def each
rational_array, current = [1, 0], 0
while current < @length
rational_array = get_next(rational_array)
if (rational_array[0].gcd(rational_array[1]) == 1)
yield Rational(rational_array.first, rational_array.last)
current += 1
end
end
end
end
class PrimeSequence
include Enumerable
def initialize(length)
@length = length
end
def each
if @length > 0
yield 2
end
current, element_number = 3, 1
while element_number < @length
if current.prime?
yield current
element_number += 1
end
current += 2
end
end
end
class FibonacciSequence
include Enumerable
def initialize(length, first: 1, second: 1)
@length, @first, @second = length, first, second
end
def each
element_number, current, previous = 0, @first, @second - @first
while element_number < @length
yield current
element_number, current, previous = element_number + 1, current + previous, current
end
end
end

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

....................

Finished in 0.01272 seconds
20 examples, 0 failures

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

Алекс обнови решението на 22.10.2015 20:31 (преди над 8 години)

+class Numeric
+ def prime?
+ not (2..self / 2).any? { |remainder| self % remainder == 0 }
+ end
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ first_group, second_group = [], []
+ RationalSequence.new(n).each do |number|
+ if number.numerator.prime? or number.denominator.prime?
+ first_group << number
+ else
+ second_group << number
+ end
+ end
+ (first_group.reduce(1, :*) / second_group.reduce(1, :*)).to_r
+ end
+
+ def aimless(n)
+ prime_pairs = PrimeSequence.new(n).each_slice(2).to_a
+ if n % 2 == 1
+ prime_pairs[n / 2].push(1)
+ end
+ rational_prime_pairs = prime_pairs.collect { |pair| Rational(pair[0], pair[1]) }
+ rational_prime_pairs.reduce(0, :+)
+ end
+
+ def worthless(n)
+ fibonacci_n = FibonacciSequence.new(n).to_a.last
+ biggest_section = Array.new
+ current = 0
+ while fibonacci_n >= (biggest_section.reduce(0, :+) + RationalSequence.new(current + 1).last)
+ biggest_section << RationalSequence.new(current + 1).last
+ current += 1
+ end
+ biggest_section
+ end
+end
+
+class RationalSequence
+ include Enumerable
+
+ def initialize(length)
+ @length = length
+ end
+
+ def get_next(rational_array)
+ if rational_array[0] == 1 and rational_array[1].even?
+ [rational_array[0], rational_array[1] + 1]
+ elsif rational_array[1] == 1 and rational_array[0].odd?
+ [rational_array[0] + 1, rational_array[1]]
+ elsif (rational_array[0] + rational_array[1]).odd?
+ [rational_array[0] - 1, rational_array[1] + 1]
+ elsif (rational_array[0] + rational_array[1]).even?
+ [rational_array[0] + 1, rational_array[1] - 1]
+ end
+ end
+
+ def last
+ self.to_a.last
+ end
+
+ def each
+ current = 0
+ rational_array = [1, 0]
+ while current < @length
+ rational_array = get_next(rational_array)
+ if (rational_array[0].gcd(rational_array[1]) == 1)
+ yield Rational(rational_array.first, rational_array.last)
+ current += 1
+ end
+ end
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(length)
+ @length = length
+ end
+
+ def each
+ if @length > 0
+ yield 2
+ end
+ current, element_number = 3, 1
+ while element_number < @length
+ if current.prime?
+ yield current
+ element_number += 1
+ end
+ current += 2
+ end
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(length, first: 1, second: 1)
+ @length = length
+ @first = first
+ @second = second
+ end
+
+ def each
+ element_number, current, previous = 0, @first, @second - @first
+ while element_number < @length
+ yield current
+ element_number, current, previous = element_number + 1, current + previous, current
+ end
+ end
+end

Алекс обнови решението на 22.10.2015 22:18 (преди над 8 години)

class Numeric
def prime?
- not (2..self / 2).any? { |remainder| self % remainder == 0 }
+ if self == 1
+ false
+ else
+ not (2..self / 2).any? { |remainder| self % remainder == 0 }
+ end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
first_group, second_group = [], []
RationalSequence.new(n).each do |number|
if number.numerator.prime? or number.denominator.prime?
first_group << number
else
second_group << number
end
end
- (first_group.reduce(1, :*) / second_group.reduce(1, :*)).to_r
+ first_group.reduce(1, :*) / second_group.reduce(1, :*)
end
def aimless(n)
prime_pairs = PrimeSequence.new(n).each_slice(2).to_a
if n % 2 == 1
prime_pairs[n / 2].push(1)
end
rational_prime_pairs = prime_pairs.collect { |pair| Rational(pair[0], pair[1]) }
rational_prime_pairs.reduce(0, :+)
end
def worthless(n)
- fibonacci_n = FibonacciSequence.new(n).to_a.last
- biggest_section = Array.new
- current = 0
+ fibonacci_n, biggest_section, current = FibonacciSequence.new(n).to_a.last, [], 0
while fibonacci_n >= (biggest_section.reduce(0, :+) + RationalSequence.new(current + 1).last)
biggest_section << RationalSequence.new(current + 1).last
current += 1
end
biggest_section
end
end
class RationalSequence
include Enumerable
def initialize(length)
@length = length
end
def get_next(rational_array)
if rational_array[0] == 1 and rational_array[1].even?
[rational_array[0], rational_array[1] + 1]
elsif rational_array[1] == 1 and rational_array[0].odd?
[rational_array[0] + 1, rational_array[1]]
elsif (rational_array[0] + rational_array[1]).odd?
[rational_array[0] - 1, rational_array[1] + 1]
elsif (rational_array[0] + rational_array[1]).even?
[rational_array[0] + 1, rational_array[1] - 1]
end
end
def last
self.to_a.last
end
def each
- current = 0
- rational_array = [1, 0]
+ rational_array, current = [1, 0], 0
while current < @length
rational_array = get_next(rational_array)
if (rational_array[0].gcd(rational_array[1]) == 1)
yield Rational(rational_array.first, rational_array.last)
current += 1
end
end
end
end
class PrimeSequence
include Enumerable
def initialize(length)
@length = length
end
def each
if @length > 0
yield 2
end
current, element_number = 3, 1
while element_number < @length
if current.prime?
yield current
element_number += 1
end
current += 2
end
end
end
class FibonacciSequence
include Enumerable
def initialize(length, first: 1, second: 1)
@length = length
@first = first
@second = second
end
def each
element_number, current, previous = 0, @first, @second - @first
while element_number < @length
yield current
element_number, current, previous = element_number + 1, current + previous, current
end
end
end

Алекс обнови решението на 24.10.2015 17:20 (преди над 8 години)

class Numeric
def prime?
- if self == 1
- false
- else
- not (2..self / 2).any? { |remainder| self % remainder == 0 }
- end
+ (1..self / 2).one? { |remainder| self % remainder == 0 }
end
end
module DrunkenMathematician
module_function
def meaningless(n)
- first_group, second_group = [], []
- RationalSequence.new(n).each do |number|
- if number.numerator.prime? or number.denominator.prime?
- first_group << number
- else
- second_group << number
- end
- end
- first_group.reduce(1, :*) / second_group.reduce(1, :*)
+ parts = RationalSequence.new(n).partition { |x| x.numerator.prime? or x.denominator.prime? }
+ parts[0].reduce(1, :*) / parts[1].reduce(1, :*)
end
def aimless(n)
prime_pairs = PrimeSequence.new(n).each_slice(2).to_a
if n % 2 == 1
prime_pairs[n / 2].push(1)
end
- rational_prime_pairs = prime_pairs.collect { |pair| Rational(pair[0], pair[1]) }
- rational_prime_pairs.reduce(0, :+)
+ prime_pairs.collect { |pair| Rational(pair[0], pair[1]) }.reduce(0, :+)
end
def worthless(n)
- fibonacci_n, biggest_section, current = FibonacciSequence.new(n).to_a.last, [], 0
- while fibonacci_n >= (biggest_section.reduce(0, :+) + RationalSequence.new(current + 1).last)
- biggest_section << RationalSequence.new(current + 1).last
- current += 1
+ fibonacci_n, sum = FibonacciSequence.new(n).to_a.last, 0
+ sequence = RationalSequence.new((1.5**n).floor)
+ sequence.take_while do |x|
+ sum += x
+ sum <= fibonacci_n
end
- biggest_section
end
end
class RationalSequence
include Enumerable
def initialize(length)
@length = length
end
def get_next(rational_array)
if rational_array[0] == 1 and rational_array[1].even?
[rational_array[0], rational_array[1] + 1]
elsif rational_array[1] == 1 and rational_array[0].odd?
[rational_array[0] + 1, rational_array[1]]
elsif (rational_array[0] + rational_array[1]).odd?
[rational_array[0] - 1, rational_array[1] + 1]
elsif (rational_array[0] + rational_array[1]).even?
[rational_array[0] + 1, rational_array[1] - 1]
end
end
def last
self.to_a.last
end
def each
rational_array, current = [1, 0], 0
while current < @length
rational_array = get_next(rational_array)
if (rational_array[0].gcd(rational_array[1]) == 1)
yield Rational(rational_array.first, rational_array.last)
current += 1
end
end
end
end
class PrimeSequence
include Enumerable
def initialize(length)
@length = length
end
def each
if @length > 0
yield 2
end
current, element_number = 3, 1
while element_number < @length
if current.prime?
yield current
element_number += 1
end
current += 2
end
end
end
class FibonacciSequence
include Enumerable
def initialize(length, first: 1, second: 1)
- @length = length
- @first = first
- @second = second
+ @length, @first, @second = length, first, second
end
def each
element_number, current, previous = 0, @first, @second - @first
while element_number < @length
yield current
element_number, current, previous = element_number + 1, current + previous, current
end
end
end