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

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

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

Резултати

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

Код

class RationalSequence
include Enumerable
def initialize(size = 1)
@sequence_size = size
end
def nil_duplicates(array)
new_array = array
duplicates = {}
index_1, index_2 = 0, 0
while index_1 < new_array.size
if new_array.count(new_array[index_1]) > 1 && !duplicates.has_key?(new_array[index_1])
duplicates[new_array[index_1]] = Array.new(1, index_1)
elsif new_array.count(new_array[index_1]) > 1
duplicates[new_array[index_1]] << index_1
end
index_1 += 1
end
duplicates.each_value do |value|
index_2 = 1
while index_2 < value.size
new_array[value[index_2]] = nil
index_2 += 1
end
end
new_array
end
def group_rat_numbers(array)
new_array = []
number, array_index = 0, 0
while array_index < array.size
if array[array_index].nil? || array[array_index].numerator == number
new_array[number] << array[array_index]
else
number += 1
new_array[number] = Array.new
new_array[number] << array[array_index]
end
array_index += 1
end
#puts new_array.drop(1).to_s
new_array.drop(1)
end
def generate_rat_array(size)
index_1, index_2 = 0, 0
array = []
while index_1 < size
index_2 = 0
while index_2 < size
array << Rational(1 + index_1, 1 + index_2)
index_2 += 1
end
index_1 += 1
end
array = nil_duplicates(array)
array = group_rat_numbers(array)
array
end
def snake_index_move(index_1, index_2, snake_direction)
snake_direction = 0 if snake_direction > 3
case snake_direction
# move down
when 0
index_1 += 1
# move diagonaly up-right
when 1
if index_1 > 0
index_1 -= 1
index_2 += 1
end
# move right
when 2
index_2 += 1
# move diagonaly down-left
when 3
if index_2 > 0
index_1 += 1
index_2 -= 1
end
end
[index_1, index_2, snake_direction]
end
def snake_index_next(index_1, index_2, snake_direction)
#new_snake_iterator = snake_iterator_direction + 1
#new_snake_iterator = 0 if new_snake_iterator > 3
test_1, test_2 = index_1, index_2
if snake_direction == 1 || snake_direction == 3
test_1, test_2, snake_direction = *snake_index_move(test_1, test_2, snake_direction)
if index_1 == test_1 && index_2 == test_2
snake_direction += 1
index_1, index_2, snake_direction = *snake_index_move(index_1, index_2, snake_direction)
else
index_1, index_2, snake_direction = *snake_index_move(index_1, index_2, snake_direction)
end
else
index_1, index_2, snake_direction = *snake_index_move(index_1, index_2, snake_direction)
snake_direction += 1
end
[index_1, index_2, snake_direction]
end
# .each method
def each
array_index = 0
index_1, index_2 = 0, 0
snake_direction = 0
array = generate_rat_array(@sequence_size)
while array_index < @sequence_size
if array[index_1][index_2].nil?
index_1, index_2, snake_direction = *snake_index_next(index_1, index_2, snake_direction)
else
yield array[index_1][index_2]
index_1, index_2, snake_direction = *snake_index_next(index_1, index_2, snake_direction)
array_index += 1
end
end
end
end
class FibonacciSequence
end
class PrimeSequence
end
class DrunkenMathematician
end

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

