Skip to content

Instantly share code, notes, and snippets.

@jcc10
Last active November 13, 2019 02:13
Show Gist options
  • Save jcc10/c04a56994e9b3ba554f5554c102fbf16 to your computer and use it in GitHub Desktop.
Save jcc10/c04a56994e9b3ba554f5554c102fbf16 to your computer and use it in GitHub Desktop.
Codeing Competition Input Function
def mass_input(prompt=None, lines=None, end=None):
"""Returns a list of lines of input."""
# Must only have lines *or* end.
assert lines is None or end is None
# *Must* have lines or end.
assert lines is not None or end is not None
# Output array
inputs = []
# This is useful in debugging, allows you to prompt before the section of input.
if prompt is not None:
print(prompt, end='')
# This is for break-after n lines.
if lines is not None:
# the line-count must be a int. (Not float or str)
assert type(lines) is int
# Loop of n items
for __ in range(lines):
# Input Loop
inputs.append(input())
# This is for break-after spicific line.
elif end is not None:
# Line must be a string. (Since all lines must be strings.
assert type(end) is str
# Bootstrap the while loop. (So [-1] is valid)
inputs.append(input())
# [-1] is the last item, while it is not the line we break at, continue.
while inputs[-1] != end:
# Input Loop
inputs.append(input())
# Remove last item (since it shouldn't be parsed.)
# Last item must be the same as end, if not something in Python itself has broken(, and may god have mercy on your soul).
assert inputs.pop(-1) == end
# Return the input array.
return inputs
@jcc10
Copy link
Author

jcc10 commented Nov 9, 2019

This was written for HP CodeWars, and has been used by the SCOPE team for several years. (At least two, probably more)

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