slice() function to get the chunk and reverse itsample_list = [11, 45, 8, 23, 14, 12, 78, 45, 89]
Chunk 1 [11, 45, 8] After reversing it [8, 45, 11] Chunk 2 [23, 14, 12] After reversing it [12, 14, 23] Chunk 3 [78, 45, 89] After reversing it [89, 45, 78]
sample_list = [11, 45, 8, 23, 14, 12, 78, 45, 89]
print("Original list ", sample_list)
length = len(sample_list)
chunk_size = int(length / 3)
start = 0
end = chunk_size
# run loop 3 times
for i in range(3):
# get indexes
indexes = slice(start, end)
# get chunk
list_chunk = sample_list[indexes]
print("Chunk ", i, list_chunk)
# reverse chunk
print("After reversing it ", list(reversed(list_chunk)))
start = end
end += chunk_size
Original list [11, 45, 8, 23, 14, 12, 78, 45, 89]
Chunk 0 [11, 45, 8]
After reversing it [8, 45, 11]
Chunk 1 [23, 14, 12]
After reversing it [12, 14, 23]
Chunk 2 [78, 45, 89]
After reversing it [89, 45, 78]
len() functionslice(start, end, step) function and reverse it using the reversed() functionstart and end value will changeFirst understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.
Sign in to save your progress and continue exactly where you left off.