..FFFFFFFFFFFFFFFFFF

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/4), (2/3), (3/2), (4/1), (5/1), (6/1), (5/2), (4/3), (3/4), (2/5), (1/6), (1/7), (1/8), (2/7), (4/5), (5/4), (7/2), (8/1), (9/1), (10/1), (9/2), (8/3), (7/4), (6/5)]
       
       (compared using ==)
     # /tmp/d20151111-27349-12lhi0a/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/4), (1/6), (1/7), (1/8)]
       
       (compared using ==)
     # /tmp/d20151111-27349-12lhi0a/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 FibonacciSequence can return the first two Fibonacci numbers
     Failure/Error: expect(FibonacciSequence.new(2).to_a).to eq [1, 1]
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:26:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:26:in `new'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:26: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 FibonacciSequence can return the first 20 Fibonacci numbers
     Failure/Error: expect(FibonacciSequence.new(20).to_a).to eq [
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:30:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:30:in `new'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:30: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 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 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:37:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:37:in `new'
     # /tmp/d20151111-27349-12lhi0a/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)>'

  6) Fifth task FibonacciSequence is properly enumerable
     Failure/Error: expect(FibonacciSequence.new(20).select { |x| x.even? }).to eq [
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:45:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:45:in `new'
     # /tmp/d20151111-27349-12lhi0a/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)>'

  7) Fifth task PrimeSequence returns an empty array for 0 primes
     Failure/Error: expect(PrimeSequence.new(0).to_a).to eq []
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:53:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:53:in `new'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:53: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)>'

  8) Fifth task PrimeSequence can tell which the first two primes are
     Failure/Error: expect(PrimeSequence.new(2).to_a).to eq [2, 3]
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:57:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:57:in `new'
     # /tmp/d20151111-27349-12lhi0a/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)>'

  9) Fifth task PrimeSequence can tell the first 58 primes
     Failure/Error: expect(PrimeSequence.new(58).to_a).to eq [
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151111-27349-12lhi0a/spec.rb:61:in `initialize'
     # /tmp/d20151111-27349-12lhi0a/spec.rb:61:in `new'
     # /tmp/d20151111-27349-12lhi0a/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)>'

  10) Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
     Failure/Error: expect(DrunkenMathematician.meaningless(0)).to eq 1
     NoMethodError:
       undefined method `meaningless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  11) Fifth task DrunkenMathematician #meaningless can calculate for 3
     Failure/Error: expect(DrunkenMathematician.meaningless(4)).to eq Rational(1, 3)
     NoMethodError:
       undefined method `meaningless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  12) Fifth task DrunkenMathematician #meaningless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.meaningless(42)).to eq Rational(1, 11)
     NoMethodError:
       undefined method `meaningless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  13) 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 `aimless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  14) 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 `aimless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  15) Fifth task DrunkenMathematician #aimless can calculate for 42
     Failure/Error: expect(DrunkenMathematician.aimless(42)).to eq expected
     NoMethodError:
       undefined method `aimless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  16) Fifth task DrunkenMathematician #worthless can calculate for 2
     Failure/Error: expect(DrunkenMathematician.worthless(2)).to eq %w(1/1).map(&:to_r)
     NoMethodError:
       undefined method `worthless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/spec.rb:103: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)>'

  17) Fifth task DrunkenMathematician #worthless can calculate for 8
     Failure/Error: expect(DrunkenMathematician.worthless(8)).to eq expected
     NoMethodError:
       undefined method `worthless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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)>'

  18) Fifth task DrunkenMathematician #worthless can calculate for 15
     Failure/Error: expect(DrunkenMathematician.worthless(15)).to eq %w(
     NoMethodError:
       undefined method `worthless' for DrunkenMathematician:Class
     # /tmp/d20151111-27349-12lhi0a/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.24564 seconds
20 examples, 18 failures

Failed examples:

rspec /tmp/d20151111-27349-12lhi0a/spec.rb:11 # Fifth task RationalSequence can calculate the first 28 rational numbers
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:18 # Fifth task RationalSequence is properly enumerable
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:25 # Fifth task FibonacciSequence can return the first two Fibonacci numbers
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:29 # Fifth task FibonacciSequence can return the first 20 Fibonacci numbers
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:36 # Fifth task FibonacciSequence can be used to calculate the Lucas numbers
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:44 # Fifth task FibonacciSequence is properly enumerable
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:52 # Fifth task PrimeSequence returns an empty array for 0 primes
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:56 # Fifth task PrimeSequence can tell which the first two primes are
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:60 # Fifth task PrimeSequence can tell the first 58 primes
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:77 # Fifth task DrunkenMathematician #meaningless can calculate for 3
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:81 # Fifth task DrunkenMathematician #meaningless can calculate for 42
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:87 # Fifth task DrunkenMathematician #aimless can calculate for 3
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:91 # Fifth task DrunkenMathematician #aimless can calculate for 4
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:95 # Fifth task DrunkenMathematician #aimless can calculate for 42
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:102 # Fifth task DrunkenMathematician #worthless can calculate for 2
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-12lhi0a/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

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

+class RationalSequence
+ include Enumerable
+
+ def initialize(size = 1)
+ @sequence_size = size
+ end
+
+ def nil_duplicates(array)
+ new_array = array
+ duplicates = {}
+ index_1, index_2 = 0, 0
+ while index_1 < new_array.size
+ if new_array.count(new_array[index_1]) > 1 && !duplicates.has_key?(new_array[index_1])
+ duplicates[new_array[index_1]] = Array.new(1, index_1)
+ elsif new_array.count(new_array[index_1]) > 1
+ duplicates[new_array[index_1]] << index_1
+ end
+ index_1 += 1
+ end
+
+ duplicates.each_value do |value|
+ index_2 = 1
+ while index_2 < value.size
+ new_array[value[index_2]] = nil
+ index_2 += 1
+ end
+ end
+
+ new_array
+ end
+
+ def group_rat_numbers(array)
+ new_array = []
+ number, array_index = 0, 0
+ while array_index < array.size
+ if array[array_index].nil? || array[array_index].numerator == number
+ new_array[number] << array[array_index]
+ else
+ number += 1
+ new_array[number] = Array.new
+ new_array[number] << array[array_index]
+ end
+ array_index += 1
+ end
+ #puts new_array.drop(1).to_s
+ new_array.drop(1)
+ end
+
+ def generate_rat_array(size)
+ index_1, index_2 = 0, 0
+ array = []
+ while index_1 < size
+ index_2 = 0
+ while index_2 < size
+ array << Rational(1 + index_1, 1 + index_2)
+ index_2 += 1
+ end
+ index_1 += 1
+ end
+ array = nil_duplicates(array)
+ array = group_rat_numbers(array)
+
+ array
+ end
+
+ def snake_index_move(index_1, index_2, snake_direction)
+ snake_direction = 0 if snake_direction > 3
+ case snake_direction
+ # move down
+ when 0
+ index_1 += 1
+ # move diagonaly up-right
+ when 1
+ if index_1 > 0
+ index_1 -= 1
+ index_2 += 1
+ end
+ # move right
+ when 2
+ index_2 += 1
+ # move diagonaly down-left
+ when 3
+ if index_2 > 0
+ index_1 += 1
+ index_2 -= 1
+ end
+ end
+
+ [index_1, index_2, snake_direction]
+ end
+
+ def snake_index_next(index_1, index_2, snake_direction)
+ #new_snake_iterator = snake_iterator_direction + 1
+ #new_snake_iterator = 0 if new_snake_iterator > 3
+ test_1, test_2 = index_1, index_2
+ if snake_direction == 1 || snake_direction == 3
+ test_1, test_2, snake_direction = *snake_index_move(test_1, test_2, snake_direction)
+ if index_1 == test_1 && index_2 == test_2
+ snake_direction += 1
+ index_1, index_2, snake_direction = *snake_index_move(index_1, index_2, snake_direction)
+ else
+ index_1, index_2, snake_direction = *snake_index_move(index_1, index_2, snake_direction)
+ end
+ else
+ index_1, index_2, snake_direction = *snake_index_move(index_1, index_2, snake_direction)
+ snake_direction += 1
+ end
+
+ [index_1, index_2, snake_direction]
+ end
+
+ # .each method
+ def each
+ array_index = 0
+ index_1, index_2 = 0, 0
+ snake_direction = 0
+ array = generate_rat_array(@sequence_size)
+ while array_index < @sequence_size
+ if array[index_1][index_2].nil?
+ index_1, index_2, snake_direction = *snake_index_next(index_1, index_2, snake_direction)
+ else
+ yield array[index_1][index_2]
+ index_1, index_2, snake_direction = *snake_index_next(index_1, index_2, snake_direction)
+ array_index += 1
+ end
+ end
+ end
+end
+
+class FibonacciSequence
+end
+
+class PrimeSequence
+end
+
+class DrunkenMathematician
+end