# can you explain code

Discussion in 'Ruby' started by Aashish Kiran, Jan 8, 2010.

1. ### Aashish KiranGuest

---------------------ruby code--------------------

after running ruby code

in below output at first occurrence of

"sssssssssssssssssssssss"

can anyone explain how "nearValues" is updating

\$hash2={"value1"=>["value2", "value3", "value6"], "value2"=>["value1",
"value3", "value6"], "value3"=>["value1", "value2", "value6"],
"value4"=>["value5", "value6"], "value5"=>["value4", "value6"],
"value6"=>["value4", "value5", "value1", "value2", "value3"]}

\$subgraphs = []

def get_subgraphs(setGroup, nearValues, checkedValues)
p
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
p "setGroup"
p setGroup
p "nearValues"
p nearValues
p "checkedValues"
p checkedValues
if nearValues.empty? && checkedValues.empty?
\$subgraphs << setGroup.sort.join(", ") if setGroup.size > 2
else
possible_pivots = nearValues + checkedValues
p "possible_pivots"
p possible_pivots

pivot = possible_pivots.inject(nil) do |best, p|
best.nil? || \$hash2[p].size > best.size ? best = p : best
end
p " pivot "
p pivot

nearValues.size.times do |i|
n = nearValues
next if \$hash2[n].include?(pivot)
nearValues = nil
p "i"
p i
p "checkedValues"
p checkedValues
p "nearValues.compact & \$hash2[n]"
p nearValues.compact & \$hash2[n]
p " checkedValues & \$hash2[n]"
p checkedValues & \$hash2[n]
get_subgraphs(setGroup + [n], nearValues.compact & \$hash2[n],
checkedValues & \$hash2[n])
p "sssssssssssssssssssssss"

checkedValues << n
p "checkedValues"
p checkedValues
p "nearValues"
p nearValues
end
end
end
p "\$hash2 ="
p \$hash2
p "\$hash2.keys ="
p \$hash2.keys

get_subgraphs([], \$hash2.keys, [])
p "\$subgraphs ="
p \$subgraphs
puts \$subgraphs.sort.join("\n") << "\n"

----------------------end--------------------

---------------- output is as follows ---------------------------

Hello World

"\$hash2 ="

{"value1"=>["value2", "value3", "value6"], "value2"=>["value1",
"value3", "value6"], "value3"=>["value1", "value2", "value6"],
"value4"=>["value5", "value6"], "value5"=>["value4", "value6"],
"value6"=>["value4", "value5", "value1", "value2", "value3"]}

"\$hash2.keys ="

["value1", "value2", "value3", "value4", "value5", "value6"]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

[]

"nearValues"

["value1", "value2", "value3", "value4", "value5", "value6"]

"checkedValues"

[]

"possible_pivots"

["value1", "value2", "value3", "value4", "value5", "value6"]

" pivot "

"value1"

"i"

0

"checkedValues"

[]

"nearValues.compact & \$hash2[n]"

["value2", "value3", "value6"]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value1"]

"nearValues"

["value2", "value3", "value6"]

"checkedValues"

[]

"possible_pivots"

["value2", "value3", "value6"]

" pivot "

"value2"

"i"

0

"checkedValues"

[]

"nearValues.compact & \$hash2[n]"

["value3", "value6"]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value1", "value2"]

"nearValues"

["value3", "value6"]

"checkedValues"

[]

"possible_pivots"

["value3", "value6"]

" pivot "

"value3"

"i"

0

"checkedValues"

[]

"nearValues.compact & \$hash2[n]"

["value6"]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value1", "value2", "value3"]

"nearValues"

["value6"]

"checkedValues"

[]

"possible_pivots"

["value6"]

" pivot "

"value6"

"i"

0

"checkedValues"

[]

"nearValues.compact & \$hash2[n]"

[]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value1", "value2", "value3", "value6"]

"nearValues"

[]

"checkedValues"

[]

"sssssssssssssssssssssss"

"checkedValues"

["value6"]

"nearValues"

[nil]

"sssssssssssssssssssssss"

"checkedValues"

["value3"]

"nearValues"

[nil, "value6"]

"sssssssssssssssssssssss"

"checkedValues"

["value2"]

"nearValues"

[nil, "value3", "value6"]

"sssssssssssssssssssssss"

"checkedValues"

["value1"]

"nearValues"

[nil, "value2", "value3", "value4", "value5", "value6"]

"i"

3

"checkedValues"

["value1"]

"nearValues.compact & \$hash2[n]"

["value5", "value6"]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value4"]

"nearValues"

["value5", "value6"]

"checkedValues"

[]

"possible_pivots"

["value5", "value6"]

" pivot "

"value5"

"i"

0

"checkedValues"

[]

"nearValues.compact & \$hash2[n]"

["value6"]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value4", "value5"]

"nearValues"

["value6"]

"checkedValues"

[]

"possible_pivots"

["value6"]

" pivot "

"value6"

"i"

0

"checkedValues"

[]

"nearValues.compact & \$hash2[n]"

[]

" checkedValues & \$hash2[n]"

[]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value4", "value5", "value6"]

"nearValues"

[]

"checkedValues"

