Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > substrings

Reply
Thread Tools

substrings

 
 
Ed Redman
Guest
Posts: n/a
 
      01-19-2006
I am struggling with the following.
Given a sting of a given length example string = 'bkerhcno'
give all the subsets of string of length 4

I don't need all the permutations just the subsets.
I can use the slice method to give me some subsets i.e
s1 = string.slice(0,4)
s2 = string.slice(1,4) etc.
Just how do I generate all the possible combination of 4 characters.

By analagy, would like to extend to finding all combination of length 5,6,
and 7.

Hope someone can help.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-19-2006
Ed Redman wrote:
> I am struggling with the following.
> Given a sting of a given length example string = 'bkerhcno'
> give all the subsets of string of length 4
>
> I don't need all the permutations just the subsets.
> I can use the slice method to give me some subsets i.e
> s1 = string.slice(0,4)
> s2 = string.slice(1,4) etc.
> Just how do I generate all the possible combination of 4 characters.
>
> By analagy, would like to extend to finding all combination of length
> 5,6, and 7.
>
> Hope someone can help.


def sub_strings(s,len)
(s.length - len).times do |idx|
yield s[idx,len]
end
end

>> sub_strings( 'bkerhcno', 4 ) {|s| p s}

"bker"
"kerh"
"erhc"
"rhcn"
"hcno"
=> 0..4

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex: How to extract substrings? Markus Dehmann Java 2 12-10-2005 06:35 AM
Compile typedef-ed substrings mrstephengross C++ 7 04-06-2005 06:33 PM
Using regular expressions to extract substrings from files Timothy Hume Python 2 09-10-2004 03:08 AM
Binary files, substrings and (un)packing. Leandro Pardini Perl 1 10-27-2003 07:57 PM
C++: Comparing Substrings In An Array Ying Yang C++ 9 09-26-2003 11:52 PM



Advertisments