Reversing a copy of a list
Once in a while, we need to reverse the order of the items in a list
collection. Some algorithms, for example, produce results in a reversed order. We'll look at the way numbers converted to a specific base are often generated from least-significant to most-significant digit. We generally want to display the values with the most-significant digit first. This leads to a need to reverse the sequence of digits in a list.
We have two ways to reverse a list. First, there's the reverse()
method. Then there's this handy trick.
Getting ready
Let's say we're doing a conversion among number bases. We'll look at how a number is represented in a base, and how we can compute that representation from a number.
Any value, v, can be defined as a polynomial function of the various digits, dn, in a given base, b:
v = dn × bn + dn-1 × bn-1 + dn-2 × bn-2 + ... + d1 × b + d0
A rational number has a finite number of digits. An irrational number would have an infinite series of digits.
For...