Решение на Първа задача от Явор Лилянов

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

Към профила на Явор Лилянов

Резултати

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

Код

#Prechat li komentarite da znam dali da slagam drugiq put :)
class Array
def to_hash()
temp={} # syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
j=0
while j<=self.length-1
i=0
while i<self.length-1
temp[self[j][i]]=self[j][i+1] #kluchut e vseki element[j][i], a stoinostta vseki element[j][i+1]
i+=2
end
j+=1
end
return temp # vrushtame polucheniq hesh
end
#--------------------------------------------------------------
def index_by
temp={} # syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
i=0
while i<self.length
current=self[i] #vzimame i-tiq element ot masiva 'self'
temp[yield current]=self[i] # kluchyt e i-tiq element preraboten ot bloka, a stoinostta e samiqt i-ti element ot masiva 'self'
i+=1
end
return temp # vrushtame polucheniq hesh
end
#--------------------------------------------------------------
def subarray_count(subarray)
i=0;j=0;sum=0;
while j<self.length
temp=[]
i=j
while i<subarray.length+j
temp+=[self[i]] #dobavqme subarray.length na broi elementi ot masiva 'self' v masiva 'temp' kato zapochvame ot index j
i+=1
end
if(temp==subarray) #ako masiva 'a' e syshtiq kato 'subarray' uvelichavame broq sreshtaniq s 1
sum+=1
end
j+=1 # premestvame indexa j s 1
end
return sum # vrushtame polucheniq broi sreshtaniq
end
#--------------------------------------------------------------
def occurences_count
temp={}# syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
i=0
while i<self.length
current=self.subarray_count([self[i]]) #vzimame broq sreshtaniq na i-tiq element v 'self' s metoda subarray_count i go zapazvame v current
temp[self[i]]=current #kluchyt e i-tiq element na 'self', a stoinostta e current
i+=1
end
return temp # vrushtame polucheniq hesh
end
end
#---------------------------------------------------------------
#izprobvane na metodite
puts 'Metoda #to_hash:'
puts [[:one, 1], [:two, 2]].to_hash
puts [[1, 2], [3, 4]].to_hash
puts [[1, 2], [1, 3]].to_hash
puts [].to_hash
puts 'Metoda #index_by'
puts ['John Coltrane', 'Miles Davis'].index_by { |name| name.split(' ').last }
puts %w[foo larodi bar].index_by { |s| s.length }
puts 'Metoda #subarray_count'
puts [1, 2, 3, 2, 3, 1].subarray_count([2, 3])
puts [1, 2, 2, 2, 2, 1].subarray_count([2, 2])
puts [1, 1, 2, 2, 1, 1, 1].subarray_count([1, 1])
puts 'Metoda #occurences_count'
puts [:foo, :bar, :foo].occurences_count
puts %w[a a b c b a].occurences_count

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

Metoda #to_hash:
{:one=>1, :two=>2}
{1=>2, 3=>4}
{1=>3}
{}
Metoda #index_by
{"Coltrane"=>"John Coltrane", "Davis"=>"Miles Davis"}
{3=>"bar", 6=>"larodi"}
Metoda #subarray_count
2
3
3
Metoda #occurences_count
{:foo=>2, :bar=>1}
{"a"=>3, "b"=>2, "c"=>1}
..............F.F

Failures:

  1) Array#occurences_count returns a hash that defaults to 0 when the key is not present
     Failure/Error: [].occurences_count[:something].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-1w7ldx0/spec.rb:74: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#occurences_count returns a hash, that does not change when indexed with a non-occuring element
     Failure/Error: hash['b'].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-1w7ldx0/spec.rb:84: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.01526 seconds
17 examples, 2 failures

Failed examples:

rspec /tmp/d20111025-2903-1w7ldx0/spec.rb:73 # Array#occurences_count returns a hash that defaults to 0 when the key is not present
rspec /tmp/d20111025-2903-1w7ldx0/spec.rb:81 # Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element

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

Явор обнови решението на 24.10.2011 16:19 (преди около 13 години)

+#Prechat li komentarite da znam dali da slagam drugiq put :)
+class Array
+ def to_hash()
+ temp={} # syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
+ j=0
+ while j<=self.length-1
+ i=0
+ while i<self.length-1
+ temp[self[j][i]]=self[j][i+1] #kluchut e vseki element[j][i], a stoinostta vseki element[j][i+1]
+ i+=2
+ end
+ j+=1
+ end
+ return temp # vrushtame polucheniq hesh
+ end
+#--------------------------------------------------------------
+ def index_by
+ temp={} # syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
+ i=0
+ while i<self.length
+ current=self[i] #vzimame i-tiq element ot masiva 'self'
+ temp[yield current]=self[i] # kluchyt e i-tiq element preraboten ot bloka, a stoinostta e samiqt i-ti element ot masiva 'self'
+ i+=1
+ end
+ return temp # vrushtame polucheniq hesh
+ end
+#--------------------------------------------------------------
+ def subarray_count(subarray)
+ i=0;j=0;sum=0;
+ while j<self.length
+ temp=[]
+ i=j
+ while i<subarray.length+j
+ temp+=[self[i]] #dobavqme subarray.length na broi elementi ot masiva 'self' v masiva 'temp' kato zapochvame ot index j
+ i+=1
+ end
+ if(temp==subarray) #ako masiva 'a' e syshtiq kato 'subarray' uvelichavame broq sreshtaniq s 1
+ sum+=1
+ end
+ j+=1 # premestvame indexa j s 1
+ end
+ return sum # vrushtame polucheniq broi sreshtaniq
+ end
+#--------------------------------------------------------------
+ def occurences_count
+ temp={}# syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
+ i=0
+ while i<self.length
+ current=self.subarray_count([self[i]]) #vzimame broq sreshtaniq na i-tiq element v 'self' s metoda subarray_count i go zapazvame v current
+ temp[self[i]]=current #kluchyt e i-tiq element na 'self', a stoinostta e current
+ i+=1
+ end
+ return temp # vrushtame polucheniq hesh
+ end
+end
+
+#---------------------------------------------------------------
+#izprobvane na metodite
+puts 'Metoda #to_hash:'
+puts [[:one, 1], [:two, 2]].to_hash
+puts [[1, 2], [3, 4]].to_hash
+puts [[1, 2], [1, 3]].to_hash
+puts [].to_hash
+
+puts 'Metoda #index_by'
+puts ['John Coltrane', 'Miles Davis'].index_by { |name| name.split(' ').last }
+puts %w[foo larodi bar].index_by { |s| s.length }
+
+puts 'Metoda #subarray_count'
+puts [1, 2, 3, 2, 3, 1].subarray_count([2, 3])
+puts [1, 2, 2, 2, 2, 1].subarray_count([2, 2])
+puts [1, 1, 2, 2, 1, 1, 1].subarray_count([1, 1])
+
+puts 'Metoda #occurences_count'
+puts [:foo, :bar, :foo].occurences_count
+puts %w[a a b c b a].occurences_count