jhanarato / uposatha-inky

Display the uposatha on a Pimoroni Inky display
MIT License
0 stars 0 forks source link

seq_to_row simplified impl #5

Closed AlyceOsbourne closed 1 year ago

AlyceOsbourne commented 1 year ago
def seq_to_rows(seq: Sequence, row_length: int) -> list[list[Any]]:
    return [
        seq[max(i - row_length, 0) : i]    # never go below 0, otherwise returns an empty list
        for i in reversed(range(len(seq), 0, -row_length))  # create the index sequence to order the results
    ]

In this first example, we reverse the range created, this is cheaper than reversing the created list, as its lazy. I might consider making this a generator that yields List[Any], this is as simple as replacing return with yield from or

def seq_to_rows(seq: Sequence, row_length: int) -> list[list[Any]]:
    rows = []
    for i in range(len(seq), 0, -row_length):
        rows.insert(0, seq[max(i - row_length, 0):i])
    return rows

Now, if I am remembering correctly, insert 0 has a high cost, dues to it being O(n) time, so, while this implementation will work, I think it will be slower than the above operation

AlyceOsbourne commented 1 year ago

this is sorta a concrete impl of dales suggestion, with the segments part implemented Also, after testing I can conclude that the insertion method is insertion method is much, much slower, so much so I wouldn't consider it to be a good choice

jhanarato commented 1 year ago

Thanks for the contribution! My next session will focus on this transformation and pay attention to the above.

jhanarato commented 1 year ago

See issue #3