substrings

Discussion in 'Ruby' started by Ed Redman, Jan 19, 2006.

  1. Ed Redman

    Ed Redman Guest

    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.
    Ed Redman, Jan 19, 2006
    #1
    1. Advertising

  2. 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
    Robert Klemme, Jan 19, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leandro Pardini

    Binary files, substrings and (un)packing.

    Leandro Pardini, Oct 25, 2003, in forum: Perl
    Replies:
    1
    Views:
    586
    Jim Gibson
    Oct 27, 2003
  2. Markus Dehmann

    regex: How to extract substrings?

    Markus Dehmann, Dec 10, 2005, in forum: Java
    Replies:
    2
    Views:
    793
    IchBin
    Dec 10, 2005
  3. Ying Yang
    Replies:
    9
    Views:
    632
    Mike Wahler
    Sep 27, 2003
  4. mrstephengross

    Compile typedef-ed substrings

    mrstephengross, Apr 4, 2005, in forum: C++
    Replies:
    7
    Views:
    396
  5. Timothy Hume
    Replies:
    2
    Views:
    489
    Brian Szmyd
    Sep 10, 2004
Loading...

Share This Page