A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
26
Task/Gray-code/Ruby/gray-code.rb
Normal file
26
Task/Gray-code/Ruby/gray-code.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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