listtools.truncate_to_sum — Abjad v1.1.1 documentation

### Navigation

- index
- modules |
- next |
- previous |
- Project Abjad »
- Abjad v1.1.1 documentation »
- Abjad API »

# listtools.truncate_to_sum

`abjad.tools.listtools.` `truncate_to_sum`(l, total)

  Truncate list l such that `sum(l) == total`.

      abjad> for n in range(10):
      ...     print n, listtools.truncate_to_sum([2, 2, 2], n)
      ...
      0 []
      1 [1]
      2 [2]
      3 [2, 1]
      4 [2, 2]
      5 [2, 2, 1]
      6 [2, 2, 2]
      7 [2, 2, 2]
      8 [2, 2, 2]
      9 [2, 2, 2]

      abjad> l = [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
      abjad> for n in range(10):
      ...     print n, listtools.truncate_to_sum(l, n)
      ...
      0 []
      1 [-1, 2]
      2 [-1, 2, -3, 4]
      3 [-1, 2, -3, 4, -5, 6]
      4 [-1, 2, -3, 4, -5, 6, -7, 8]
      5 [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
      6 [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
      7 [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
      8 [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
      9 [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]

  Return empty list when `total == 0`:

      abjad> listtools.truncate_to_sum([1, 2, 3, 4, 5], 0)
      []

  Raise `TypeError` when l is not a list:

      abjad> listtools.truncate_to_sum('foo', 4)
      TypeError

  Raise `ValueError` on negative total:

      abjad> listtools.truncate_to_sum([2, 2, 2], -4)
      ValueError

### Navigation

- index
- modules |
- next |
- previous |
- Project Abjad »
- Abjad v1.1.1 documentation »
- Abjad API »