Created
February 25, 2020 15:21
-
-
Save raghur/b44b616b4c9e3d6adbbc02f8f2ebf180 to your computer and use it in GitHub Desktop.
pyramid
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
import sys | |
import logging | |
import itertools | |
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) | |
def termGenerator(sequence, start, length, k): | |
"""Generate kth term in a rolling sequence of 1, 3, 5... letters | |
Even terms are reversed | |
""" | |
# pick seqLen chars after skipping first startPos chars | |
# logging.debug("Picking %d letters after %d", termLen, startPos) | |
chars = itertools.islice(itertools.cycle(sequence), start, start + length) | |
term = "".join(chars) | |
if k % 2 == 0: | |
term = term[::-1] | |
return term | |
termLen = lambda k: 1 + (k - 1)* 2 | |
startPos = lambda k: (k-1)** 2 | |
def main(sequence, depth, direction): | |
"""TODO: Docstring for main. | |
:returns: TODO | |
""" | |
logging.debug("seq %s depth %s dir %s", sequence, depth, direction) | |
# terms 1, 3, 5, 7, 9 .... | |
start = 1 | |
end = depth | |
if direction == -1: | |
start = end | |
end = 0 | |
skip =0 | |
pos = 1 | |
for i in range(start, end, direction): | |
term = termGenerator(sequence, skip, termLen(i), pos) | |
pos +=1 | |
skip += len(term) | |
print(term) | |
if __name__ == "__main__": | |
seq, depth, direction = sys.argv[1:] | |
main("abc", 6, 1) | |
main("1234567890", 10, -1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment