Най-дълга последователност от символи

Краен срок
29.11.2013 12:00

Срокът за предаване на решения е отминал

Най-дългa последователност от символи

Нали помните онова за Monkey Patching-а…? :-)

Дефинирайте метод String#longest_sequence, който връща масив от тези символи в низа, които имат най-дълги последователности в него. „Последователност“ ще рече непрекъсната поредица от един и същ символ, например низът 'aaAA_32222BBB_)))' има 8 последователности: ['aa', 'AA', '_', '3', '2222', 'BBB', '_', ')))'], като най-дългата е '2222', тоест 'aaAA_32222BBB_)))'.longest_sequence трябва да върне ['2'].

Ако има повече от един отговор, то връщате всички, като наредбата на резултатите не е от значение, например: 'aaabbb23ccc'.longest_sequence може да върне която и да е пермутация на масива ['a', 'b', 'c'].

Забележка: Във върнатия масив трябва да има низове (String) с дължина 1, а не символи (Symbol).

Примери:

'aab'.longest_sequence                 # => ['а']
'abba'.longest_sequence                # => ['b']
'aaaaabbbbbddacccc'.longest_sequence   # => ['a', 'b'] (Може и ['b', 'a'])
'.....??????&gh'.longest_sequence      # => ['?']

P.S. Примерните тестове от вас. :-) Вярваме, че можете да си ги напишете сами.

Решения

Петко Борджуков
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Петко Борджуков
class String
def longest_sequence
return [] if empty?
scan(/(.)(\1*)/).map(&:join).group_by(&:length).max.last.map(&:chr).uniq
end
end
....

Finished in 0.00648 seconds
4 examples, 0 failures
Георги Ангелов
  • Некоректно
  • 3 успешни тест(а)
  • 1 неуспешни тест(а)
Георги Ангелов
class String
def longest_sequence
sequences = scan(/(?<result>(?<symbol>.)\k<symbol>*)/m)
maximum_length = sequences.map(&:first).map(&:length).max
sequences.select { |sequence| sequence.first.length == maximum_length }.map(&:last)
end
end
...F

Failures:

  1) #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
     Failure/Error: 'aaabbbaaa'.longest_sequence.should =~ ['a', 'b']
       expected collection contained:  ["a", "b"]
       actual collection contained:    ["a", "a", "b"]
       the extra elements were:        ["a"]
     # /tmp/d20131202-25737-1bi4pp/spec.rb:27: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.00642 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131202-25737-1bi4pp/spec.rb:26 # #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
Лилия Любенова
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Лилия Любенова
class String
def longest_sequence
sequences = self.scan(/((.)\2*)/).map(&:first)
max_length = sequences.map(&:length).max
sequences.select {|sequence| sequence.length == max_length}.map(&:squeeze).uniq
end
end
....

Finished in 0.00636 seconds
4 examples, 0 failures
Иван Капукаранов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Капукаранов
class String
def longest_sequence
string = self
index = string.length
sequences = {}
while index != 0
matched_data = string[0, index].match /(.)\1*\z/
if sequences[$1]
sequences[$1] = matched_data[0].length if sequences[$1] < matched_data[0].length
else
sequences[$1] = matched_data[0].length
end
index = string[0, index] =~ /(.)\1*\z/
end
sequences.keep_if { |key, value| value == sequences.values.max }.keys
end
end
....

Finished in 0.00691 seconds
4 examples, 0 failures
Емануела Моллова
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Емануела Моллова
class String
def longest_sequence
chunked = chars.chunk { |char| char }.sort_by { |item| item.last.size }
chunked.select { |item| item.last.size == chunked.last.last.size }.map(&:first).uniq
end
end
....

Finished in 0.00836 seconds
4 examples, 0 failures
Георги Шопов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Шопов
class String
def longest_sequence
matches = scan /((.)\2*)/
max_length = matches.map { |match| match[0].length }.max
matches.each_with_object([]) do |match, list|
if match[0].length == max_length and not list.member? match[1]
list << match[1]
end
end
end
end
....