[]

"sssssssssssssssssssssss"

"checkedValues"

["value6"]

"nearValues"

[nil]

"sssssssssssssssssssssss"

"checkedValues"

["value5"]

"nearValues"

[nil, "value6"]

"sssssssssssssssssssssss"

"checkedValues"

["value1", "value4"]

"nearValues"

[nil, "value2", "value3", nil, "value5", "value6"]

"i"

4

"checkedValues"

["value1", "value4"]

"nearValues.compact & \$hash2[n]"

["value6"]

" checkedValues & \$hash2[n]"

["value4"]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value5"]

"nearValues"

["value6"]

"checkedValues"

["value4"]

"possible_pivots"

["value6", "value4"]

" pivot "

"value6"

"i"

0

"checkedValues"

["value4"]

"nearValues.compact & \$hash2[n]"

[]

" checkedValues & \$hash2[n]"

["value4"]

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"setGroup"

["value5", "value6"]

"nearValues"

[]

"checkedValues"

["value4"]

"possible_pivots"

["value4"]

" pivot "

"value4"

"sssssssssssssssssssssss"

"checkedValues"

["value4", "value6"]

"nearValues"

[nil]

"sssssssssssssssssssssss"

"checkedValues"

["value1", "value4", "value5"]

"nearValues"

[nil, "value2", "value3", nil, nil, "value6"]

"\$subgraphs ="

["value1, value2, value3, value6", "value4, value5, value6"]

value1, value2, value3, value6

value4, value5, value6
--
Posted via http://www.ruby-forum.com/.

Aashish Kiran, Jan 8, 2010

2. ### Aldric GiacomoniGuest

Aashish Kiran wrote:
>
> ---------------------ruby code--------------------
>
> after running ruby code
>
> in below output at first occurrence of
>
> "sssssssssssssssssssssss"
>
> can anyone explain how "nearValues" is updating
>

(skip long useless code and output)

Well, your code says this:

nearValues.size.times do |i|
n = nearValues
next if \$hash2[n].include?(pivot)
nearValues = nil

That means, er. .. If the pivot is in \$hash2[n], whatever that means,
then go to the next number. Otherwise, if the pivot is not in \$hash2[n],
nearValues is set to nil.
This, incidentally, is absolutely terrible Ruby, and I want to inflict a
lot of pain to the person who wrote it.
--
Posted via http://www.ruby-forum.com/.

Aldric Giacomoni, Jan 8, 2010

3. ### Aashish KiranGuest

Aldric Giacomoni wrote:
> Aashish Kiran wrote:
>>
>> ---------------------ruby code--------------------
>>
>> after running ruby code
>>
>> in below output at first occurrence of
>>
>> "sssssssssssssssssssssss"
>>
>> can anyone explain how "nearValues" is updating
>>

> (skip long useless code and output)
>
> Well, your code says this:
>
> nearValues.size.times do |i|
> n = nearValues
> next if \$hash2[n].include?(pivot)
> nearValues = nil
>
> That means, er. .. If the pivot is in \$hash2[n], whatever that means,
> then go to the next number. Otherwise, if the pivot is not in \$hash2[n],
> nearValues is set to nil.
> This, incidentally, is absolutely terrible Ruby, and I want to inflict a
> lot of pain to the person who wrote it.

thank you for reply
can you please explain how nearValues is adding values

"sssssssssssssssssssssss"

"checkedValues"

["value6"]

"nearValues"

[nil]

"sssssssssssssssssssssss"

"checkedValues"

["value3"]

"nearValues"

[nil, "value6"]

"sssssssssssssssssssssss"

"checkedValues"

["value2"]

"nearValues"

[nil, "value3", "value6"]

"sssssssssssssssssssssss"

"checkedValues"

["value1"]

"nearValues"

[nil, "value2", "value3", "value4", "value5", "value6"]

"i"

3
--
Posted via http://www.ruby-forum.com/.

Aashish Kiran, Jan 8, 2010
4. ### Aldric GiacomoniGuest

Aashish Kiran wrote:
> Aldric Giacomoni wrote:
>> Aashish Kiran wrote:
>>>
>>> can anyone explain how "nearValues" is updating
>>>

>> (skip long useless code and output)
>>
>> Well, your code says this:
>>
>> nearValues.size.times do |i|
>> n = nearValues
>> next if \$hash2[n].include?(pivot)
>> nearValues = nil
>>
>> That means, er. .. If the pivot is in \$hash2[n], whatever that means,
>> then go to the next number. Otherwise, if the pivot is not in \$hash2[n],
>> nearValues is set to nil.
>> This, incidentally, is absolutely terrible Ruby, and I want to inflict a
>> lot of pain to the person who wrote it.

>
>
> thank you for reply
> can you please explain how nearValues is adding values
>

No, because that's not in the code supplied. nearValues is passed to
this method. The code is ugly but very readable. Go over it a few times
and it will make sense.
Hints:
- "p" means "puts" and stands for 'send to the screen' (basically)
- Unknown methods can be looked up here: http://ruby-doc.org/core/
--
Posted via http://www.ruby-forum.com/.

Aldric Giacomoni, Jan 8, 2010

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.