aboutsummaryrefslogtreecommitdiff
path: root/challenge-114/stuart-little/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-114/stuart-little/python/ch-2.py')
-rwxr-xr-xchallenge-114/stuart-little/python/ch-2.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/challenge-114/stuart-little/python/ch-2.py b/challenge-114/stuart-little/python/ch-2.py
new file mode 100755
index 0000000000..a88d0f2adc
--- /dev/null
+++ b/challenge-114/stuart-little/python/ch-2.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+# run <script> <number>
+
+import re
+import sys
+
+binNr=f"0{int(sys.argv[1]):b}"
+print(f"Initial number in base two: {binNr}")
+
+nxt = re.sub(r"01(1*)$", r"10\1", binNr) if (int(sys.argv[1]) % 2) else re.sub(r"01(1*)(0*)$", r"10\2\1", binNr)
+print(f"Next number in base two: {nxt}")
+
+print(f"Next number in base two: {int(nxt, base=2)}")