One-dimensional arrays

The 1D array: size, indices from 0 to n − 1, access and modification, traversal by index or by element, out-of-bounds access, shifting to insert or delete, and copying.

  • 25 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios
  6. Understand

A few cards from the deck

  • Array: what is it?

    Show the answer

    A sequence of cells of the same type stored side by side in memory, each identified by its position.

    Reading or writing a cell by its number always takes the same time: t[0] and t[999] are equally fast.

  • What does this program print?

    ```python
    t = [4, 7, 1, 9]
    for i in range(len(t) - 1):
    t[i] = t[i + 1]
    print(t)
    `

    • [7, 1, 9, 9]
    • [7, 1, 9, 4]
    • [4, 4, 7, 1]
    • [7, 1, 9]
    Show the answer

    [7, 1, 9, 9]

    Each cell receives the value of its right-hand neighbour; the last one is never modified and keeps 9. The array keeps its size: nothing is removed, one value is just duplicated.

  • An array of n cells has its indices from … to ….

    Show the answer

    An array of n cells has its indices from 0 to n - 1.

    With 12 cells, the last one has index 11: index n does not exist, and it is the most common off-by-one error.

  • Index: what is it?

    Show the answer

    The number that identifies a cell of an array; the first cell has number 0.

    It is the distance from the first cell, hence the 0. With n cells, the indices run from 0 to n − 1: the last cell is t[len(t) - 1].