Skip to content

Instantly share code, notes, and snippets.

@Taywee
Last active April 6, 2021 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taywee/ed844ef2a517fcff78da28daabed935b to your computer and use it in GitHub Desktop.
Save Taywee/ed844ef2a517fcff78da28daabed935b to your computer and use it in GitHub Desktop.
Simple commented example of a very simple hanoi backup rotation function, released MIT
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © 2016 Taylor C. Richberger <taywee@gmx.com>
# This code is released under MIT license
import locale
import argparse
import string
def hanoi(day, tapes):
# Make sure the recursion works on the proper schedule, with the bottom
# tape only being written on the last day of the cycle
day = day % (2 ** (tapes - 1))
# An obvious algorithm takes direct advantage of the fact that the
# frequency of the tape is correlated by its number, with tape 0 being
# 2**1, tape 1 being 2**2, and so on. This would make tape 0 be written
# every day, but by reversing the test order, each tape's frequency is
# reduced to half, because every other iteration is overridden by the next
# tape up in the cycle.
# If you are iterating days manually (rather than using the obvious days
# since epoch method), this has the advantage that the longest-living tape
# in the cycle is written first.
for i in reversed(range(tapes)):
if day % (2 ** i) == 0:
return i
def main():
locale.setlocale(locale.LC_ALL, '')
parser = argparse.ArgumentParser(description='Do Something')
parser.add_argument('-t', '--tapes', help='How many tapes', type=int, required=True)
args = parser.parse_args()
days = 2 ** (args.tapes - 1)
width = len(str(days))
for i in range(days):
tape = hanoi(day=i, tapes=args.tapes)
print('{day:0{width}d}:{spaces}{tape}'.format(day=i + 1, width=width, spaces=' ' * tape, tape=string.ascii_uppercase[tape]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment