Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4483 > unrolled thread
| Started by | aix aix <ruby.aix@gmail.com> |
|---|---|
| First post | 2011-05-13 07:25 -0500 |
| Last post | 2011-05-13 13:43 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.ruby
Calcul XOR : array , times. aix aix <ruby.aix@gmail.com> - 2011-05-13 07:25 -0500
Re: Calcul XOR : array , times. Harry Kakueki <list.push@gmail.com> - 2011-05-13 10:17 -0500
Re: Calcul XOR : array , times. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-13 13:22 -0500
Re: Calcul XOR : array , times. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-13 13:43 -0500
| From | aix aix <ruby.aix@gmail.com> |
|---|---|
| Date | 2011-05-13 07:25 -0500 |
| Subject | Calcul XOR : array , times. |
| Message-ID | <a7cc430dc5fa3e0403b87599dcb06dc2@ruby-forum.com> |
Hello ,
I have problems with my code for XOR calcul :
a = [1, 0, 1, 1, 0, 0, 1]
b = ["101101", "101100", "110011", "000111", "010110"]
# good result : 000001, 011010,101000, 001010,110000
# false result : ["000001", "000000", "011111", "101011", "111010"]
=> output with this code
# Why ? Because he spends that time with respect b[i].lenght ( = 6
generally ) that prevents take in this case the element 7 and 8 of "a".
# works in case a.length <b [x]. length
# How to resolv this problem ?
i = j = x = 0
c = []
d = []
f = []
5.times { |x| c.push b[x].split('') }
5.times { |i|
e = c[i].length
e.times { |j| d.push(a[j % 7].to_i ^ c[i][j].to_i) }
f.push d
d = []
}
puts "Résultat brute: "
p f
z = f.length
z.times { |h| f[h] = f[h].join("")}
puts "Résultats :"
p f
In this case :
a = [1, 0, 1, 1, 0, 0, 1]
b = ["101101", "101100", "110011", "000111", "010110"]
After splitting
b : [["1","0","1","1","0","1"], ["1","0","1","1","0","0"],
["1","1","0","0","1","1"],
["0","0","0","1","1","1"], ["0","1","0","1","1","0"]]
But `a.length => 7` and `b[x].length =>6`
In this code, the xor calcul is performed with only 6 elements in a on
7.
So the calculation is performed here:
a[0] ^ b[0][0] ; a[1] ^ b[0][1] ; a[2] ^ b[0][2] ; a[3] ^ b[0][3] ;
a[4] ^ b[0][4] ; a[5] ^ b[0][5] ;
a[0] ^ b[1][0] ; a[1] ^ b[1][1] ; a[2] ^ b[1][2] ; a[3] ^ b[1][3] ;
a[4] ^ b[1][4] ; a[5] ^ b[1][5] ;
a[0] ^ b[2][0] ; a[1] ^ b[2][1] ; a[2] ^ b[2][2] ; a[3] ^ b[2][3] ;
a[4] ^ b[2][4] ; a[5] ^ b[2][5] ;
a[0] ^ b[3][0] ; a[1] ^ b[3][1] ; a[2] ^ b[3][2] ; a[3] ^ b[3][3] ;
a[4] ^ b[3][4] ; a[5] ^ b[3][5] ;
a[0] ^ b[4][0] ; a[1] ^ b[4][1] ; a[2] ^ b[4][2] ; a[3] ^ b[4][3] ;
a[4] ^ b[4][4] ; a[5] ^ b[4][5] ;
`a[6]` is never use.
and the calculation should be done:
a[0] ^ b[0][0] ; a[1] ^ b[0][1] ; a[2] ^ b[0][2] ; a[3] ^ b[0][3] ;
a[4] ^ b[0][4] ; a[5] ^ b[0][5] ;
a[6] ^ b[1][0] ; a[0] ^ b[1][1] ; a[1] ^ b[1][2] ; a[2] ^ b[1][3] ;
a[3] ^ b[1][4] ; a[4] ^ b[1][5] ;
a[5] ^ b[2][0] ; a[6] ^ b[2][1] ; a[0] ^ b[2][2] ; a[1] ^ b[2][3] ;
a[2] ^ b[2][4] ; a[3] ^ b[2][5] ;
a[4] ^ b[3][0] ; a[5] ^ b[3][1] ; a[6] ^ b[3][2] ; a[0] ^ b[3][3] ;
a[1] ^ b[3][4] ; a[2] ^ b[3][5] ;
a[3] ^ b[4][0] ; a[4] ^ b[4][1] ; a[5] ^ b[4][2] ; a[6] ^ b[4][3] ;
a[0] ^ b[4][4] ; a[1] ^ b[4][5] ;
I want to make the right calculation of course but I do not see how to
this.
How to use a[6] as above ?
Thanks
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Harry Kakueki <list.push@gmail.com> |
|---|---|
| Date | 2011-05-13 10:17 -0500 |
| Message-ID | <BANLkTimJsgZPt6aGctrL6kYxS+DhC0P-aA@mail.gmail.com> |
| In reply to | #4483 |
On Fri, May 13, 2011 at 9:25 PM, aix aix <ruby.aix@gmail.com> wrote:
> Hello ,
>
> I have problems with my code for XOR calcul :
>
> a = [1, 0, 1, 1, 0, 0, 1]
> b = ["101101", "101100", "110011", "000111", "010110"]
> # good result : 000001, 011010,101000, 001010,110000
> # false result : ["000001", "000000", "011111", "101011", "111010"]
>
> How to use a[6] as above ?
>
Sorry, I did not look for a problem with your code. I did it another way.
This is not tested so it may have some problems but maybe it will give
you some ideas.
a = [1, 0, 1, 1, 0, 0, 1]
b = ["101101", "101100", "110011", "000111", "010110"]
bmod = b.join.split(//).map{|x| x.to_i}
amod = a*(bmod.size/a.size+1)
res = amod[0...bmod.size].zip(bmod).map{|z| z[0]^z[1]}
p [].tap{|y| b.size.times{y << res.slice!(0...b[0].size)}}.map{|g| g.join}
Harry
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-13 13:22 -0500 |
| Message-ID | <8047b73e0afaafad066d17f440d40cf1@ruby-forum.com> |
| In reply to | #4483 |
aix aix wrote in post #998497: > How to use a[6] as above ? > > Thanks You want Array#cycle. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-13 13:43 -0500 |
| Message-ID | <c15ba9ed49c89eee245ec3239b5f5bcf@ruby-forum.com> |
| In reply to | #4510 |
7stud -- wrote in post #998575:
> aix aix wrote in post #998497:
>> How to use a[6] as above ?
>>
>> Thanks
>
> You want Array#cycle.
Here's an example using Enumerator#next:
a = [1, 0, 1, 1, 0, 0, 1]
b = ["101101", "101100", "110011", "000111", "010110"]
enum = a.cycle
b.each do |str|
int_arr = str.split(//).map(&:to_i)
result_arr = int_arr.map do |int|
int ^ enum.next
end
p result_arr.join('')
end
--output:--
"000001"
"011010"
"101000"
"001010"
"110000"
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web