Решение на Трета задача от Христина Тодорова

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

Към профила на Христина Тодорова

Резултати

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

Код

class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit - 1
@saver = limit
@arr = []
@step = 2
end
def to_a
if @limit == - 1 then return @arr else @arr.push(Rational(1,1)) end
while(@limit > 0)
i = 1
while(@limit > 0 and i <= @step)
@arr.push(Rational(@step - i + 1, i)).uniq!
@limit = @saver - @arr.length
i += 1
end
@step += 1
i = 1
while(@limit > 0 and i <= @step)
@arr.push(Rational(i, @step - i + 1)).uniq!
@limit = @saver - @arr.length
i += 1
end
@step += 1
end
@arr
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
stop = 0
current_number = 2
while stop < @limit do
if prime?(current_number)
yield current_number
stop = stop+1
end
current_number = current_number + 1
end
end
def prime?(n)
return false if n <= 1
2.upto(n-1) do |x|
return false if n%x == 0
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(limit,first: 1, second: 1)
@limit = limit
@first = first
@second = second
end
def each
if(@limit == 0)
yield
return
end
stop = 0
yield @first
while stop < @limit-1
stop = stop + 1
yield @second
@first, @second = @second, @first + @second
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
if (n == 0)
return 0
end
arr = RationalSequence.new(n).to_a
first_array = []
second_array = []
for i in 0...n
if !PrimeSequence.new(1).prime?(arr[i].numerator) and
!PrimeSequence.new(1).prime?(arr[i].denominator)
second_array << arr[i]
else
first_array << arr[i]
end
end
product_one = 1
first_array.each { |a| product_one *= a }
product_two = 1
second_array.each { |a| product_two *= a }
product_one / product_two
end
def aimless(n)
if (n == 0)
return 0
end
sum = 0
arr = PrimeSequence.new(n).to_a
(0..n/2).each do |i|
if i%2 == 0
sum = sum + Rational(arr[i],arr[i+1])
end
end
sum
end
def worthless(n)
if (n == 0)
0
else
number = FibonacciSequence.new(n).to_a.at(n-1)
i = 1
sum = RationalSequence.new(i).to_a.reduce :+
while sum.ceil <= number
i += 1
sum = RationalSequence.new(i).to_a.reduce :+
end
RationalSequence.new(i-1).to_a
end
end
end

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

