Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Gray-code/Ruby/gray-code.rb
Normal file
25
Task/Gray-code/Ruby/gray-code.rb
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class Integer
|
||||
# Converts a normal integer to a Gray code.
|
||||
def to_gray
|
||||
raise Math::DomainError, "integer is negative" if self < 0
|
||||
self ^ (self >> 1)
|
||||
end
|
||||
|
||||
# Converts a Gray code to a normal integer.
|
||||
def from_gray
|
||||
raise Math::DomainError, "integer is negative" if self < 0
|
||||
recurse = proc do |i|
|
||||
next 0 if i == 0
|
||||
o = recurse[i >> 1] << 1
|
||||
o | (i[0] ^ o[1])
|
||||
end
|
||||
recurse[self]
|
||||
end
|
||||
end
|
||||
|
||||
(0..31).each do |number|
|
||||
encoded = number.to_gray
|
||||
decoded = encoded.from_gray
|
||||
printf "%2d : %5b => %5b => %5b : %2d\n",
|
||||
number, number, encoded, decoded, decoded
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue