Skip to content

Instantly share code, notes, and snippets.

@shachopin
Created December 19, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shachopin/cbed117a7850b84114c0 to your computer and use it in GitHub Desktop.
Save shachopin/cbed117a7850b84114c0 to your computer and use it in GitHub Desktop.
solution for octal problem
class Octal
attr_reader :digits
def initialize(input)
@invalid = (input =~ /\D|8|9/)
@digits = input.chars.map(&:to_i)
end
def to_decimal
return 0 if @invalid
digits.reverse.each_with_index.inject(0){|sum, (n, index)| sum + n * (8 ** index) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment