Решение на Първа задача от Христо Банчев

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

Към профила на Христо Банчев

Резултати

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

Код

# Христо Любомиров Банчев, ф.н. 44385
# Информатика, курс 4, група 3
class Array
def to_hash
result = {}
copy = Array.new( self )
(copy.size/2).times do
result.merge!({ copy.shift => copy.shift })
end
result
end
def index_by
result = {}
self.each { |a| result[yield(a)] = a }
result
end
def subarray_count( subarray )
copy = Array.new( self )
count = 0
copy.size.times do
count += 1 if subarray == copy[0..subarray.size-1]
copy.shift
end
count
end
def occurences_count
result = Hash.new( 0 )
self.each { |a| result[a] += 1 }
result
end
end

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

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

Failures:

  1) Array#to_hash converts an array to a hash
     Failure/Error: [[:one, 1], [:two, 2]].to_hash.should eq(one: 1, two: 2)
       
       expected {:one=>1, :two=>2}
            got {[:one, 1]=>[:two, 2]}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{:one=>1, :two=>2}
       +{[:one, 1]=>[:two, 2]}
     # /tmp/d20111025-2903-7aryqu/spec.rb:3: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) Array#to_hash uses the last pair when a key is present multiple times
     Failure/Error: [[1, 2], [3, 4], [1, 3]].to_hash.should eq(1 => 3, 3 => 4)
       
       expected {1=>3, 3=>4}
            got {[1, 2]=>[3, 4]}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>3, 3=>4}
       +{[1, 2]=>[3, 4]}
     # /tmp/d20111025-2903-7aryqu/spec.rb:7: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) Array#to_hash works when the keys and values are arrays
     Failure/Error: [[1, [2, 3]], [[4, 5], 6]].to_hash.should eq(1 => [2, 3], [4, 5] => 6)
       
       expected {1=>[2, 3], [4, 5]=>6}
            got {[1, [2, 3]]=>[[4, 5], 6]}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>[2, 3], [4, 5]=>6}
       +{[1, [2, 3]]=>[[4, 5], 6]}
     # /tmp/d20111025-2903-7aryqu/spec.rb:11: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) Array#to_hash works with boolean keys
     Failure/Error: [nil, 'nil'],
       
       expected {nil=>"nil", true=>"true", false=>"false"}
            got {[nil, "nil"]=>[true, "true"]}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{nil=>"nil", true=>"true", false=>"false"}
       +{[nil, "nil"]=>[true, "true"]}
     # /tmp/d20111025-2903-7aryqu/spec.rb:20: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.01768 seconds
17 examples, 4 failures

Failed examples:

rspec /tmp/d20111025-2903-7aryqu/spec.rb:2 # Array#to_hash converts an array to a hash
rspec /tmp/d20111025-2903-7aryqu/spec.rb:6 # Array#to_hash uses the last pair when a key is present multiple times
rspec /tmp/d20111025-2903-7aryqu/spec.rb:10 # Array#to_hash works when the keys and values are arrays
rspec /tmp/d20111025-2903-7aryqu/spec.rb:18 # Array#to_hash works with boolean keys

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

Христо обнови решението на 24.10.2011 16:54 (преди над 12 години)

+# Христо Любомиров Банчев, ф.н. 44385
+# Информатика, курс 4, група 3
+
+class Array
+ def to_hash
+ result = {}
+ copy = Array.new( self )
+
+ (copy.size/2).times do
+ result.merge!({ copy.shift => copy.shift })
+ end
+
+ result
+ end
+
+ def index_by
+ result = {}
+ self.each { |a| result[yield(a)] = a }
+ result
+ end
+
+ def subarray_count( subarray )
+ copy = Array.new( self )
+ count = 0
+ copy.size.times do
+ count += 1 if subarray == copy[0..subarray.size-1]
+ copy.shift
+ end
+ count
+ end
+
+ def occurences_count
+ result = Hash.new( 0 )
+ self.each { |a| result[a] += 1 }
+ result
+ end
+end