Finished in 0.00629 seconds
4 examples, 0 failures
Мария Терзиева
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Мария Терзиева
class String
def longest_sequence
return [] if empty?
sequences = scan(/((.)\2*)/).map(&:first)
sequences.group_by(&:size).max.last.map { |string| string[0] }.uniq
end
end
....

Finished in 0.00664 seconds
4 examples, 0 failures
Калоян Калудов
  • Некоректно
  • 3 успешни тест(а)
  • 1 неуспешни тест(а)
Калоян Калудов
class String
def longest_sequence
parts = self.scan(/((.)\2*)/).map { |part| part.first }
parts = parts.sort_by { |part| -part.size }
parts = parts.find_all { |part| part.size == parts.first.size }
parts.map { |part| part[0] }
end
end
...F

Failures:

  1) #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
     Failure/Error: 'aaabbbaaa'.longest_sequence.should =~ ['a', 'b']
       expected collection contained:  ["a", "b"]
       actual collection contained:    ["a", "a", "b"]
       the extra elements were:        ["a"]
     # /tmp/d20131202-25737-j3tfsc/spec.rb:27: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.00651 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131202-25737-j3tfsc/spec.rb:26 # #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
Георги Пурнаров
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Пурнаров
class String
def longest_sequence
array,result=self.split(''),[]
unless array.empty?
hash,current_count,previuos=Hash.new { 0 },0,array[0]
array.each do |symbol|
if previuos == symbol
current_count=current_count+1
hash[symbol]=current_count>hash[symbol] ? current_count : hash[symbol]
else
current_count=1
hash[symbol]=current_count>hash[symbol] ? current_count : hash[symbol]
previuos=symbol
end
end
max_value=hash.max { |a,b| a.last <=> b.last }.last
hash.each {|key, value| result.push key if value==max_value}
end
result
end
end
....

Finished in 0.00671 seconds
4 examples, 0 failures
Петър Мазълов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Петър Мазълов
class String
def longest_sequence
string = self
sequences = []
while string != "" do
sequences << string.slice!(/(.)\1*/)
end
maximum = sequences.map { |i| i.size }.max
sequences.select { |i| i.size == maximum }.map{ |i| i[0] }.uniq
end
end
....

Finished in 0.00633 seconds
4 examples, 0 failures
Антонио Николов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Антонио Николов
class String
def longest_sequence
all_sequences = {}
from_index = -1
count = 0
max = 1
split("").each do |symbol|
from_index += 1
count = 0
split("").each_with_index do |rest, index|
if index < from_index
next
elsif symbol == rest
count += 1
all_sequences[symbol] = count if all_sequences[symbol] == nil or all_sequences[symbol] < count
max = count if count > max
else
max = count if count > max
break
end
end
end
max_sequence = []
all_sequences.each { |key, value| max_sequence << key if value == max }
max_sequence
end
end
....

Finished in 0.00961 seconds
4 examples, 0 failures
Росен Рачев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Росен Рачев
class String
def longest_sequence
seq = []
scan(/((.)+?(\2+)?)/) { |m| seq << m.first }
seq = seq.sort_by { |element| element.length }.reverse
seq.delete_if { |element| element.length < seq[0].length }.map { |element| element.slice 0 }.uniq
end
end
....

