Created
January 20, 2017 14:17
-
-
Save anonymous/0da784db138efc75a70fa2d30e05d30b to your computer and use it in GitHub Desktop.
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
def show_slicing(text, start=0, stop=None, step=1): | |
if step<1: | |
print("Sorry, reverse stepping not supported in this visualization") | |
else: | |
sliced_text = text[start:stop:step] | |
lead_space = ' '*start+'|' | |
marker = '^' + (step-1)*' ' | |
(marks, tail) = divmod(len(text)-start, step) | |
untruncated_markline = lead_space + marker*marks + 'x'*tail | |
truncated_markline = untruncated_markline[:(stop+1)]+'|' | |
print(f'|{text}|') | |
print(f'{truncated_markline}') | |
print(f'Slicing result: <{sliced_text}>') | |
show_slicing('The quick brown fox jumped over the lazy sleeping dog',1,-5,4) | |
show_slicing('The quick brown fox jumped over the lazy sleeping dog',2,13,3) | |
show_slicing('The quick brown fox jumped over the lazy sleeping dog',2,13,3) | |
show_slicing('The quick brown fox jumped over the lazy sleeping dog',2,14,3) | |
show_slicing('The quick brown fox jumped over the lazy sleeping dog',2,15,3) | |
show_slicing('The quick brown fox jumped over the lazy sleeping dog',10,13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment