-
Notifications
You must be signed in to change notification settings - Fork 30
/
gray_code.rb
51 lines (47 loc) · 943 Bytes
/
gray_code.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def gray_code(n)
return [0] if n==0
res1=gray_code(n-1)
res2=res1.reverse
shift=2**(n-1)
res2.each_with_index{|n,i| res2[i]=n+shift }
res1+res2
end
# reflect gray code. refer to Combinatorics::Richard
def gray_code(n)
return [0] if n<1
return [0,1] if n==1
result=Array.new
baseLine='0'*n
terminLine='1'+baseLine[1..n]
result.push Integer("0b"+baseLine)
while baseLine!=terminLine
if sigma baseLine
baseLine=changeBit(baseLine, baseLine.length-1)
else
baseLine=changeBit(baseLine, findPosition(baseLine))
end
result.push Integer("0b"+baseLine)
end
result
end
def sigma(line)
count=0
line.length.times {|i| count+=1 if line[i]=='1'}
count%2==0
end
def changeBit(line, index)
if line[index]=='1'
line[index]='0'
else
line[index]='1'
end
line
end
def findPosition(line)
cur=line.length-1
while line[cur]=='0'
cur-=1
break if cur==0
end
cur-1
end