Решение на Трета задача от Кузман Белев

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

Към профила на Кузман Белев

Резултати

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

Код

class Integer
def prime?
return false if self == 1
2.upto((self**0.5).floor).each { |item| return false if self%item == 0 }
true
end
end
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
temp_array = []
result_array = []
2.upto(100).select { |sum| temp_array << produce_rats(sum) }
temp_array = temp_array.flatten & temp_array.flatten
result_array = temp_array.select { |item| item }.take(@limit).to_a
result_array.each { |item| yield item }
end
def produce_rats(sum)
result_array = []
if sum.odd?
1.upto(sum - 1).each { |i| result_array << (sum-i)/i.to_r }
else
1.upto(sum - 1).each { |i| result_array << i/(sum-i).to_r }
end
result_array
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
result_array = 1.upto(Float::INFINITY).lazy.select { |item| item.prime? }.take(@limit).to_a
result_array.each { |item| yield item }
end
end
class FibonacciSequence
include Enumerable
def initialize(limit, first: 1, second: 1)
@limit = limit
@first = first
@second = second
end
def each
previous, current = @first, @second
count = 0
while count < @limit
yield previous
current, previous = current + previous, current
count += 1
end
end
end
module DrunkenMathematician
module_function
def answer
42
end
def meaningless(count)
all_rats = []
return {} if count == 0
return (1/1).to_r if count == 1
RationalSequence.new(count).each { |item| all_rats << item }
array_prime = all_rats.find_all { |item| item.numerator.prime? or item.denominator.prime?}
array_prime.reduce(:*) / (all_rats - array_prime).reduce(:*)
end
def aimless(count)
return 0 if count == 0
all_primes = []
rats = []
PrimeSequence.new(count).each { |item| all_primes << item }
all_primes << 1 unless all_primes.length.even?
all_primes.each_slice(2) { |item| rats << item[0]/item[1].to_r }
rats.reduce(:+)
end
def worthless(count)
return {} if count == 0
sum = 0
all_rats = []
nth_fibonacci = (FibonacciSequence.new(count).to_a - FibonacciSequence.new(count - 1).to_a)[0]
RationalSequence.new(count).each { |item| all_rats << item }
all_rats.take_while { |item| (sum += item) <= nth_fibonacci }
end
end

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

...........F.....FFF

Failures:

  1) Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
     Failure/Error: expect(DrunkenMathematician.meaningless(0)).to eq 1
       
       expected: 1
            got: {}
       
       (compared using ==)
     # /tmp/d20151111-27349-1eqcupo/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)>'

  2) Fifth task DrunkenMathematician #worthless can calculate for 2
     Failure/Error: expect(DrunkenMathematician.worthless(2)).to eq %w(1/1).map(&:to_r)
     ArgumentError:
       comparison of Rational with nil failed
     # /tmp/d20151111-27349-1eqcupo/solution.rb:86:in `<='
     # /tmp/d20151111-27349-1eqcupo/solution.rb:86:in `block in worthless'
     # /tmp/d20151111-27349-1eqcupo/solution.rb:86:in `take_while'
     # /tmp/d20151111-27349-1eqcupo/solution.rb:86:in `worthless'
     # /tmp/d20151111-27349-1eqcupo/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)>'

  3) 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-1eqcupo/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)>'

  4) 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-1eqcupo/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.32289 seconds
20 examples, 4 failures

Failed examples:

rspec /tmp/d20151111-27349-1eqcupo/spec.rb:72 # Fifth task DrunkenMathematician #meaningless can calculate for 0 and 1
rspec /tmp/d20151111-27349-1eqcupo/spec.rb:102 # Fifth task DrunkenMathematician #worthless can calculate for 2
rspec /tmp/d20151111-27349-1eqcupo/spec.rb:106 # Fifth task DrunkenMathematician #worthless can calculate for 8
rspec /tmp/d20151111-27349-1eqcupo/spec.rb:111 # Fifth task DrunkenMathematician #worthless can calculate for 15

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

