Given:
sample_list = [11, 45, 8, 23, 14, 12, 78, 45, 89]
Expected Outcome:
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]
Hint:
slice()
Solution:
len()
slice(start, end, step)
reversed()
start
end
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
slice()
function to get the chunk and reverse itSolution:
len()
functionslice(start, end, step)
function and reverse it using thereversed()
functionstart
andend
value will change