Skip to content

Instantly share code, notes, and snippets.

@wings27
Last active June 22, 2018 23:44
Show Gist options
  • Save wings27/a7ffc67657d902e77f7d3ff688728efc to your computer and use it in GitHub Desktop.
Save wings27/a7ffc67657d902e77f7d3ff688728efc to your computer and use it in GitHub Desktop.
python equivalent for Scanner in java.
class Scanner:
def __init__(self):
self.__line_split = None
self.__pointer = None
def next_int(self):
try:
next_str = self._next_str()
ret = int(next_str)
return ret
except ValueError:
return None
def _next_str(self):
if not self.__line_split or self.__pointer >= len(self.__line_split):
self.__line_split = self.__next_line_str().split()
self.__pointer = 0
if not self.__line_split:
return ''
next_one = self.__line_split[self.__pointer]
self.__pointer += 1
return str(next_one)
def __next_line_str(self):
self.__line_split = None
self.__pointer = None
line = str(raw_input())
return line
def main():
scanner = Scanner()
while True:
n = scanner.next_int()
if n is None:
break
# todo: insert code here
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment