简体   繁体   中英

What is the best way to split a string to get all the substrings by Ruby?

For example, the words "stack", I want to get an array like:

['s', 'st', 'sta', ... 'stack', 't', 'ta', ... , 'c', 'ck', 'k']

I did this by such code:

def split_word(str)
  result = []
  chas = str.split("")
  len = chas.size
  (0..len-1).each do |i|
    (i..len-1).each do |j|
      result.push(chas[i..j].join)
    end
  end
  result.uniq
end

Is there better and clean way to do that? Thanks.

def split_word s
  (0..s.length).inject([]){|ai,i|
    (1..s.length - i).inject(ai){|aj,j|
      aj << s[i,j]
    }
  }.uniq
end

And you can also consider using Set instead of Array for the result.

PS: Here's another idea, based on array product:

def split_word s
  indices = (0...s.length).to_a
  indices.product(indices).reject{|i,j| i > j}.map{|i,j| s[i..j]}.uniq
end

I'd write:

def split_word(s)
  0.upto(s.length - 1).flat_map do |start| 
    1.upto(s.length - start).map do |length| 
      s[start, length]
    end
  end.uniq
end

groups = split_word("stack")
# ["s", "st", "sta", "stac", "stack", "t", "ta", "tac", "tack", "a", "ac", "ack", "c", "ck", "k"]

It's usually more clear and more compact to use map (functional) instead of the pattern init empty + each + append + return (imperative).

def substrings(str)
  output = []
  (0...str.length).each do |i|
    (i...str.length).each do |j|
      output << str[i..j]
    end
  end
  output
end

this is just a cleaned up version of your method and it works with less steps =)

Don't think so.

Here's my attempted version:

def split_word(str)
  length = str.length - 1
  [].tap do |result|
    0.upto(length) do |i|
      length.downto(i) do |j|
        substring = str[i..j]
        result << substring unless result.include?(substring)
      end
    end
  end
end
def substrings(str)
  (0...str.length).map do |i|
     (i...str.length).each { |j| str[i..j]}
  end
end

Just another way to do it, that reads a little clearer to me.

Here is the recursive way to get all the possible sub strings.

def substrings str
  return [] if str.size < 1
  ((0..str.size-1).map do |pos|
    str[0..pos]
  end) + substrings(str[1..])
end

Way later, but this is what I got from reformatting your code a bit.

def substrings(string)
  siz = string.length
  answer = []

  (0..siz-1).each do |n|
    (n..siz-1).each do |i|
      answer << string[n..i]
    end
  end
  answer
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM