have a problem with the loops and variable

Discussion in 'Ruby' started by Sajjad Po, Nov 30, 2009.

  1. Sajjad Po

    Sajjad Po Guest

    Hi Friends.
    My first Code :
    -----------------
    q = 1
    w = 2
    while q < w
    s1 = 1
    puts s1
    q += 1
    end
    -----------------

    My last code :
    -----------------
    q = 1
    w = 2
    while q < w
    s[q] = 1
    puts s[q]
    q += 1
    end
    -----------------

    Why does not work ?
    --
    Posted via http://www.ruby-forum.com/.
    Sajjad Po, Nov 30, 2009
    #1
    1. Advertising


  2. > My last code :
    > -----------------
    > q = 1
    > w = 2
    > while q < w
    > s[q] = 1
    > puts s[q]
    > q += 1
    > end
    > -----------------
    >
    > Why does not work ?


    You should be declaring s as an array: s[]

    >> s = []

    => []
    >> q = 1

    => 1
    >> w = 2

    => 2
    >> while q < w

    .. s[q] = 1
    .. puts s[q]
    .. q += 1
    .. end
    1
    => nil
    >>

    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Nov 30, 2009
    #2
    1. Advertising

  3. Michael Linfield wrote:
    >
    >> My last code :
    >> -----------------
    >> q = 1
    >> w = 2
    >> while q < w
    >> s[q] = 1
    >> puts s[q]
    >> q += 1
    >> end
    >> -----------------
    >>
    >> Why does not work ?

    >
    > You should be declaring s as an array: s[]
    >
    >>> s = []

    > => []
    >>> q = 1

    > => 1
    >>> w = 2

    > => 2
    >>> while q < w

    > .. s[q] = 1
    > .. puts s[q]
    > .. q += 1
    > .. end
    > 1
    > => nil
    >>>


    Note that it would be far more idiomatic to use s.each. Never use += to
    step through subscripts in Ruby.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 1, 2009
    #3
  4. Sajjad Po

    Sajjad Po Guest

    thank you friends.
    i want show all details array.for password brute forcer.
    sample : 1 character
    -------------
    a = [1,2,3]
    q1 = 0
    while q1 < a.length
    x = ''
    x += a[q].to_s+a[q1].to_s
    puts x
    q1 += 1
    end
    -------------
    sample : 2 character
    -------------
    a = [1,2,3]
    q = 0
    while q <a.length
    q1 = 0
    while q1 < a.length
    x = ''
    x += a[q].to_s+a[q1].to_s
    puts x
    q1 += 1
    end
    q += 1
    end
    -------------
    sample : 3 character
    -------------
    a = [1,2,3]
    z = 0
    while z <a.length
    q = 0
    while q <a.length
    q1 = 0
    while q1 < a.length
    x = ''
    x += a[z].to_s+a[q].to_s+a[q1].to_s
    puts x
    q1 += 1
    end
    q += 1
    end
    z += 1
    end
    -------------
    And ....

    i want show dynamic.
    sample :
    ------
    a = [1,2,3]
    start_length = 2
    end_length = 3
    ------
    11
    12
    13
    21
    22
    23
    31
    32
    33
    111
    112
    113
    Sajjad Po, Dec 1, 2009
    #4
  5. Sajjad Po

    Sajjad Iran Guest

    Sajjad Iran, Dec 1, 2009
    #5
  6. Michael Linfield, Dec 1, 2009
    #6
  7. Michael Linfield wrote:
    > Sajjad Po wrote:
    >> I want create a program like this.
    >> http://magiccoder.persiangig.com/md5.pl

    >
    > It'd be faster to use a compiled language to do something of that sort.
    > Ruby can do it, but not nearly as quick as C#/C++


    I wonder how big the time difference would be. It seems to me that most
    of the time would be spent in stepping through arrays -- and that would
    be about the same speed in Ruby and C.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 1, 2009
    #7
  8. For something small such as testing lets say a million keys, the
    difference would be minimal. However, when you're brute forcing, you
    generally are testing a million keys/hashes a second, and when using
    algorithms that move that much data through it every second, C++ can
    make all the difference in the world.

    Benchmarks show the difference of milliseconds, even nanoseconds, but
    those add up quick when throwing a million keys a second at them.
    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Dec 1, 2009
    #8
  9. Michael Linfield wrote:
    > For something small such as testing lets say a million keys, the
    > difference would be minimal. However, when you're brute forcing, you
    > generally are testing a million keys/hashes a second, and when using
    > algorithms that move that much data through it every second, C++ can
    > make all the difference in the world.


    Not impressed with this explanation. Most of the stuff in a Ruby
    brute-forced is already implemented in C anyway in MRI (e.g. Array), or
    easily can be if the optimization proves necessary (e.g. the math).
    There's really nothing to be gained from avoiding Ruby.

    >
    > Benchmarks show the difference of milliseconds, even nanoseconds, but
    > those add up quick when throwing a million keys a second at them.


    And have you done those benchmarks?

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 2, 2009
    #9
  10. >Not impressed with this explanation.

    It wasn't supposed to be an involved explanation... there is only so
    much time in the day.

    >And have you done those benchmarks?


    N being the recursive function.

    N CPU secs Time(sec)
    3 0.05 468
    7 0.28 484
    11 3.83 724


    N CPU secs Time(sec)
    3 2.06 2,424
    7 14.21 2,532
    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Dec 2, 2009
    #10
  11. C++ GNU
    > N CPU secs Time(sec)
    > 3 0.05 468
    > 7 0.28 484
    > 11 3.83 724
    >


    Ruby 1.9
    > N CPU secs Time(sec)
    > 3 2.06 2,424
    > 7 14.21 2,532


    Forgot to add which one was which.

    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Dec 2, 2009
    #11
  12. Michael Linfield wrote:
    > C++ GNU
    >> N CPU secs Time(sec)
    >> 3 0.05 468
    >> 7 0.28 484
    >> 11 3.83 724
    >>

    >
    > Ruby 1.9
    >> N CPU secs Time(sec)
    >> 3 2.06 2,424
    >> 7 14.21 2,532

    >
    > Forgot to add which one was which.


    I am *extremely* surprised by this. What's the source code you're
    using? Unless I missed something, I don't think I saw it...

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 2, 2009
    #12
  13. Marnen states:

    "Note that it would be far more idiomatic to use s.each. Never use +=
    to step through subscripts in Ruby."

    An example based on Sajjad's original code would be helpful. Thanks in
    advance.

    - Steve W.


    --
    Posted via http://www.ruby-forum.com/.
    Steve Wilhelm, Dec 2, 2009
    #13
  14. Steve Wilhelm wrote:
    >
    > Marnen states:
    >
    > "Note that it would be far more idiomatic to use s.each. Never use +=
    > to step through subscripts in Ruby."
    >
    > An example based on Sajjad's original code would be helpful.


    Then write one! :) If you're not sure how, do what you can and post it
    here. I'm sure I or someone else will be happy to look over it.

    > Thanks in
    > advance.
    >
    > - Steve W.


    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 2, 2009
    #14
  15. It's a recursive function that calculates the fibonacci sequence..

    If I really felt the need to impress you, then I could go type it up
    again..
    It's only about 7 lines of code if it's really that important to you.

    >Then write one! :) If you're not sure how, do what you can and post it
    >here. I'm sure I or someone else will be happy to look over it.


    If this matter is of this much importance to you, then just as you
    stated yourself, go write the code and I or someone else here would be
    happy to look over it.

    Calculating Fibonacci isn't hard. I'm quite sure a smart guy such as
    yourself can figure it out.

    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Dec 2, 2009
    #15
  16. Michael Linfield wrote:
    > It's a recursive function that calculates the fibonacci sequence..
    >
    > If I really felt the need to impress you, then I could go type it up
    > again..
    > It's only about 7 lines of code if it's really that important to you.


    There's no point in posting benchmarks if you're not going to show the
    code that's being benchmarked.

    >
    >>Then write one! :) If you're not sure how, do what you can and post it
    >>here. I'm sure I or someone else will be happy to look over it.

    >
    > If this matter is of this much importance to you, then just as you
    > stated yourself, go write the code and I or someone else here would be
    > happy to look over it.
    >
    > Calculating Fibonacci isn't hard. I'm quite sure a smart guy such as
    > yourself can figure it out.


    Huh? I think you're misattributing your quotes and getting confused as
    a result.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 2, 2009
    #16
  17. On Wed, Dec 2, 2009 at 2:00 PM, Michael Linfield <> wrote:
    > It's a recursive function that calculates the fibonacci sequence..


    That's a problem right there. Ruby has extremely slow method
    invocation because of all the dispatch rules.
    If you really want to compare Ruby to C when crunching numbers / doing
    array operations, write an iterative solution for a fairer test.

    -greg
    Gregory Brown, Dec 3, 2009
    #17
  18. Marnen Laibow-Koser wrote:
    > Steve Wilhelm wrote:
    >>
    >> Marnen states:
    >>
    >> "Note that it would be far more idiomatic to use s.each. Never use +=
    >> to step through subscripts in Ruby."
    >>
    >> An example based on Sajjad's original code would be helpful.

    >
    > Then write one! :) If you're not sure how, do what you can and post it
    > here. I'm sure I or someone else will be happy to look over it.
    >
    >> Thanks in
    >> advance.
    >>
    >> - Steve W.

    >
    > Best,
    > -- 
    > Marnen Laibow-Koser
    > http://www.marnen.org
    >


    Okay, Sajjad's original code was

    q = 1
    w = 2
    while q < w
    s[q] = 1
    puts s[q]
    q += 1
    end


    It was suggested to use s.each. The problem as I see it is that Sajjad
    is trying to populate the array s with ones.

    I think what Sajjad wanted was:

    q, w = 1, 2
    s = Array.new(w - q) { 1 }

    It doesn't quite do what the original code attempts, because Sajjad's
    code starts at array index 1 instead of 0. I do know know if that is
    what he actually intended or if it is another bug.

    Is there a better way to populate an array with default values using
    array's each method?




    --
    Posted via http://www.ruby-forum.com/.
    Steve Wilhelm, Dec 3, 2009
    #18
  19. Steve Wilhelm wrote:
    > I think what Sajjad wanted was:
    >
    > q, w = 1, 2
    > s = Array.new(w - q) { 1 }
    >
    > It doesn't quite do what the original code attempts, because Sajjad's
    > code starts at array index 1 instead of 0. I do know know if that is
    > what he actually intended or if it is another bug.
    >
    > Is there a better way to populate an array with default values using
    > array's each method?


    q, w = 1, 2
    s = Array.new(w - q) { |i| i.zero? ? 0 : 1 }

    How's that? :)
    Otherwise, brute-forcing like that is really just using permutations..
    So how about this?
    http://trevoke.net/blog/2008/12/20/lexicographic-permutations-in-ruby/
    I didn't include explanations for the code, but you can check out the
    other blog if you'd like. It does the permutation and runs a block for
    each permutations.
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Dec 3, 2009
    #19
  20. Steve Wilhelm wrote:
    > Marnen Laibow-Koser wrote:
    >> Steve Wilhelm wrote:
    >>>
    >>> Marnen states:
    >>>
    >>> "Note that it would be far more idiomatic to use s.each. Never use +=
    >>> to step through subscripts in Ruby."
    >>>
    >>> An example based on Sajjad's original code would be helpful.

    >>
    >> Then write one! :) If you're not sure how, do what you can and post it
    >> here. I'm sure I or someone else will be happy to look over it.
    >>
    >>> Thanks in
    >>> advance.
    >>>
    >>> - Steve W.

    >>
    >> Best,
    >> -- 
    >> Marnen Laibow-Koser
    >> http://www.marnen.org
    >>

    >
    > Okay, Sajjad's original code was
    >
    > q = 1
    > w = 2
    > while q < w
    > s[q] = 1
    > puts s[q]
    > q += 1
    > end
    >
    >
    > It was suggested to use s.each. The problem as I see it is that Sajjad
    > is trying to populate the array s with ones.
    >
    > I think what Sajjad wanted was:
    >
    > q, w = 1, 2
    > s = Array.new(w - q) { 1 }


    I think you're probably right.

    >
    > It doesn't quite do what the original code attempts, because Sajjad's
    > code starts at array index 1 instead of 0. I do know know if that is
    > what he actually intended or if it is another bug.
    >
    > Is there a better way to populate an array with default values using
    > array's each method?


    No, I made the wrong suggestion. Sorry.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 4, 2009
    #20
    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. Thomas Matthews
    Replies:
    3
    Views:
    399
    Jason
    Aug 21, 2003
  2. Zachary  Turner

    Variable declaration and while loops

    Zachary Turner, Jun 18, 2007, in forum: C++
    Replies:
    10
    Views:
    548
    Andre Kostur
    Jun 18, 2007
  3. Derek Martin
    Replies:
    0
    Views:
    1,314
    Derek Martin
    Jul 18, 2008
  4. Me
    Replies:
    2
    Views:
    232
  5. Replies:
    2
    Views:
    137
    Laurent Bugnion
    Aug 22, 2006
Loading...

Share This Page