Skip to content

Instantly share code, notes, and snippets.

@HackerEarthBlog
Created February 1, 2017 06:20
Show Gist options
  • Save HackerEarthBlog/5330e5f11f96a22608b45affa61fa858 to your computer and use it in GitHub Desktop.
Save HackerEarthBlog/5330e5f11f96a22608b45affa61fa858 to your computer and use it in GitHub Desktop.
for handling buffered input
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
@ClaudioGi
Copy link

You might take a look at the Python module called deque which optimizes this kind of functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment