have a problem with the loops and variable

S

Sajjad Po

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
 
M

Marnen Laibow-Koser

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

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
(e-mail address removed)
 
S

Sajjad Po

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
 
M

Marnen Laibow-Koser

Michael said:
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
(e-mail address removed)
 
M

Michael Linfield

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.
 
M

Marnen Laibow-Koser

Michael said:
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
(e-mail address removed)
 
M

Michael Linfield

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
 
M

Marnen Laibow-Koser

Michael said:
C++ GNU

Ruby 1.9

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
(e-mail address removed)
 
S

Steve Wilhelm

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.
 
M

Marnen Laibow-Koser

Steve said:
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
(e-mail address removed)
 
M

Michael Linfield

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.
 
M

Marnen Laibow-Koser

Michael said:
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.
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
(e-mail address removed)
 
G

Gregory Brown

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
 
S

Steve Wilhelm

Marnen said:
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.


Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)

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?
 
A

Aldric Giacomoni

Steve said:
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.
 
M

Marnen Laibow-Koser

Steve said:
Marnen said:
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.


Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)

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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top