Кузман обнови решението на 25.10.2015 10:57 (преди над 8 години)

+class Integer
+ def prime?(number)
+ return true if (number == 2 or number == 3)
+ return false if number == 1
+ square = (number**0.5).floor
+ 2.upto(square).each { |item| return false if number%item == 0 }
+ true
+ end
+end
+class RationalSequence
+ include Enumerable
+ def initialize(limit)
+ @limit = limit
+ end
+ def each
+ temp_array = []
+ result_array = []
+ 2.upto(100).select { |sum| temp_array << produce_rats(sum) }
+ temp_array = temp_array.flatten & temp_array.flatten
+ result_array = temp_array.select { |item| item }.take(@limit).to_a
+ result_array.each { |item| yield item }
+ end
+ def produce_rats(sum)
+ result_array = []
+ if sum.odd?
+ 1.upto(sum - 1).each { |i| result_array << (sum-i)/i.to_r }
+ return result_array
+ else
+ 1.upto(sum - 1).each { |i| result_array << i/(sum-i).to_r }
+ return result_array
+ end
+ end
+end
+class PrimeSequence
+ include Enumerable
+ def initialize(limit)
+ @limit = limit
+ end
+ def each
+ result_array = 1.upto(Float::INFINITY).lazy.select { |item| item.prime? }.take(@limit).to_a
+ result_array.each { |item| yield item }
+ end
+end
+class FibonacciSequence
+ include Enumerable
+ def initialize(limit, first: 1, second: 1)
+ @limit = limit
+ @first = first
+ @second = second
+ end
+ def each
+ previous, current = @first, @second
+ count = 0
+ while count < @limit
+ yield previous
+ current, previous = current + previous, current
+ count += 1
+ end
+ end
+end
+module DrunkenMathematician
+ module_function
+ def answer
+ 42
+ end
+ def meaningless(count)
+ all_rats = []
+ RationalSequence.new(count).each { |item| all_rats << item }
+ array_prime = all_rats.find_all { |item| item.numerator.prime? or item.denominator.prime?}
+ array_prime.reduce(:*) / (all_rats - array_prime).reduce(:*)
+ end
+ def aimless(count)
+ all_primes = []
+ rats = []
+ PrimeSequence.new(count).each { |item| all_primes << item }
+ all_primes << 1 unless all_primes.length.even?
+ all_primes.each_slice(2) { |item| rats << item[0]/item[1].to_r }
+ rats.reduce(:+)
+ end
+ def worthless(count)
+ sum = 0
+ all_rats = []
+ nth_fibonacci = (FibonacciSequence.new(count).to_a - FibonacciSequence.new(count-1).to_a)[0]
+ RationalSequence.new(count).each { |item| all_rats << item }
+ all_rats.take_while { |item| (sum+=item) <= nth_fibonacci }
+ end
+end

Както си написал prime? би следвало да се извиква с some_number_to_be_ignored.prime?(the_number_to_be_checked). Вместо това направи така, че да не приема аргумент, а да работи върху текущата инстанция (self).

Алгоритмично си на прав път, но не си помислил за corner case-и, вероятно защото кодът ти е започнал да те обърква.

Опитай се да поразкрасиш кода, следвайки style guide-а. Ако си оправиш whitespace-ите съм убеден, че много по-лесно ще можеш да разсъждаваш върху какво се случва.

След като оправиш prime? и поподредиш малко ще откриеш, че не си далече от работещо решение. Би помогнало ако зададеш някои конкретни въпроси.

Кузман обнови решението на 26.10.2015 17:27 (преди над 8 години)

