comparing the first half of a string to the second half

Discussion in 'Ruby' started by joe chesak, Sep 21, 2010.

  1. joe chesak

    joe chesak Guest

    [Note: parts of this message were removed to make it a legal post.]

    I want to be able to test a string, to see if the second half of the string
    is a duplicate of the first part of the string, and if so I just want the
    first half (or the second for that matter). I am using ruby-1.9.2.

    I can accomplish this with the following:

    str = 'Cyano The CatCyano The Cat'

    str = str[0..str.size/2-1] if str[0..str.size/2-1] == str[str.size/2..-1]
    => "Cyano The Cat"

    Is there a better way?

    Joe
     
    joe chesak, Sep 21, 2010
    #1
    1. Advertising

  2. I think a regular expression is probably the most succinct, cleanest
    approach for this:

    s =3D "Cyano The Cat" * 2
    if s =3D~ /^(.+)\1$/
    puts $1
    end

    On Tue, Sep 21, 2010 at 9:41 PM, joe chesak <> wrote:
    > I want to be able to test a string, to see if the second half of the stri=

    ng
    > is a duplicate of the first part of the string, and if so I just want the
    > first half (or the second for that matter). =C2=A0I am using =C2=A0ruby-1=

    9.2.
    >
    > I can accomplish this with the following:
    >
    > str =3D 'Cyano The CatCyano The Cat'
    >
    > str =3D str[0..str.size/2-1] if str[0..str.size/2-1] =3D=3D str[str.size/=

    2..-1]
    > =C2=A0=3D> "Cyano The Cat"
    >
    > Is there a better way?
    >
    > Joe
    >
     
    Adam Prescott, Sep 21, 2010
    #2
    1. Advertising

  3. joe chesak

    Xavier Noria Guest

    On Tue, Sep 21, 2010 at 10:55 PM, Adam Prescott <> wrot=
    e:

    > I think a regular expression is probably the most succinct, cleanest
    > approach for this:
    >
    > s =3D "Cyano The Cat" * 2
    > if s =3D~ /^(.+)\1$/
    > =C2=A0puts $1
    > end


    Clever, but almost: "aa\n" passes and it shouldn't.

    For an arbitrary string you want \A and \z (and /m).
     
    Xavier Noria, Sep 21, 2010
    #3
  4. On Sep 21, 2010, at 4:41 PM, joe chesak wrote:

    > I want to be able to test a string, to see if the second half of the
    > string
    > is a duplicate of the first part of the string, and if so I just
    > want the
    > first half (or the second for that matter). I am using ruby-1.9.2.
    >
    > I can accomplish this with the following:
    >
    > str = 'Cyano The CatCyano The Cat'
    >
    > str = str[0..str.size/2-1] if str[0..str.size/2-1] == str[str.size/
    > 2..-1]
    > => "Cyano The Cat"
    >
    > Is there a better way?
    >
    > Joe



    irb> str = 'Cyano The CatCyano The Cat'
    => "Cyano The CatCyano The Cat"
    irb> str =~ /\A(.*)\1\z/
    => 0
    irb> $1
    => "Cyano The Cat"

    irb> str = "I am not a twinNeither am I"
    => "I am not a twinNeither am I"
    irb> str =~ /\A(.*)\1\z/
    => nil

    -Rob

    Rob Biedenharn
    http://AgileConsultingLLC.com/
    http://GaslightSoftware.com/
     
    Rob Biedenharn, Sep 21, 2010
    #4
  5. joe chesak

    Steel Steel Guest

    joe chesak wrote:
    > I want to be able to test a string, to see if the second half of the
    > string
    > is a duplicate of the first part of the string, and if so I just want
    > the
    > first half (or the second for that matter). I am using ruby-1.9.2.
    >
    > I can accomplish this with the following:
    >
    > str = 'Cyano The CatCyano The Cat'
    >
    > str = str[0..str.size/2-1] if str[0..str.size/2-1] ==
    > str[str.size/2..-1]
    > => "Cyano The Cat"
    >
    > Is there a better way?
    >
    > Joe


    str = 'Cyano The CatCyano The Cat'
    o,t=a.each_slice(a.size/2).map{|x|x}
    str = o if t==o
    --
    Posted via http://www.ruby-forum.com/.
     
    Steel Steel, Sep 22, 2010
    #5
  6. joe chesak

    Steel Steel Guest

    joe chesak wrote:
    > I want to be able to test a string, to see if the second half of the
    > string
    > is a duplicate of the first part of the string, and if so I just want
    > the
    > first half (or the second for that matter). I am using ruby-1.9.2.
    >
    > I can accomplish this with the following:
    >
    > str = 'Cyano The CatCyano The Cat'
    >
    > str = str[0..str.size/2-1] if str[0..str.size/2-1] ==
    > str[str.size/2..-1]
    > => "Cyano The Cat"
    >
    > Is there a better way?
    >
    > Joe


    str = 'Cyano The CatCyano The Cat'
    o,t=str.chars.to_a.each_slice(str.size/2).map{|x|x}
    str = o if t==o
    --
    Posted via http://www.ruby-forum.com/.
     
    Steel Steel, Sep 22, 2010
    #6
  7. joe chesak

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Sep 21, 2010 at 3:41 PM, joe chesak <> wrote:

    > I want to be able to test a string, to see if the second half of the string
    > is a duplicate of the first part of the string, and if so I just want the
    > first half (or the second for that matter). I am using ruby-1.9.2.
    >
    > I can accomplish this with the following:
    >
    > str = 'Cyano The CatCyano The Cat'
    >
    > str = str[0..str.size/2-1] if str[0..str.size/2-1] == str[str.size/2..-1]
    > => "Cyano The Cat"
    >
    > Is there a better way?
    >
    > Joe
    >


    Instead of subtracting 1 from the right side of the range, you can just use
    3 dots.

    str = 'Cyano The CatCyano The Cat'
    half = str.size / 2
    first = str[0...half]
    last = str[half..-1]
    str = first if first == last
    p str
     
    Josh Cheek, Sep 22, 2010
    #7
  8. joe chesak

    (r.*n){2} Guest

    On Sep 21, 11:24 pm, Josh Cheek <> wrote:
    > [Note:  parts of this message were removed to make it a legal post.]
    >
    >
    >
    >
    >
    > On Tue, Sep 21, 2010 at 3:41 PM, joe chesak <> wrote:
    > > I want to be able to test a string, to see if the second half of the string
    > > is a duplicate of the first part of the string, and if so I just want the
    > > first half (or the second for that matter).  I am using  ruby-1.9.2..

    >
    > > I can accomplish this with the following:

    >
    > > str = 'Cyano The CatCyano The Cat'

    >
    > > str = str[0..str.size/2-1] if str[0..str.size/2-1] == str[str.size/2..-1]
    > >  => "Cyano The Cat"

    >
    > > Is there a better way?

    >
    > > Joe

    >
    > Instead of subtracting 1 from the right side of the range, you can just use
    > 3 dots.
    >
    > str   = 'Cyano The CatCyano The Cat'
    > half  = str.size / 2
    > first = str[0...half]
    > last  = str[half..-1]
    > str   = first if first == last
    > p str


    x = s.size
    y = x / 2
    r = x % 2
    puts "#{s[0..y-1]}" if ( r == 0) && (s[0..y-1] == s[y..-1])
     
    (r.*n){2}, Sep 23, 2010
    #8
    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. Daniel Walzenbach
    Replies:
    7
    Views:
    619
    Steven Cheng[MSFT]
    Feb 10, 2004
  2. trint
    Replies:
    4
    Views:
    721
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Sep 11, 2007
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,627
    James Kanze
    Oct 8, 2009
  4. Mike Ballard
    Replies:
    6
    Views:
    279
    Dr.Ruud
    Nov 15, 2005
  5. yelipolok
    Replies:
    4
    Views:
    282
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page