For the next set of examples we're going to need a data file containing some numbers. Using the code in recaman.py below, we'll write a sequence of numbers called Recaman's sequence to a text file, with one number per line:
import sys
from itertools import count, islice
def sequence():
"""Generate Recaman's sequence."""
seen = set()
a = 0
for n in count(1):
yield a
seen.add(a)
c = a - n
if c < 0 or c in seen:
c = a + n
a = c
def write_sequence(filename, num):
"""Write Recaman's sequence to a text file."""
f = open(filename, mode='wt', encoding='utf-8')
f.writelines("{0}\n".format(r)
for r in islice(sequence(), num + 1))
f.close()
if __name__ == '__main__...