class Integer
- def prime?(number)
- return true if (number == 2 or number == 3)
- return false if number == 1
- square = (number**0.5).floor
- 2.upto(square).each { |item| return false if number%item == 0 }
+ def prime?
+ return false if self == 1
+ 2.upto((self**0.5).floor).each { |item| return false if self%item == 0 }
true
end
end
class RationalSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
temp_array = []
- result_array = []
+ result_array = []
2.upto(100).select { |sum| temp_array << produce_rats(sum) }
- temp_array = temp_array.flatten & temp_array.flatten
- result_array = temp_array.select { |item| item }.take(@limit).to_a
- result_array.each { |item| yield item }
+ temp_array = temp_array.flatten & temp_array.flatten
+ result_array = temp_array.select { |item| item }.take(@limit).to_a
+ result_array.each { |item| yield item }
end
def produce_rats(sum)
result_array = []
if sum.odd?
- 1.upto(sum - 1).each { |i| result_array << (sum-i)/i.to_r }
- return result_array
- else
- 1.upto(sum - 1).each { |i| result_array << i/(sum-i).to_r }
- return result_array
- end
+ 1.upto(sum - 1).each { |i| result_array << (sum-i)/i.to_r }
+ else
+ 1.upto(sum - 1).each { |i| result_array << i/(sum-i).to_r }
+ end
+ result_array
end
end
class PrimeSequence
include Enumerable
def initialize(limit)
@limit = limit
end
def each
result_array = 1.upto(Float::INFINITY).lazy.select { |item| item.prime? }.take(@limit).to_a
- result_array.each { |item| yield item }
+ result_array.each { |item| yield item }
end
end
class FibonacciSequence
include Enumerable
def initialize(limit, first: 1, second: 1)
@limit = limit
- @first = first
- @second = second
+ @first = first
+ @second = second
end
def each
- previous, current = @first, @second
- count = 0
- while count < @limit
- yield previous
- current, previous = current + previous, current
- count += 1
- end
+ previous, current = @first, @second
+ count = 0
+ while count < @limit
+ yield previous
+ current, previous = current + previous, current
+ count += 1
+ end
end
end
module DrunkenMathematician
module_function
def answer
42
end
def meaningless(count)
all_rats = []
+ return {} if count == 0
+ return (1/1).to_r if count == 1
RationalSequence.new(count).each { |item| all_rats << item }
- array_prime = all_rats.find_all { |item| item.numerator.prime? or item.denominator.prime?}
+ array_prime = all_rats.find_all { |item| item.numerator.prime? or item.denominator.prime?}
array_prime.reduce(:*) / (all_rats - array_prime).reduce(:*)
end
def aimless(count)
+ return 0 if count == 0
all_primes = []
- rats = []
+ rats = []
PrimeSequence.new(count).each { |item| all_primes << item }
- all_primes << 1 unless all_primes.length.even?
- all_primes.each_slice(2) { |item| rats << item[0]/item[1].to_r }
- rats.reduce(:+)
+ all_primes << 1 unless all_primes.length.even?
+ all_primes.each_slice(2) { |item| rats << item[0]/item[1].to_r }
+ rats.reduce(:+)
end
def worthless(count)
+ return {} if count == 0
sum = 0
all_rats = []
- nth_fibonacci = (FibonacciSequence.new(count).to_a - FibonacciSequence.new(count-1).to_a)[0]
+ nth_fibonacci = (FibonacciSequence.new(count).to_a - FibonacciSequence.new(count - 1).to_a)[0]
RationalSequence.new(count).each { |item| all_rats << item }
- all_rats.take_while { |item| (sum+=item) <= nth_fibonacci }
+ all_rats.take_while { |item| (sum += item) <= nth_fibonacci }
end
end

Благодаря за коментара!

Предполагам, едно от нещата, които визираш, е грозната идентация (: Току що се преборих с notepad++ и начина, по който ме саботира(понеже, да, Windows user съм).Доста бих оценил конкретни примери, при възможност и време, разбира се.

Така или иначе, ще мина пак през ръководството, но мисля, че в 4-та задача имам малък напредък.