Finished in 0.00757 seconds
4 examples, 0 failures
Стефан Василев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Стефан Василев
class String
def longest_sequence
matches, self_value = [], self
each_char do |charecter|
matches << (%r{[#{charecter}]+}.match self_value).to_s unless matches.include? (%r{[#{charecter}]+}.match self_value).to_s
self_value = self_value.reverse.chop.reverse
end
matches.keep_if { |element| element.length == matches.map { |element| element = element.length }.max }.map! { |element| element = element[0] }
end
end
....

Finished in 0.01269 seconds
4 examples, 0 failures
Борислава Аладжова
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Борислава Аладжова
class String
def longest_sequence
return [] if empty?
most_frequent_chars = []
highest_frequency = 0
current_char = self[0]
remaining = self
until remaining.empty?
r = Regexp.quote "#{current_char}"
frequency = (/#{r}+/.match remaining).to_s.size
if frequency > highest_frequency
highest_frequency = frequency
most_frequent_chars = [current_char]
elsif frequency == highest_frequency
most_frequent_chars << current_char
end
remaining = $'
current_char = remaining[0]
end
most_frequent_chars.uniq
end
end
....

Finished in 0.00711 seconds
4 examples, 0 failures
Ангел Цанев
  • Некоректно
  • 0 успешни тест(а)
  • 4 неуспешни тест(а)
Ангел Цанев
class String
def longest_sequance
sequance_array = self.scan(/((.)\2*)/).map(&:first)
sequance_array.sort{ |x,y| y.size <=> x.size }
sequance_array.keep_if{ |sequance| sequance.size == sequance_array[0].size }
sequance_array.map{ |sequance| sequance[0] }.uniq
end
end
FFFF

Failures:

  1) #longest_sequence for an empty string should return an empty Array
     Failure/Error: ''.longest_sequence.should eq []
     NoMethodError:
       undefined method `longest_sequence' for "":String
     # /tmp/d20131202-25737-57t60i/spec.rb:4:in `block (3 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) #longest_sequence for a string with only one longest sequence should return only the symbol that it is comprised of
     Failure/Error: 'aab'.longest_sequence.should eq ['a']
     NoMethodError:
       undefined method `longest_sequence' for "aab":String
     # /tmp/d20131202-25737-57t60i/spec.rb:10:in `block (3 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) #longest_sequence for a string with multiple longest sequences should return an array containg the symbols they are comprised of
     Failure/Error: 'aabb'.longest_sequence.should =~ ['a','b']
     NoMethodError:
       undefined method `longest_sequence' for "aabb":String
     # /tmp/d20131202-25737-57t60i/spec.rb:19:in `block (3 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) #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
     Failure/Error: 'aaabbbaaa'.longest_sequence.should =~ ['a', 'b']
     NoMethodError:
       undefined method `longest_sequence' for "aaabbbaaa":String
     # /tmp/d20131202-25737-57t60i/spec.rb:27: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.00488 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20131202-25737-57t60i/spec.rb:3 # #longest_sequence for an empty string should return an empty Array
rspec /tmp/d20131202-25737-57t60i/spec.rb:9 # #longest_sequence for a string with only one longest sequence should return only the symbol that it is comprised of
rspec /tmp/d20131202-25737-57t60i/spec.rb:18 # #longest_sequence for a string with multiple longest sequences should return an array containg the symbols they are comprised of
rspec /tmp/d20131202-25737-57t60i/spec.rb:26 # #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
Кристиан Ташков
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиан Ташков
class String
def longest_sequence
max = 0
prev_char = '';
prev_length = 0;
sequences = chars.map do |char|
if prev_char == char
prev_length += 1
else
prev_length = 1
end
prev_char = char
max = prev_length if prev_length > max
{length: prev_length, char: char}
end
sequences.select { |sequence| sequence[:length] == max }
.map { |sequence| sequence[:char] }.uniq
end
end
....

Finished in 0.00627 seconds
4 examples, 0 failures
Илиян Бобев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Бобев
class String
def longest_sequence
groups = self.scan(/((.)\2*(?!\2))/).map { |group| group[0] }
groups.map { |group| group.chars.first unless group.size < groups.map(&:size).max }.compact.uniq
end
end
....

Finished in 0.00647 seconds
4 examples, 0 failures
Веселин Генадиев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Веселин Генадиев
class String
def longest_sequence
series = self.scan(/((.)\2*)/).map(&:first).sort_by{|x| -x.length}
series.select{|x| x.length == series[0].length}.map{|x| x[0]}.uniq
end
end
....

Finished in 0.00763 seconds
4 examples, 0 failures
Диан Николов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Диан Николов
class String
def longest_sequence
longestSequence = { "length" => 0, "sequences" => [] }
currentSequenceLength = 0
currentChar = ''
(0..length - 1).each do |i|
if self[i] != currentChar
if longestSequence["length"] == currentSequenceLength
longestSequence["sequences"] << currentChar if currentChar != ''
elsif longestSequence["length"] < currentSequenceLength
longestSequence["length"] = currentSequenceLength
longestSequence["sequences"] = [currentChar]
end
currentSequenceLength = 0
end
currentChar = self[i]
currentSequenceLength += 1
end
if longestSequence["length"] == currentSequenceLength
longestSequence["sequences"] << currentChar if currentChar != ''
else if longestSequence["length"] < currentSequenceLength
longestSequence["length"] = currentSequenceLength
longestSequence["sequences"] = [currentChar]
end
end
longestSequence["sequences"].uniq
end
end
....

Finished in 0.00769 seconds
4 examples, 0 failures
Георги Урумов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Урумов
class String
def longest_sequence()
match_str = self
max_l = 0
match_datas = []
while match_str.length > 0 do
md = match_str.match(/(.)\1{0,}/)
break unless md
if md[0].length > max_l
max_l = md[0].length
match_datas = []
match_datas << md[1]
elsif md[0].length == max_l
match_datas << md[1]
end
match_str = md.post_match
end
match_datas.uniq
end
end
....

Finished in 0.00631 seconds
4 examples, 0 failures
Емануил Иванов
  • Некоректно
  • 0 успешни тест(а)
  • 4 неуспешни тест(а)
Емануил Иванов
class String
def longest_sequence
array=self
output=Array.new
regexp=/(.)\1{1,}/
while (array.match regexp)
substr=array.match(regexp).to_s
array=array.sub(substr,"")
output.push(substr)
end
output.group_by(&:size).max.last.to_s.split(regexp)
end
end
FFFF

Failures:

  1) #longest_sequence for an empty string should return an empty Array
     Failure/Error: ''.longest_sequence.should eq []
     NoMethodError:
       undefined method `last' for nil:NilClass
     # /tmp/d20131202-25737-h0oriu/solution.rb:11:in `longest_sequence'
     # /tmp/d20131202-25737-h0oriu/spec.rb:4:in `block (3 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) #longest_sequence for a string with only one longest sequence should return only the symbol that it is comprised of
     Failure/Error: 'aab'.longest_sequence.should eq ['a']
       
       expected: ["a"]
            got: ["[\"", "a", "\"]"]
       
       (compared using ==)
     # /tmp/d20131202-25737-h0oriu/spec.rb:10:in `block (3 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) #longest_sequence for a string with multiple longest sequences should return an array containg the symbols they are comprised of
     Failure/Error: 'aabb'.longest_sequence.should =~ ['a','b']
       expected collection contained:  ["a", "b"]
       actual collection contained:    ["\", \"", "\"]", "[\"", "a", "b"]
       the extra elements were:        ["\", \"", "\"]", "[\""]
     # /tmp/d20131202-25737-h0oriu/spec.rb:19:in `block (3 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) #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
     Failure/Error: 'aaabbbaaa'.longest_sequence.should =~ ['a', 'b']
       expected collection contained:  ["a", "b"]
       actual collection contained:    ["\", \"", "\", \"", "\"]", "[\"", "a", "a", "b"]
       the extra elements were:        ["\", \"", "\", \"", "\"]", "[\"", "a"]
     # /tmp/d20131202-25737-h0oriu/spec.rb:27: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.0067 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20131202-25737-h0oriu/spec.rb:3 # #longest_sequence for an empty string should return an empty Array
rspec /tmp/d20131202-25737-h0oriu/spec.rb:9 # #longest_sequence for a string with only one longest sequence should return only the symbol that it is comprised of
rspec /tmp/d20131202-25737-h0oriu/spec.rb:18 # #longest_sequence for a string with multiple longest sequences should return an array containg the symbols they are comprised of
rspec /tmp/d20131202-25737-h0oriu/spec.rb:26 # #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
Никола Ненков
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Никола Ненков
class String
def longest_sequence
return [] if empty?
sequences = Hash[scan /((.)\2*)/]
max_length = sequences.keys.max_by(&:length).length
sequences.reject { |key, value| key.length < max_length }.values
end
end
....

Finished in 0.00651 seconds
4 examples, 0 failures
Давид Петров
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Давид Петров
class String
def longest_sequence
if empty?
[]
else
splited_by_substrings = chars.chunk{|e| e}.map{|e| e[1].join}.compact
max_length = splited_by_substrings.max_by {|a| a.length }.length
splited_by_substrings.select {|e| e.length==max_length }.map {|x| x[0]}.uniq
end
end
end
....

Finished in 0.0067 seconds
4 examples, 0 failures
Милен Мавров
  • Некоректно
  • 2 успешни тест(а)
  • 2 неуспешни тест(а)
Милен Мавров
class String
def longest_sequence
hash = {}
help_me(self, hash)
hash.select { |k, v| v == hash.values.max }.keys
end
def help_me(target, hash)
return hash if target.empty?
first_letter = target.chr
sequence = /#{first_letter}+/.match(target).to_s
if hash.has_key?(sequence[0])
hash[sequence[0]] = [hash[sequence[0]], sequence.length].max
else
hash[sequence[0]] = sequence.length
end
target[0...sequence.length] = ''
help_me(target, hash)
end
end
.FF.

Failures:

  1) #longest_sequence for a string with only one longest sequence should return only the symbol that it is comprised of
     Failure/Error: '.....??????&gh'.longest_sequence.should eq ['?']
       
       expected: ["?"]
            got: ["."]
       
       (compared using ==)
     # /tmp/d20131202-25737-kx8n7g/spec.rb:13:in `block (3 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) #longest_sequence for a string with multiple longest sequences should return an array containg the symbols they are comprised of
     Failure/Error: '.....??????&ggggggh'.longest_sequence.should =~ ['g', '?']
       expected collection contained:  ["?", "g"]
       actual collection contained:    ["."]
       the missing elements were:      ["?", "g"]
       the extra elements were:        ["."]
     # /tmp/d20131202-25737-kx8n7g/spec.rb:22:in `block (3 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.00767 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20131202-25737-kx8n7g/spec.rb:9 # #longest_sequence for a string with only one longest sequence should return only the symbol that it is comprised of
rspec /tmp/d20131202-25737-kx8n7g/spec.rb:18 # #longest_sequence for a string with multiple longest sequences should return an array containg the symbols they are comprised of
Георги Кръстев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Кръстев
class String
def longest_sequence
sequences = scan(/(.)(\1*)/).map(&:join).group_by(&:length)
sequences.empty? ? [] : sequences.max.last.map(&:squeeze).uniq
end
end
....

Finished in 0.00665 seconds
4 examples, 0 failures
Моника Димитрова
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Моника Димитрова
class String
def longest_sequence
sequences = {}
sequence_counter sequences, self
sequences.select{ |key, value| value == sequences.values.max }.keys.map { |key| key.squeeze }
end
def sequence_counter(sequences, string)
matched = /(.)\1*/.match string
if matched
sequences[matched.to_s] = matched.to_s.size
sequence_counter sequences, matched.post_match
end
end
end
....

Finished in 0.00663 seconds
4 examples, 0 failures
Николай Хубанов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Хубанов
class String
def initial
self[0,1]
end
def longest_sequence
return [] if empty?
scan(/((.)\2*)/).map(&:first).group_by(&:size).max.last.map(&:initial).uniq
end
end
....

Finished in 0.00637 seconds
4 examples, 0 failures
Цветан Иванов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Цветан Иванов
class String
def longest_sequence
return [] if to_s.empty?
commnon_sequences = split_common_sequence
sorted_common_sequences = commnon_sequences.sort { |first, second| second.length <=> first.length }
longest_common_sequences = get_longest_sequences(sorted_common_sequences)
get_longest_sequences_symbols(longest_common_sequences).uniq
end
def get_longest_sequences_symbols(longest_common_sequences)
longest_common_sequences.map { |sequence| sequence[0] }
end
def get_longest_sequences(commnon_sequences)
biggest_sequence_length = commnon_sequences[0].length
commnon_sequences.delete_if { |sequence| sequence.length != biggest_sequence_length }
end
def split_common_sequence
last_common_simbol_index = -1
chars.each_with_object([]) do |symbol, common_sequence|
current_common_sequece = common_sequence[last_common_simbol_index]
if (current_common_sequece == nil or current_common_sequece.squeeze) != symbol
last_common_simbol_index += 1
common_sequence[last_common_simbol_index] = symbol
else
common_sequence[last_common_simbol_index] += symbol
end
end
end
end
....

Finished in 0.00649 seconds
4 examples, 0 failures
Моника Илиева
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Моника Илиева
class String
def longest_sequence
return [] if empty?
scan(/((.)\2*)/).group_by do |repeated_elements, elements|
repeated_elements.length
end.max.last.map(&:last).uniq
end
end
....

Finished in 0.00652 seconds
4 examples, 0 failures
Ангел Венчев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Венчев
class String
def longest_sequence
lengths = chars.uniq.map do |i|
[i, split(/[^#{i}]/).map(&:length).max]
end
lengths.select { |i| i.last == lengths.map(&:last).max }.map(&:first)
end
end
....

Finished in 0.00761 seconds
4 examples, 0 failures
Мария Митева
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Мария Митева
class String
def longest_sequence
return [] if self.size == 0
sorted_by_count = self.scan(/((.)\2*)/).sort_by { |obj| -obj[0].size }
size_of_longest_match = sorted_by_count[0][0].size
sorted_by_count.select { |a| size_of_longest_match == a[0].size }.map { |a| a[1] }.uniq
end
end
....

Finished in 0.00632 seconds
4 examples, 0 failures
Красимира Божанова
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Красимира Божанова
class String
def longest_sequence
return [] if empty?
all_sequences = scan /((.)\2*)/m
all_sequences.map(&:first).uniq.group_by(&:length).max[1].map(&:chr)
end
end
....

Finished in 0.00825 seconds
4 examples, 0 failures
Георги Гърдев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Гърдев
class String
def longest_sequence
subsequences = Hash.new { |hash, key| hash[key] = [] }
chars.chunk { |char| char }.map do |_, subsequence|
subsequences[subsequence.length] << subsequence.first
end
subsequences[subsequences.keys.max].uniq
end
end
....

Finished in 0.00696 seconds
4 examples, 0 failures
Десислав Илиев
  • Некоректно
  • 3 успешни тест(а)
  • 1 неуспешни тест(а)
Десислав Илиев
class String
def longest_sequence
list_of_sequences = []
longest_sequences = []
text = self
top_length = 0
while text != ""
if (text =~ /((.)\2*)(.*)/)
list_of_sequences << text.gsub(/((.)\2*)(.*)/, '\1')
if list_of_sequences.last.size > top_length then top_length = list_of_sequences.last.size end
text.gsub!(/((.)\2*)(.*)/, '\3')
end
end
list_of_sequences.each do | sequence |
if sequence.size == top_length
longest_sequences << sequence[0]
end
end
longest_sequences
end
end
...F

Failures:

  1) #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
     Failure/Error: 'aaabbbaaa'.longest_sequence.should =~ ['a', 'b']
       expected collection contained:  ["a", "b"]
       actual collection contained:    ["a", "a", "b"]
       the extra elements were:        ["a"]
     # /tmp/d20131202-25737-19uicbr/spec.rb:27: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.00824 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131202-25737-19uicbr/spec.rb:26 # #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
Христо Хърсев
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Христо Хърсев
class String
def longest_sequence
begin
gsub(/(.)\1*/).to_a.group_by(&:size).max.last.map(&:chr).uniq
rescue NoMethodError
[]
end
end
end
....

Finished in 0.00651 seconds
4 examples, 0 failures
Кристиян Азманов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиян Азманов
class String
def longest_sequence
hash_as_buffer = {}
utf8_encoded = self.encode('utf-8')
while (utf8_encoded != "") do
head = utf8_encoded[0]
match = match_template(head, utf8_encoded)
populate_hash(match, hash_as_buffer)
utf8_encoded = utf8_encoded.sub!(match,"")
end
return get_hash_selection(hash_as_buffer)
end
def match_template(head, utf8_encoded)
if head=="]" then
return (/[\]]+/.match(utf8_encoded)).to_s
elsif head=="["
return (/[\[]+/.match(utf8_encoded)).to_s
elsif head=="^"
return (/[\^]+/.match(utf8_encoded)).to_s
else
return (/[#{head}]+/.match(utf8_encoded)).to_s
end
end
def populate_hash(new_value, hash_container)
key = new_value[0].ord
subseq_length = new_value.length
if !hash_container.has_key?(key) or hash_container[key] < new_value.length then
hash_container[key] = subseq_length
end
end
def get_hash_selection(hash_as_buffer)
filtered_only_max_values = []
rendered_utf8_codes = []
max_value_for_hash_value = hash_as_buffer.values.max
hash_as_buffer.each do |key, value|
filtered_only_max_values << key if value == max_value_for_hash_value
end
filtered_only_max_values.each {|utf8_code| rendered_utf8_codes << [utf8_code].pack('U')}
return rendered_utf8_codes
end
end
....

Finished in 0.00831 seconds
4 examples, 0 failures
Кристиян Кисимов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиян Кисимов
class String
def group
chars_array = chars
grouped_symbol = []
until chars_array == []
grouped_symbol << chars_array.take_while { |symbol| symbol == chars_array[0] }
chars_array = chars_array.drop_while { |symbol| symbol == chars_array[0] }
end
grouped_symbol
end
def longest_sequence
groups_lengths = group.map { |group| group.length }
group.select { |group| group.length == groups_lengths.max }.map { |group| group[0] }.uniq
end
end
....

Finished in 0.0211 seconds
4 examples, 0 failures
Александър Попов
  • Коректно
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Попов
class String
def longest_sequence
longest = {}
current = 1
chars.each_with_index do |char, i|
if chars[i] == chars[i+1]
current += 1
else
longest[char] = current if not longest[char] or longest[char] < current
current = 1
end
end
longest.select { |k, v| v == longest.values.max }.keys
end
end
....

Finished in 0.00729 seconds
4 examples, 0 failures
Димитър Бонев
  • Некоректно
  • 3 успешни тест(а)
  • 1 неуспешни тест(а)
Димитър Бонев
String.class_eval do
def longest_sequence
sequence_sizes = scan(/((.)\2*)/m).group_by { |match_data| match_data.first.size }
longest_sequences = sequence_sizes[sequence_sizes.keys.max] || []
longest_sequences.map &:last
end
end
...F

Failures:

  1) #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once
     Failure/Error: 'aaabbbaaa'.longest_sequence.should =~ ['a', 'b']
       expected collection contained:  ["a", "b"]
       actual collection contained:    ["a", "a", "b"]
       the extra elements were:        ["a"]
     # /tmp/d20131202-25737-q56tik/spec.rb:27: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.00671 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20131202-25737-q56tik/spec.rb:26 # #longest_sequence for a string with multiple longest sequences comprised of the same symbol should return an array that contains the symbol only once