[Q] how to do insertion and smooth traversal simultaneously on array

Discussion in 'Ruby' started by SHIGETOMI, Takuhiko, Sep 6, 2005.

  1. greetings, guys.

    this is a pseudo-ruby code which shows what i am expecting.

    a = [ 0, 1, 2, 3, 4, 5 ]
    a.each_with_index do |x, i|
    puts "[#{i}] : #{x}"
    if i % 2 == 0 # even index ?
    a.insert( i, ':)' )
    skip 1 # <- this is a pseudo directive to skip the next element
    # which has just been inserted now
    end
    end
    puts a.size

    expected output...

    [0] : 0
    [1] : 1
    [2] : 2
    [3] : 3
    [4] : 4
    [5] : 5
    9 <-- means that 3 smileys have been inserted

    as above, i want to traverse an array with some conditional insertion to
    itself.

    do anyone know cool way for that?

    ttsp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
    3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618
    SHIGETOMI, Takuhiko, Sep 6, 2005
    #1
    1. Advertising

  2. SHIGETOMI, Takuhiko wrote:
    > greetings, guys.
    >
    > this is a pseudo-ruby code which shows what i am expecting.
    >
    > a = [ 0, 1, 2, 3, 4, 5 ]
    > a.each_with_index do |x, i|
    > puts "[#{i}] : #{x}"
    > if i % 2 == 0 # even index ?
    > a.insert( i, ':)' )
    > skip 1 # <- this is a pseudo directive to skip the next element
    > # which has just been inserted now
    > end
    > end
    > puts a.size
    >
    > expected output...
    >
    > [0] : 0
    > [1] : 1
    > [2] : 2
    > [3] : 3
    > [4] : 4
    > [5] : 5
    > 9 <-- means that 3 smileys have been inserted
    >
    > as above, i want to traverse an array with some conditional insertion
    > to itself.
    >
    > do anyone know cool way for that?


    The easiest is probably to use the index for iterating and adjust it on
    insertions. The alternative is to remember insertions and apply them
    after the iteration (probably in reverse order to avoid complicated index
    calculations).

    Kind regards

    robert
    Robert Klemme, Sep 6, 2005
    #2
    1. Advertising

  3. Re: how to do insertion and smooth traversal simultaneously on array

    SHIGETOMI, Takuhiko wrote:
    > greetings, guys.
    >
    > this is a pseudo-ruby code which shows what i am expecting.
    >
    > a = [ 0, 1, 2, 3, 4, 5 ]
    > a.each_with_index do |x, i|
    > puts "[#{i}] : #{x}"
    > if i % 2 == 0 # even index ?
    > a.insert( i, ':)' )
    > skip 1 # <- this is a pseudo directive to skip the next element
    > # which has just been inserted now
    > end
    > end
    > puts a.size
    >
    > expected output...
    >
    > [0] : 0
    > [1] : 1
    > [2] : 2
    > [3] : 3
    > [4] : 4
    > [5] : 5
    > 9 <-- means that 3 smileys have been inserted
    >
    > as above, i want to traverse an array with some conditional insertion to
    > itself.


    If the array is "flat":
    -----------------------------------------
    a = [ 0, 1, 2, 3, 4, 5 ]
    a.each_with_index do |x, i|
    if i % 2 == 0 # even index ?
    a = [ ":)", x ]
    end
    end
    a.flatten!
    p a
    -----------------------------------------
    [":)", 0, 1, ":)", 2, 3, ":)", 4, 5]


    If the array isn't flat:
    -----------------------------------------
    a = [ [81,"&"], 82, 83, [84,"@"], 85 ]
    a = (0...a.size).to_a.zip(a)
    p a
    a = a.inject([]) { |ary,x|
    if x[0] % 2 == 0 # even index ?
    ary += [ ":)", x[1] ]
    else
    ary << x[1]
    end
    }
    p a
    -----------------------------------------
    [[0, [81, "&"]], [1, 82], [2, 83], [3, [84, "@"]], [4, 85]]
    [":)", [81, "&"], 82, ":)", 83, [84, "@"], ":)", 85]
    William James, Sep 6, 2005
    #3
    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. Bryan Young

    Urgent.... Smooth Refresh

    Bryan Young, Jan 25, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    850
    Jeff @ Dart
    Jan 25, 2005
  2. Tim Tyler

    Re: Smooth running applet

    Tim Tyler, Jul 2, 2003, in forum: Java
    Replies:
    0
    Views:
    859
    Tim Tyler
    Jul 2, 2003
  3. Karl von Laudermann

    Re: Smooth running applet

    Karl von Laudermann, Jul 2, 2003, in forum: Java
    Replies:
    3
    Views:
    1,513
    Tim Tyler
    Jul 4, 2003
  4. Replies:
    6
    Views:
    222
    Peter Michaux
    Dec 9, 2006
  5. sylver
    Replies:
    4
    Views:
    183
    sylver
    Aug 2, 2007
Loading...

Share This Page