Created
February 1, 2017 06:20
-
-
Save HackerEarthBlog/5330e5f11f96a22608b45affa61fa858 to your computer and use it in GitHub Desktop.
for handling buffered input
This file contains hidden or 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
class UngetableInput: | |
def __init__(self, initialBuffer=''): | |
self.buffer = initialBuffer | |
def getc(self): | |
if not self.buffer: | |
self.buffer = input() | |
c = self.buffer[0] | |
self.buffer = self.buffer[1:] | |
return c | |
def ungetc(self, c): | |
self.buffer = c + self.buffer | |
def getword(self): | |
if not self.buffer: | |
self.buffer = input() | |
word = self.buffer.split()[0] | |
self.buffer = ' '.join(self.buffer.split()[1:]) | |
return word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might take a look at the Python module called deque which optimizes this kind of functionality.