Решение на Трета задача от Стоян Желязков

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

Към профила на Стоян Желязков

Резултати

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

Код

class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
rationals_found = []
max_value = 1
while rationals_found.length < @limit
numerators, denominators = create_parts(max_value)
sequence = generate_sequence(numerators, denominators)
process_sequence(sequence, rationals_found, @limit).each do |rational_number|
yield (rationals_found.push rational_number).last
end
max_value += 1
end
end
def create_parts(max_value)
if max_value.odd?
numerators = (1..max_value).to_a
denominators = numerators.reverse
else
denominators = (1..max_value).to_a
numerators = denominators.reverse
end
return numerators, denominators
end
def generate_sequence(numerators, denominators)
rationals = []
index = 0
numerators.length.times do
rationals.push Rational(numerators[index], denominators[index])
index += 1
end
rationals
end
def process_sequence(sequence, found, maximum)
(sequence - found).take(maximum - found.length)
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
primes_found, current = 0, 1
while primes_found < @limit
if (PrimeSequence.is_prime? current)
yield current
primes_found += 1
end
current += 1
end
end
def self.is_prime?(n)
i = 2
while i < n
if (n % i) == 0
false
end
i += 1
end
n != 1
end
end
class FibonacciSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
current, previous = 1, 0
@limit.times do
yield current
current, previous = current + previous, current
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
group_one, group_two = [], []
RationalSequence.new(n).to_a.each do |rational_number|
if PrimeSequence.is_prime? rational_number.numerator or
PrimeSequence.is_prime? rational_number.denominator
group_one.push rational_number
else
group_two.push rational_number
end
end
numerator = [(group_one.reduce { |a, b| a * b}).to_i, 1].max
denominator = [(group_two.reduce { |a, b| a * b}).to_i, 1].max
Rational(numerator, denominator)
end
def aimless(n)
sum = 0
PrimeSequence.new(n).to_a.each_slice(2) do |slice|
if (slice.length.odd?)
slice.push 1
end
sum += Rational(slice.first, slice.last)
end
sum
end
def worthless(n)
fibonacci = FibonacciSequence.new(n).to_a.last
largest_range = RationalSequence.new(n).to_a
while largest_range.reduce { |a, b| a + b } > fibonacci do
n -= 1
largest_range = RationalSequence.new(n).to_a
end
largest_range
end
end

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

......F...F.FFFFF.FF

Failures:

  1) Fifth task FibonacciSequence can be used to calculate the Lucas numbers
     Failure/Error: expect(FibonacciSequence.new(31, first: 2, second: 1).to_a).to eq [
     ArgumentError:
       wrong number of arguments (2 for 1)
     # /tmp/d20151111-27349-1ts8ulr/solution.rb:85:in `initialize'
     # /tmp/d20151111-27349-1ts8ulr/spec.rb:37:in `new'
     # /tmp/d20151111-27349-1ts8ulr/spec.rb:37: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 PrimeSequence can tell the first 58 primes
     Failure/Error: expect(PrimeSequence.new(58).to_a).to eq [
       
       expected: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271]
            got: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
       
       (compared using ==)
     # /tmp/d20151111-27349-1ts8ulr/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)>'

  3) Fifth task DrunkenMathematician #meaningless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.meaningless(4)).to eq Rational(1, 3)
       
       expected: (1/3)
            got: (1/1)
       
       (compared using ==)
     # /tmp/d20151111-27349-1ts8ulr/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)>'

  4) Fifth task DrunkenMathematician #meaningless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.meaningless(42)).to eq Rational(1, 11)
       
       expected: (1/11)
            got: (1/1)
       
       (compared using ==)
     # /tmp/d20151111-27349-1ts8ulr/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)>'

  5) Fifth task DrunkenMathematician #aimless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.aimless(3)).to eq(Rational(2, 3) + Rational(5, 1))
       
       expected: (17/3)
            got: (14/3)
       
       (compared using ==)
     # /tmp/d20151111-27349-1ts8ulr/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)>'

  6) Fifth task DrunkenMathematician #aimless can calculate for 4
     Failure/Error: expect(DrunkenMathematician.aimless(4)).to eq(Rational(2, 3) + Rational(5, 7))
       
       expected: (29/21)
            got: (22/15)
       
       (compared using ==)
     # /tmp/d20151111-27349-1ts8ulr/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)>'

  7) Fifth task DrunkenMathematician #aimless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.aimless(42)).to eq expected
       
       expected: (126481765191558862062699751684617707800/6619489496139348390798112786167608259)
            got: (5732019776287831438/294362129962575675)
       
       (compared using ==)
     # /tmp/d20151111-27349-1ts8ulr/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)>'

  8) 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-1ts8ulr/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)>'

  9) 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-1ts8ulr/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.01746 seconds
20 examples, 9 failures

Failed examples:

rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:36 # Fifth task FibonacciSequence can be used to calculate the Lucas numbers
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:60 # Fifth task PrimeSequence can tell the first 58 primes
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:77 # Fifth task DrunkenMathematician #meaningless can calculate for 3
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:81 # Fifth task DrunkenMathematician #meaningless can calculate for 42
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:87 # Fifth task DrunkenMathematician #aimless can calculate for 3
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:91 # Fifth task DrunkenMathematician #aimless can calculate for 4
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:95 # Fifth task DrunkenMathematician #aimless can calculate for 42
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-1ts8ulr/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

Стоян обнови решението на 21.10.2015 21:37 (преди над 8 години)

+class RationalSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ rationals_found = []
+ max_value = 1
+
+ while rationals_found.length < @limit
+ numerators, denominators = create_parts(max_value)
+ sequence = generate_sequence(numerators, denominators)
+ process_sequence(sequence, rationals_found, @limit).each do |rational_number|
+ yield (rationals_found.push rational_number).last
+ end
+ max_value += 1
+ end
+ end
+
+ def create_parts(max_value)
+ if max_value.odd?
+ numerators = (1..max_value).to_a
+ denominators = numerators.reverse
+ else
+ denominators = (1..max_value).to_a
+ numerators = denominators.reverse
+ end
+
+ return numerators, denominators
+ end
+
+ def generate_sequence(numerators, denominators)
+ rationals = []
+ index = 0
+ numerators.length.times do
+ rationals.push Rational(numerators[index], denominators[index])
+ index += 1
+ end
+ rationals
+ end
+
+ def process_sequence(sequence, found, maximum)
+ (sequence - found).take(maximum - found.length)
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ primes_found, current = 0, 1
+
+ while primes_found < @limit
+ if (PrimeSequence.is_prime? current)
+ yield current
+ primes_found += 1
+ end
+ current += 1
+ end
+ end
+
+ def self.is_prime?(n)
+ i = 2
+ while i < n
+ if (n % i) == 0
+ return false
+ end
+
+ i += 1
+ end
+
+ n != 1
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ current, previous = 1, 1
+
+ @limit.times do
+ yield current
+ current, previous = current + previous, current
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ group_one, group_two = [], []
+ RationalSequence.new(n).to_a.each do |rational_number|
+ if PrimeSequence.is_prime? rational_number.numerator or
+ PrimeSequence.is_prime? rational_number.denominator
+ group_one.push rational_number
+ else
+ group_two.push rational_number
+ end
+ end
+
+ numerator = [(group_one.reduce { |a, b| a * b}).to_i, 1].max
+ denominator = [(group_two.reduce { |a, b| a * b}).to_i, 1].max
+ Rational(numerator, denominator)
+ end
+
+ def aimless(n)
+ sum = 0
+ PrimeSequence.new(n).to_a.each_slice(2) do |slice|
+ if (slice.length.odd?)
+ slice.push 1
+ end
+
+ sum += Rational(slice.first, slice.last)
+ end
+ sum
+ end
+
+ def worthless(n)
+ fibonacci = FibonacciSequence.new(n).to_a.last
+ largest_range = RationalSequence.new(n).to_a
+ p largest_range.reduce { |a, b| a + b } >= fibonacci
+ while largest_range.reduce { |a, b| a + b } > fibonacci do
+ n -= 1
+ largest_range = RationalSequence.new(n).to_a
+ end
+ largest_range
+ end
+end
  • Фибоначито е счупено, то е 1, 1, 2, 3, 5, ...
  • На много места викаш на Enumerable нещата #to_a, което е излишно
  • Прегледай пак методите на Enumerable (в meaningless можеш да се справиш по-добре)
  • Инеднтация, return и тн., а и имаш и един принт в worthless ;)

С други думи имаш какво да оправяш.

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

class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
rationals_found = []
max_value = 1
while rationals_found.length < @limit
numerators, denominators = create_parts(max_value)
- sequence = generate_sequence(numerators, denominators)
+ sequence = generate_sequence(numerators, denominators)
process_sequence(sequence, rationals_found, @limit).each do |rational_number|
yield (rationals_found.push rational_number).last
end
max_value += 1
end
end
def create_parts(max_value)
if max_value.odd?
numerators = (1..max_value).to_a
denominators = numerators.reverse
else
denominators = (1..max_value).to_a
numerators = denominators.reverse
end
return numerators, denominators
end
def generate_sequence(numerators, denominators)
rationals = []
index = 0
numerators.length.times do
rationals.push Rational(numerators[index], denominators[index])
index += 1
end
rationals
end
def process_sequence(sequence, found, maximum)
(sequence - found).take(maximum - found.length)
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
primes_found, current = 0, 1
while primes_found < @limit
if (PrimeSequence.is_prime? current)
yield current
primes_found += 1
end
current += 1
end
end
def self.is_prime?(n)
i = 2
while i < n
if (n % i) == 0
- return false
+ false
end
i += 1
end
n != 1
end
end
class FibonacciSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
- current, previous = 1, 1
+ current, previous = 1, 0
@limit.times do
yield current
current, previous = current + previous, current
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
group_one, group_two = [], []
RationalSequence.new(n).to_a.each do |rational_number|
if PrimeSequence.is_prime? rational_number.numerator or
PrimeSequence.is_prime? rational_number.denominator
group_one.push rational_number
else
group_two.push rational_number
end
end
numerator = [(group_one.reduce { |a, b| a * b}).to_i, 1].max
denominator = [(group_two.reduce { |a, b| a * b}).to_i, 1].max
Rational(numerator, denominator)
end
def aimless(n)
sum = 0
PrimeSequence.new(n).to_a.each_slice(2) do |slice|
if (slice.length.odd?)
slice.push 1
end
sum += Rational(slice.first, slice.last)
end
sum
end
def worthless(n)
fibonacci = FibonacciSequence.new(n).to_a.last
largest_range = RationalSequence.new(n).to_a
- p largest_range.reduce { |a, b| a + b } >= fibonacci
while largest_range.reduce { |a, b| a + b } > fibonacci do
n -= 1
largest_range = RationalSequence.new(n).to_a
end
largest_range
end
end