I am trying to make an array of all different combinations of a string.
The minimum amount of chars in the output string must be two.
# Example string. But what if string is n chars long though?
s = "string"
What I tried:
new_array = s.size.times.map{|i| s.split("",i)}.map do |a|
if a.size == 1
a
elsif a.size != s.size and a.size > 2
[a[0..-2].join(''),a[-1]]
end
end
new_array.compact.sort_by{|a| a.first.size }
Expected output:
[['st', 'ring',],['str', 'ing'],['stri', 'ng']['string'], ['st',
‘ri’, ‘ng’]]
The code above works until I get to: s.length > 5.
I only need the consecutive forward groups of chars output. Each
subarray should contain all the letters of s
broken in different ways.
I don’t need ['gnirts']
, ['gn', irts']
, or something mixed up like
['stngri']
. When it’s over seven chars, it is tricky. Here’s a map of
the different combinations, assuming the string s
is a minimum length
of two:
For a 2-letter string: ['first 2 letters']
3-letter string: ['first 3 letters']
4-letter string: [['first 2', 'third and fourth'], ['first four']]
5-letter string: [['first 2, 'thrid fourth and fifth'],['first 3,
fourth and fifth’],[‘all five’]]
and etc.
I’m wondering if what I’m asking is possible.
There is a Fibonacci sequence in here. There is only 1 solution for 2
or 3- letter strings. 2 solutions for 4-char. 3 for 5-char. 5 for
6-char, and 8 for 7-char.