Ruby Program to print pattern [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a hash and print that in the following Pattern :
Input 1 :
{
a: [1,2,3,4,5,6,7,8,9],
b: [1,2,3,4,5,6],
c: [2,3,4,5,6,7]
}
Output 1 :
A1 A2 A3 A4 A5 A6 A7 A8 A9
B1 B2 B3 B4 B5 B6
C2 C3 C4 C5 C6 C7
Input 2 :
{
a: [1,2,3,4,5,6,7],
b: [2,3,4,5,6],
c: [1,2,3,4,5,6,7]
}
Output 2 :
A1 A2 A3 A4 A5 A6 A7
B2 B3 B4 B5 B6
C1 C2 C3 C4 C5 C6 C7
For e.g.
If we consider Input 1 then the expectation is, the solution should add blank spaces at the missing number position.
=> It should return :
[
["A1","A2","A3","A4","A5","A6","A7","A8","A9"],
["B1","B2","B3","B4","B5","B6","","",""],
["","C2","C3","C4","C5","C6","C7","",""]
]

You can use collection, map or each for this
data = {a: [1,2,3,4,5,6,7,8,9],b: [1,2,3,4,5,6],c: [2,3,4,5,6,7]}
data.map{|k,v| (1..9).map{|a| data[k].include?(a) ? k.to_s.upcase() +a.to_s : ' '}}

Related

MD5 implementation in Ruby

I am trying to implement MD5 in Ruby, following the pseudo code written in the wiki.
Here is the codes, not working well:
# : All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
# s specifies the per-round shift amounts
s = []
s.concat([7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22])
s.concat([5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20])
s.concat([4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23])
s.concat([6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21])
# Use binary integer part of the sines of integers (Radians) as constants:
k = 0.upto(63).map do |i|
(Math.sin(i+1).abs * 2 ** 32).floor
end
# Initialize variables:
a0 = 0x67452301 # A
b0 = 0xefcdab89 # B
c0 = 0x98badcfe # C
d0 = 0x10325476 # D
message = File.read(ARGV[0])
# Pre-processing
# with bit stream "string" (MSB)
bits = message.unpack('B*')[0]
org_len = bits.size
bits << '1' # adding a single 1 bit
bits << '0' while !((bits.size + 64) % 512 == 0) # padding with zeros
bits << (org_len % 2 ** 64).to_s(2).rjust(64, '0')
message32 = [bits].pack('B*').unpack('V*') # ?
# 1. bits.scan(/(.{8})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(2, '0') }.each_slice(16) { |c| puts c.join(' ')} => for test
# 2. [bits].pack('B*').unpack('N*') == bits.scan(/(.{32})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(8, '0').to_i(16) } => true
# custom operations for wrapping the results as modulo 2 ** 32
class Integer
def r
self & 0xFFFFFFFF
end
def rotate_l(count)
(self << count).r | (self >> (32 - count))
end
end
# Process the message in successive 512-bit chunks:
message32.each_slice(16).each do |m|
a = a0
b = b0
c = c0
d = d0
0.upto(63) do |i|
if i < 16
f = d ^ (b & (c ^ d))
g = i
elsif i < 32
f = c ^ (d & (b ^ c))
g = (5 * i + 1) % 16
elsif i < 48
f = b ^ c ^ d
g = (3 * i + 5) % 16
elsif i < 64
f = c ^ (b | ~d)
g = (7 * i) % 16
end
f = (f + a + k[i] + m[g]).r
a = d
d = c
c = b
b = (b + f.rotate_l(s[i])).r
end
a0 = (a0 + a).r
b0 = (b0 + b).r
c0 = (c0 + c).r
d0 = (d0 + d).r
end
puts [a0, b0, c0, d0].pack('V*').unpack('H*')
I'm testing with messages, well known for collision in just one block:
Message 1
Message 2
They are resulted in the same value, but not correct:
❯ ruby md5.rb message1.bin
816922b82e2f8d5bd3abf90777ad72c9
❯ ruby md5.rb message2.bin
816922b82e2f8d5bd3abf90777ad72c9
❯ md5 message*
MD5 (/Users/hansuk/Downloads/message1.bin) = 008ee33a9d58b51cfeb425b0959121c9
MD5 (/Users/hansuk/Downloads/message2.bin) = 008ee33a9d58b51cfeb425b0959121c9
I have an uncertainty about pre-processing steps.
I checked the bit stream after pre-processing, with the comments on the line 34 and 35, the original message written in same and the padding bits are right:
❯ hexdump message1.bin
0000000 4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87
0000010 d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18
0000020 af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75
0000030 93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2
0000040
(byebug) bits.scan(/(.{8})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(2, '0') }.each_slice(16) { |c| puts c.join(' ')}
4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87
d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18
af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75
93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2
80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00
nil
What am I missed?
The one most classical mistake in implementing MD5 is botching endianness: the padded message and the message length are to be turned to 32-bit words per little-endian convention, so that message 'abc' in ASCII (0x61 0x62 0x63) is turned to a 16-word padded message block with m[0]=0x80636261, m[14]=0x18, and m[1…13,15]=0.
I never wrote anything in Ruby, but I get the feeling the code yields m[0]=0x61626380, m[15]=0x18, and m[1…14]=0.
Also: the 4-word result is to be turned to 16 bytes per little-endian convention too.

Shuffle elements from three vectors using AVX

After doing few operations I got following three intermediate vectors.
__m256 Vec1 = [a0 a1 a2 a3 a4 a5 a6 a7]; //8 float values
__m256 Vec2 = [b0 b1 b2 b3 b4 b5 b6 b7]; //8 float values
__m256 Vec3 = [c0 c1 c2 c3 c4 c5 c6 c7]; //8 float values
I should rearrange these vectors as shown below for further processing.
__m256 ReVec1 = [a0 a1 b0 b1 c0 c1 a2 a3];
__m256 ReVec2 = [b2 b3 c2 c3 a4 a5 b4 b5];
__m256 ReVec3 = [c4 c5 a6 a7 b6 b7 c6 c7];
How can I shuffle three Vectors in AVX?

MPI Gathering columns into bigger matrix

Lets say I 3 different matrix on 3 different threads:
a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6
c1 c2 c3 c4 c5 c6
d1 d2 d3 d4 d5 d6
e1 e2 e3 e4 e5 e6
f1 f2 f3 f4 f5 f6
I want to gather them by using MPI_Gather into this one matrix
a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6
c1 c2 c3 c4 c5 c6
d1 d2 d3 d4 d5 d6
e1 e2 e3 e4 e5 e6
f1 f2 f3 f4 f5 f6
I am using
MPI_Gather(&oldMatrix,N/size,columnType,&newMatrix,N/size,gatherColType,0,MPI_COMM_WORLD);
Where coltype is:
MPI_Type_vector(N,
1,
N/size,
MPI_FLOAT,
&column);
MPI_Type_commit(&column);
MPI_Type_create_resized(column, 0, 1*sizeof(float), &columnType);
MPI_Type_commit(&columnType);
However, I am not sure how should i create gatherColType (if it is needed). Can you help me about that?
note: N is matrix size and size is thread count (i.e. 3 in this question)

How can I modify this graph?

I have written this code:
digraph G {
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [constraint = false, label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1}
}
It produces the following graph:
Actually I have three questions:
1.How can I make the edge A8 -> A3 neater? It looks very bad.
2.How can I make the edges A254 -> A8 + A8 -> A3 rectangular?
3.How can I make this graph vertical?
EDIT:
It is essential that the nodes are lined up and in the same order shown in the graph above.
This
is generated from
digraph G {
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A8,A3}
}
The best thing to do is read the documentation and experiment with the ideas in there
I have found the solution, here it is
You can make the edge neater by making a suitable adjustments to the distance between nodes and to the size of the nodes. And the most important thing is to force the A8->A3 edge to be longer by setting the minlen attribute of the corrosponding edge.
The following code
digraph G {
nodesep=0.5
node[fixedsize=true, shape="circle", width=0.5]
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [minlen = 3, constraint = false, label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1}
}
generates the following graph:
Edges can not be rectangular.
You can make the graph vertical by using the command rotate=90

How nested 'for' loops are inserting newlines in this program automatically?

#include <stdio.h>
int main()
{
int alpha,numeric;
for(alpha='A';alpha<'K';alpha++)
{
for(numeric=0;numeric<10;numeric++)
{
printf("%c%d\t",alpha,numeric);
}
}
return 0;
}
The output I got is as follows.
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9
B0 B1 B2 B3 B4 B5 B6 B7 B8 B9
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9
E0 E1 E2 E3 E4 E5 E6 E7 E8 E9
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9
G0 G1 G2 G3 G4 G5 G6 G7 G8 G9
H0 H1 H2 H3 H4 H5 H6 H7 H8 H9
I0 I1 I2 I3 I4 I5 I6 I7 I8 I9
J0 J1 J2 J3 J4 J5 J6 J7 J8 J9
But, more neat and in form of a matrix, without any space between lines.
Someone please tell me how the printing is entering to new line even though there is no newline(\n) character in the program.

Resources