...F.......F..F.F..F

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:0x007fb4cc032ba8>
     # /tmp/d20151111-27349-1311tla/spec.rb:19:in `select'
     # /tmp/d20151111-27349-1311tla/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 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-1311tla/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)>'

  3) 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: (2/3)
       
       (compared using ==)
     # /tmp/d20151111-27349-1311tla/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)>'

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

  5) Fifth task DrunkenMathematician #worthless can calculate for 15
     Failure/Error: expect(DrunkenMathematician.worthless(15)).to eq %w(
     Timeout::Error:
       execution expired
     # /tmp/d20151111-27349-1311tla/solution.rb:23:in `uniq!'
     # /tmp/d20151111-27349-1311tla/solution.rb:23:in `to_a'
     # /tmp/d20151111-27349-1311tla/solution.rb:140:in `worthless'
     # /tmp/d20151111-27349-1311tla/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 1.1 seconds
20 examples, 5 failures

Failed examples:

rspec /tmp/d20151111-27349-1311tla/spec.rb:18 # Fifth task RationalSequence is properly enumerable
rspec /tmp/d20151111-27349-1311tla/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-1311tla/spec.rb:87 # Fifth task DrunkenMathematician #aimless can calculate for 3
rspec /tmp/d20151111-27349-1311tla/spec.rb:95 # Fifth task DrunkenMathematician #aimless can calculate for 42
rspec /tmp/d20151111-27349-1311tla/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

Христина обнови решението на 25.10.2015 19:18 (преди над 8 години)

+class RationalSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ @saver = limit
+ @arr = []
+ @step = 2
+ end
+
+ def to_a
+ if @limit == 0 then return @arr else @arr.push(Rational(1,1)) end
+ while(@limit - 1 > 0)
+ i = 1
+ while(@limit > 0 and i <= @step)
+ @arr.push(Rational(@step - i + 1, i)).uniq!
+ @limit = @saver - @arr.length
+ i += 1
+ end
+ @step += 1
+ i = 1
+ while(@limit > 0 and i <= @step)
+ @arr.push(Rational(i, @step - i + 1)).uniq!
+ @limit = @saver - @arr.length
+ i += 1
+ end
+ @step += 1
+ end
+ @arr
+ end
+end
+
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(limit)
+ @limit = limit
+ end
+
+ def each
+ stop = 0
+ current_number = 2
+
+ while stop < @limit do
+ if prime?(current_number)
+ yield current_number
+ stop = stop+1
+ end
+ current_number = current_number + 1
+ end
+ end
+
+
+ def prime?(n)
+ return false if n <= 1
+ 2.upto(n-1) do |x|
+ return false if n%x == 0
+ end
+ true
+ end
+end
+
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(limit,first: 1, second: 1)
+ @limit = limit
+ @first = first
+ @second = second
+ end
+
+ def each
+ stop = 0
+
+ yield @first
+ while stop < @limit-1
+ stop = stop + 1
+ yield @second
+ @first, @second = @second, @first + @second
+ end
+ end
+
+end
+
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ arr = RationalSequence.new(n).to_a
+ first_array = []
+ second_array = []
+ for i in 0...n
+ if !PrimeSequence.new(1).prime?(arr[i].numerator) and
+ !PrimeSequence.new(1).prime?(arr[i].denominator)
+ second_array << arr[i]
+ else
+ first_array << arr[i]
+ end
+ end
+ product_one = 1
+ first_array.each { |a| product_one *= a }
+ product_two = 1
+ second_array.each { |a| product_two *= a }
+ product_one / product_two
+ end
+
+ def aimless(n)
+ sum = 0
+ arr = PrimeSequence.new(n).to_a
+ (0..n/2).each do |i|
+ if i%2 == 0
+ sum = sum + Rational(arr[i],arr[i+1])
+ end
+ end
+ return sum
+ end
+
+ def worthless(n)
+ number = FibonacciSequence.new(n).to_a.at(n-1)
+ i = 1
+ sum = RationalSequence.new(i).to_a.reduce :+
+ while sum.ceil <= number
+ i += 1
+ sum = RationalSequence.new(i).to_a.reduce :+
+ end
+ RationalSequence.new(i-1).to_a
+ end
+
+end

Христина обнови решението на 25.10.2015 20:12 (преди над 8 години)

class RationalSequence
include Enumerable
def initialize(limit)
- @limit = limit
+ @limit = limit - 1
@saver = limit
@arr = []
@step = 2
end
def to_a
- if @limit == 0 then return @arr else @arr.push(Rational(1,1)) end
- while(@limit - 1 > 0)
+ if @limit == - 1 then return @arr else @arr.push(Rational(1,1)) end
+ while(@limit > 0)
i = 1
while(@limit > 0 and i <= @step)
@arr.push(Rational(@step - i + 1, i)).uniq!
@limit = @saver - @arr.length
i += 1
end
@step += 1
i = 1
while(@limit > 0 and i <= @step)
@arr.push(Rational(i, @step - i + 1)).uniq!
@limit = @saver - @arr.length
i += 1
end
@step += 1
end
@arr
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
stop = 0
current_number = 2
while stop < @limit do
if prime?(current_number)
yield current_number
stop = stop+1
end
current_number = current_number + 1
end
end
def prime?(n)
return false if n <= 1
2.upto(n-1) do |x|
return false if n%x == 0
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(limit,first: 1, second: 1)
@limit = limit
@first = first
@second = second
end
def each
stop = 0
yield @first
while stop < @limit-1
stop = stop + 1
yield @second
@first, @second = @second, @first + @second
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
arr = RationalSequence.new(n).to_a
first_array = []
second_array = []
for i in 0...n
if !PrimeSequence.new(1).prime?(arr[i].numerator) and
!PrimeSequence.new(1).prime?(arr[i].denominator)
second_array << arr[i]
else
first_array << arr[i]
end
end
product_one = 1
first_array.each { |a| product_one *= a }
product_two = 1
second_array.each { |a| product_two *= a }
product_one / product_two
end
def aimless(n)
sum = 0
arr = PrimeSequence.new(n).to_a
(0..n/2).each do |i|
if i%2 == 0
sum = sum + Rational(arr[i],arr[i+1])
end
end
return sum
end
def worthless(n)
number = FibonacciSequence.new(n).to_a.at(n-1)
i = 1
sum = RationalSequence.new(i).to_a.reduce :+
while sum.ceil <= number
i += 1
sum = RationalSequence.new(i).to_a.reduce :+
end
RationalSequence.new(i-1).to_a
end
end

Христина обнови решението на 25.10.2015 20:34 (преди над 8 години)

class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit - 1
@saver = limit
@arr = []
@step = 2
end
def to_a
if @limit == - 1 then return @arr else @arr.push(Rational(1,1)) end
while(@limit > 0)
i = 1
while(@limit > 0 and i <= @step)
@arr.push(Rational(@step - i + 1, i)).uniq!
@limit = @saver - @arr.length
i += 1
end
@step += 1
i = 1
while(@limit > 0 and i <= @step)
@arr.push(Rational(i, @step - i + 1)).uniq!
@limit = @saver - @arr.length
i += 1
end
@step += 1
end
@arr
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
stop = 0
current_number = 2
while stop < @limit do
if prime?(current_number)
yield current_number
stop = stop+1
end
current_number = current_number + 1
end
end
def prime?(n)
return false if n <= 1
2.upto(n-1) do |x|
return false if n%x == 0
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(limit,first: 1, second: 1)
@limit = limit
@first = first
@second = second
end
def each
+ if(@limit == 0)
+ yield
+ return
+ end
stop = 0
yield @first
while stop < @limit-1
stop = stop + 1
yield @second
@first, @second = @second, @first + @second
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
+ if (n == 0)
+ return 0
+ end
arr = RationalSequence.new(n).to_a
first_array = []
second_array = []
for i in 0...n
if !PrimeSequence.new(1).prime?(arr[i].numerator) and
!PrimeSequence.new(1).prime?(arr[i].denominator)
second_array << arr[i]
else
first_array << arr[i]
end
end
product_one = 1
first_array.each { |a| product_one *= a }
product_two = 1
second_array.each { |a| product_two *= a }
product_one / product_two
end
def aimless(n)
+ if (n == 0)
+ return 0
+ end
sum = 0
arr = PrimeSequence.new(n).to_a
(0..n/2).each do |i|
if i%2 == 0
sum = sum + Rational(arr[i],arr[i+1])
end
end
- return sum
+ sum
end
def worthless(n)
- number = FibonacciSequence.new(n).to_a.at(n-1)
- i = 1
- sum = RationalSequence.new(i).to_a.reduce :+
- while sum.ceil <= number
- i += 1
- sum = RationalSequence.new(i).to_a.reduce :+
+ if (n == 0)
+ 0
+ else
+ number = FibonacciSequence.new(n).to_a.at(n-1)
+ i = 1
+ sum = RationalSequence.new(i).to_a.reduce :+
+ while sum.ceil <= number
+ i += 1
+ sum = RationalSequence.new(i).to_a.reduce :+
+ end
+ RationalSequence.new(i-1).to_a
end
- RationalSequence.new(i-1).to_a
end
end