Решение на Първа задача от Петър Добрев

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

Към профила на Петър Добрев

Резултати

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

Код

class Integer
def prime?
dividers = 2..(self / 2).abs
dividers.each do |divider|
if self % divider == 0
return false
end
end
return true
end
def prime_factors
factors = ->(n, fact_list) { return fact_list if n == 1
factor = (2..n).find {|x| n % x == 0}
factors.(n / factor, fact_list + [factor]) }
return factors.(self.abs, fact_list = [])
end
def harmonic
harmonic_gen = ->(curr_num, sum) { return sum if curr_num == 0
puts sum
harmonic_gen.(curr_num - 1,
sum + 1.to_r / curr_num) }
return harmonic_gen.(self, Rational(0, 1))
end
def digits
digits_gen = ->(n, digits_list) { return digits_list.reverse if n == 0
digits_gen.(n / 10,
digits_list + [n % 10]) }
return digits_gen.(self, digits_list = [])
end
end
class Array
def frequencies
frequency_hash = Hash.new(0)
self.each { |element| frequency_hash[element] += 1 }
return frequency_hash
end
def average
sum = 0.0
self.each { |element| sum += element}
return sum / self.size
end
def drop_every(n)
modified_array = self[0..self.size-1]
current_index = n - 1
while current_index < modified_array.length
modified_array.delete_at(current_index)
current_index += n - 1
end
return modified_array
end
def combine_with(other)
combine = ->(combined, fst, snd) { return combined + snd if fst == []
return combined + fst if snd == []
combine.(combined + [fst[0]] + [snd[0]],
fst[1..fst.size],
snd[1..snd.size]) }
return combine.(combined = [], self, other)
end
end

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

F..0/1
0/1
1/2
0/1
1/10
19/90
121/360
1207/2520
1627/2520
2131/2520
2761/2520
3601/2520
4861/2520
0/1
1/42
83/1722
2521/34440
14751/149240
354889/2835560
15966453/104915720
169927007/944241480
39381067/188848296
763902287/3210421032
9473065501/35314631352
42306590923/141258525408
1452762844021/4379014287648
7993649934713/21895071438240
253710919544917/634957071708960
276387957820237/634957071708960
899714659206151/1904871215126880
972978936711031/1904871215126880
5245868926580531/9524356075634400
5642717096398631/9524356075634400
139306849292802913/219060189739591200
149264130644602513/219060189739591200
159695568251249713/219060189739591200
170648577738229273/219060189739591200
182178061408734073/219060189739591200
194348071949822473/219060189739591200
207233965463916073/219060189739591200
220925227322640523/219060189739591200
235529239971946603/219060189739591200
251176396381917403/219060189739591200
268027180208039803/219060189739591200
286282196019672403/219060189739591200
27836068974842873/19914562703599200
29827525245202793/19914562703599200
32040254434491593/19914562703599200
34529574772441493/19914562703599200
5339216043075299/2844937529085600
5813372297922899/2844937529085600
6382359803740019/2844937529085600
7093594186011419/2844937529085600
8041906695706619/2844937529085600
9464375460249419/2844937529085600
.FF........

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-cgh7ud/spec.rb:3:in `block (2 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) Integer#digits constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-cgh7ud/spec.rb:44:in `block (2 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) Integer#digits works with negative numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20131023-4395-cgh7ud/solution.rb:29

Finished in 0.24036 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-cgh7ud/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-cgh7ud/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-cgh7ud/spec.rb:50 # Integer#digits works with negative numbers

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

Петър обнови решението на 16.10.2013 16:43 (преди около 11 години)

+class Integer
+ def prime?
+ dividers = 2..(self / 2).abs
+ dividers.each do |divider|
+ if self % divider == 0
+ return false
+ end
+ end
+ return true
+ end
+
+ def prime_factors
+ factors = ->(n, fact_list) { return fact_list if n == 1
+ factor = (2..n).find {|x| n % x == 0}
+ factors.(n / factor, fact_list + [factor]) }
+ return factors.(self.abs, fact_list = [])
+ end
+
+ def harmonic
+ test = Rational(1, 1)
+ harmonic_gen = ->(curr_num, sum) { return sum if curr_num == 0
+ puts sum
+ harmonic_gen.(curr_num - 1,
+ sum + test / curr_num) }
+ return harmonic_gen.(self, Rational(0, 1))
+ end
+
+ def digits
+ digits_gen = ->(n, digits_list) { return digits_list.reverse if n == 0
+ digits_gen.(n / 10,
+ digits_list + [n % 10]) }
+ return digits_gen.(self, digits_list = [])
+ end
+end
+
+
+class Array
+ def frequencies
+ frequency_hash = Hash.new(0)
+ self.each { |element| frequency_hash[element] += 1 }
+ return frequency_hash
+ end
+
+ def average
+ sum = 0.0
+ self.each { |element| sum += element}
+ return sum / self.size
+ end
+
+ def drop_every(n)
+ modified_array = self[0..self.size-1]
+ current_index = n - 1
+ while current_index < modified_array.length
+ modified_array.delete_at(current_index)
+ current_index += n - 1
+ end
+ return modified_array
+ end
+
+ def combine_with(other)
+ combine = ->(combined, fst, snd) { return combined + snd if fst == []
+ return combined + fst if snd == []
+ combine.(combined + [fst[0]] + [snd[0]],
+ fst[1..fst.size],
+ snd[1..snd.size]) }
+ return combine.(combined = [], self, other)
+ end
+end
+

Петър обнови решението на 16.10.2013 16:54 (преди около 11 години)

class Integer
def prime?
dividers = 2..(self / 2).abs
dividers.each do |divider|
if self % divider == 0
return false
end
end
return true
end
def prime_factors
factors = ->(n, fact_list) { return fact_list if n == 1
factor = (2..n).find {|x| n % x == 0}
factors.(n / factor, fact_list + [factor]) }
return factors.(self.abs, fact_list = [])
end
def harmonic
- test = Rational(1, 1)
harmonic_gen = ->(curr_num, sum) { return sum if curr_num == 0
puts sum
harmonic_gen.(curr_num - 1,
- sum + test / curr_num) }
+ sum + 1.to_r / curr_num) }
return harmonic_gen.(self, Rational(0, 1))
end
def digits
digits_gen = ->(n, digits_list) { return digits_list.reverse if n == 0
digits_gen.(n / 10,
digits_list + [n % 10]) }
return digits_gen.(self, digits_list = [])
end
end
class Array
def frequencies
frequency_hash = Hash.new(0)
self.each { |element| frequency_hash[element] += 1 }
return frequency_hash
end
def average
sum = 0.0
self.each { |element| sum += element}
return sum / self.size
end
def drop_every(n)
modified_array = self[0..self.size-1]
current_index = n - 1
while current_index < modified_array.length
modified_array.delete_at(current_index)
current_index += n - 1
end
return modified_array
end
def combine_with(other)
combine = ->(combined, fst, snd) { return combined + snd if fst == []
return combined + fst if snd == []
combine.(combined + [fst[0]] + [snd[0]],
fst[1..fst.size],
snd[1..snd.size]) }
return combine.(combined = [], self, other)
end
end
-