Last active
August 29, 2015 14:21
-
-
Save gerardvcruz/ffc6e25ae3150d4b27c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# q can be unlimited length | |
# one var only | |
# each element is a single digit integer | |
class SingleVarQ | |
def initialize(start) | |
@q = start | |
end | |
def queue | |
p @q | |
end | |
def enqueue num_to_queue | |
unless num_to_queue > 9 || num_to_queue < 0 | |
@q == 0 ? @q = num_to_queue : @q = num_to_queue * 10**index(@q) + @q | |
end | |
end | |
def dequeue | |
@q /= 10 | |
end | |
def circular_queue | |
enqueue(@q % 10); dequeue | |
end | |
private | |
def index(q) | |
q < 10 ? 1 : Math.log10(q).ceil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment