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

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

Към профила на Иван Иванов

Резултати

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

Код

# fn: 44583 Ivan Borisov Ivanov
class Integer
def prime?
2.upto(pred).all? { |a| remainder(a).nonzero? }
end
end
class Integer
def prime_divisors
if (self > -2 && self < 2)
return nil
end
@number = self
if (self < -1 )
@number = -self
end
(2 .. @number).select{|x|
x.prime? && (@number % x == 0)}.map{ |x| x}
end
end
# test:
# puts "\n0.prime_divisors = #{0.prime_divisors}"
# puts "\n14.prime_divisors = #{14.prime_divisors}"
# puts "\n-1.prime_divisors = #{-1.prime_divisors}"
# puts "\n23.prime_divisors = #{23.prime_divisors}"
# puts "\n4.prime_divisors = #{4.prime_divisors}"
# puts "\n1.prime_divisors = #{1.prime_divisors}"
# puts "\n-15.prime_divisors = #{-15.prime_divisors}"
# fn: 44583 Ivan Borisov Ivanov
class Range
def fizzbuzz
@tmpArray = self.to_a
puts (@tmpArray[0] .. @tmpArray.last).map {|x|
if x % 3 == 0 && x % 5 == 0
:fizzbuzz
else if x % 5 ==0
:buzz
else if x % 3 == 0
:fizz
else
x
end end end
}.to_s
end
end
# test:
# (1 .. 15).fizzbuzz
# (11 ... 20).fizzbuzz

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

..[1, 2, :fizz, 4, :buzz, :fizz, 7, 8, :fizz, :buzz, 11, :fizz, 13, 14, :fizzbuzz, 16, 17, :fizz, 19, :buzz, :fizz, 22, 23, :fizz, :buzz, 26, :fizz, 28, 29, :fizzbuzz, 31, 32, :fizz, 34, :buzz, :fizz, 37, 38, :fizz, :buzz, 41, :fizz, 43, 44, :fizzbuzz, 46, 47, :fizz, 49, :buzz, :fizz, 52, 53, :fizz, :buzz, 56, :fizz, 58, 59, :fizzbuzz, 61, 62, :fizz, 64, :buzz, :fizz, 67, 68, :fizz, :buzz, 71, :fizz, 73, 74, :fizzbuzz, 76, 77, :fizz, 79, :buzz, :fizz, 82, 83, :fizz, :buzz, 86, :fizz, 88, 89, :fizzbuzz, 91, 92, :fizz, 94, :buzz, :fizz, 97, 98, :fizz, :buzz]
F[:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
FFFFF

Failures:

  1) Range#fizzbuzz it works with the first 100 numbers
     Failure/Error: (1..100).fizzbuzz.should eq [
       
       expected: [1, 2, :fizz, 4, :buzz, :fizz, 7, 8, :fizz, :buzz, 11, :fizz, 13, 14, :fizzbuzz, 16, 17, :fizz, 19, :buzz, :fizz, 22, 23, :fizz, :buzz, 26, :fizz, 28, 29, :fizzbuzz, 31, 32, :fizz, 34, :buzz, :fizz, 37, 38, :fizz, :buzz, 41, :fizz, 43, 44, :fizzbuzz, 46, 47, :fizz, 49, :buzz, :fizz, 52, 53, :fizz, :buzz, 56, :fizz, 58, 59, :fizzbuzz, 61, 62, :fizz, 64, :buzz, :fizz, 67, 68, :fizz, :buzz, 71, :fizz, 73, 74, :fizzbuzz, 76, 77, :fizz, 79, :buzz, :fizz, 82, 83, :fizz, :buzz, 86, :fizz, 88, 89, :fizzbuzz, 91, 92, :fizz, 94, :buzz, :fizz, 97, 98, :fizz, :buzz]
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-1uajnaa/spec.rb:18:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Range#fizzbuzz works with tricky ranges
     Failure/Error: (10...17).fizzbuzz.should eq [:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
       
       expected: [:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-1uajnaa/spec.rb:33:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Hash#group_values maps each value to an array of keys
     Failure/Error: {a: 1}.group_values.should eq 1 => [:a]
     NoMethodError:
       undefined method `group_values' for {:a=>1}:Hash
     # /tmp/d20130307-6960-1uajnaa/spec.rb:41:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Hash#group_values takes repetitions into account
     Failure/Error: {a: 1, b: 2, c: 1}.group_values.should eq 1 => [:a, :c], 2 => [:b]
     NoMethodError:
       undefined method `group_values' for {:a=>1, :b=>2, :c=>1}:Hash
     # /tmp/d20130307-6960-1uajnaa/spec.rb:45:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Array#densities maps each element to the number of occurences in the original array
     Failure/Error: [:a, :b, :c].densities.should eq [1, 1, 1]
     NoMethodError:
       undefined method `densities' for [:a, :b, :c]:Array
     # /tmp/d20130307-6960-1uajnaa/spec.rb:51:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Array#densities maps each element to the number of occurences in the original array (again)
     Failure/Error: [].densities.should eq []
     NoMethodError:
       undefined method `densities' for []:Array
     # /tmp/d20130307-6960-1uajnaa/spec.rb:57:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01431 seconds
8 examples, 6 failures

Failed examples:

rspec /tmp/d20130307-6960-1uajnaa/spec.rb:17 # Range#fizzbuzz it works with the first 100 numbers
rspec /tmp/d20130307-6960-1uajnaa/spec.rb:32 # Range#fizzbuzz works with tricky ranges
rspec /tmp/d20130307-6960-1uajnaa/spec.rb:40 # Hash#group_values maps each value to an array of keys
rspec /tmp/d20130307-6960-1uajnaa/spec.rb:44 # Hash#group_values takes repetitions into account
rspec /tmp/d20130307-6960-1uajnaa/spec.rb:50 # Array#densities maps each element to the number of occurences in the original array
rspec /tmp/d20130307-6960-1uajnaa/spec.rb:56 # Array#densities maps each element to the number of occurences in the original array (again)

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

Иван обнови решението на 15.10.2012 12:58 (преди около 12 години)

+# fn: 44583 Ivan Borisov Ivanov
+class Integer
+ def prime?
+ 2.upto(pred).all? { |a| remainder(a).nonzero? }
+ end
+end
+
+class Integer
+ def prime_divisors
+ if (self > -2 && self < 2)
+ return nil
+ end
+ @number = self
+ if (self < -1 )
+ @number = -self
+ end
+
+ (2 .. @number).select{|x|
+ x.prime? && (@number % x == 0)}.map{ |x| x}
+ end
+end
+# test:
+# puts "\n0.prime_divisors = #{0.prime_divisors}"
+# puts "\n14.prime_divisors = #{14.prime_divisors}"
+# puts "\n-1.prime_divisors = #{-1.prime_divisors}"
+# puts "\n23.prime_divisors = #{23.prime_divisors}"
+# puts "\n4.prime_divisors = #{4.prime_divisors}"
+# puts "\n1.prime_divisors = #{1.prime_divisors}"
+# puts "\n-15.prime_divisors = #{-15.prime_divisors}"
+
+
+# fn: 44583 Ivan Borisov Ivanov
+class Range
+ def fizzbuzz
+ @tmpArray = self.to_a
+
+ puts (@tmpArray[0] .. @tmpArray.last).map {|x|
+ if x % 3 == 0 && x % 5 == 0
+ :fizzbuzz
+ else if x % 5 ==0
+ :buzz
+ else if x % 3 == 0
+ :fizz
+ else
+ x
+ end end end
+ }.to_s
+
+ end
+end
+# test:
+# (1 .. 15).fizzbuzz
+# (11 ... 20).fizzbuzz