A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Slice list into 3 equal chunks and reverse each chunk using python programming
Q:

Slice list into 3 equal chunks and reverse each chunk using python programming

0

Slice list into 3 equal chunks and reverse each chunk

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]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Divide the length of a list by 3 to get the each chunk size
  • Run loop three times and use the slice() function to get the chunk and reverse it

Solution:

  • Get the length of a list using a len() function
  • Divide the length by 3 to get the chunk size
  • Run loop three times
  • In each iteration, get a chunk using a slice(start, end, step) function and reverse it using the reversed() function
  • In each iteration, start and end value will change
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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now