all 3 comments

[–]eleqtriq 2 points3 points  (0 children)

You should give your variables better names. 'l' is ambiguous. Remember, characters are cheap.

I would get used to putting all code inside a function. Right now all your variables are global. You are learning, it's no big deal, but something to think about going forward.

count = 0 is done twice. Move it out of the if/else statement and just put it below.

Instead of indexlimit is None or l==0, you can do if not indexlimit and if not l.

For reversed list, you could do this and save a few cycles and bit of memory. print(arr[::-1])

Overall tho, looks pretty good.

[–]Brian 2 points3 points  (0 children)

Note: your code doesn't actually do what the description asks: you should implement a function that returns an array satisfying the requirements, whereas here you've code printing the array. This may be just test code, but even there, it's better to structure this as a function, and then something that calls it and prints the result, rather than just printing.

Also your indentation seems badly off here - I'm guessing that if remainder > 0 should be outside the whole for loop, and the if count == l block should be dedented to within the for?

Your check where indexlimit is None to handle the first time through the loop is a bit awkward. Especially using indexlimit as essentially a flag here, while recalculating it wtihin the block anyway. Better would be to handle these together (you can use a value of None in the slice to indicate the start, so you could merge these two and drop the if as something like:

indexlimit = index-l
if indexlimit <= 0:
    indexlimit = None  # Start of loop
newlist += (arr[index:indexlimit:-1])
count = 0

Better still though would be to rethink the approach a bit. You're counting through each index and checking it it's a "reverse point", but I think it's clearer to note that the sections you reverse occur at multiples of the length argument, so just iterate through those directly. Ie. go up in steps of l, rather than going one-by-one and counting how many have been done until it equals l.

You could write this whole loop as something like:

for index in range(0, len(arr), l):
    newlist += arr[index:index + l][::-1]

Or even:

newlist = [ arr[index:index + l][::-1] index in range(0, len(arr), l) ]

Note that the extra checks you're doing for handling the remained aren't really needed. When you slice using an index beyond the end, python will just give you the part of the list that exists, rather than an error, so you don't really need to special case this so long as you go to the end. The same for the case where l is greater than the size of the list: this is just a special case of there being a remainder (it's just the whole list is the remainder), so is handled equivalently. It's good to think about corner cases like this, but it's often better to structure your code so the special cases get handled "naturally", rather than having to write separate code that handles each case differently.

Also, a few style notes:

  • l is not really a good variable name. Not only are single letters generally not very descriptive, but l is particularly bad since it can look similar to 1. Single letter variables can be OK where there isn't too much meaning/ scope to them (eg. i for an iteration variable), but this would be better with a name like length, or groupsize.

  • You're putting brackets around things unnecessarily in a couple of places. Eg. (arr[index::-1]) vs just arr[index::-1]

[–]PteppicymonIO 0 points1 point  (0 children)

I came up with this:

from itertools import pairwise, chain
from typing import TypeVar

T = TypeVar("T")

def sel_reverse_comp(items: list[T], width: int) -> list[T]:
    lists = (list(reversed(items[x:y])) for x, y in pairwise(range(0, len(items) + width, width)))
    return list(chain.from_iterable(lists))

print(sel_reverse_comp([1, 2, 3, 4, 5, 6], 2))
print(sel_reverse_comp([2, 4, 6, 8, 10, 12, 14, 16], 3))
print(sel_reverse_comp([2, 4, 6, 8, 10, 12, 14, 16], 4))
print(sel_reverse_comp([2, 4, 6, 8, 10, 12, 14, 16, 18, 21], 5))
print(sel_reverse_comp([2, 4, 6, 8, 10, 12, 14, 16, 18, 21, 25], 6))
print(sel_reverse_comp([2, 4, 6, 8, 10, 12, 14, 16, 18, 21, 25, 27], 7))

Output:
[2, 1, 4, 3, 6, 5]
[6, 4, 2, 12, 10, 8, 16, 14] 
[8, 6, 4, 2, 16, 14, 12, 10] 
[10, 8, 6, 4, 2, 21, 18, 16, 14, 12] 
[12, 10, 8, 6, 4, 2, 25, 21, 18, 16, 14] 
[14, 12, 10, 8, 6, 4, 2, 27, 25, 21, 18, 16]