aboutsummaryrefslogtreecommitdiff
path: root/challenge-069/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-20 21:40:27 +0100
committerGitHub <noreply@github.com>2024-09-20 21:40:27 +0100
commitb521af146ef7c2bd338bc5355e2fc8f6c49cbd30 (patch)
tree8d8e24fcdbb56212b91343e1306cce425826dc53 /challenge-069/paulo-custodio/python/ch-2.py
parent962fb60ab30715e2dedc4fda8619042d22fba65e (diff)
parent4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 (diff)
downloadperlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.gz
perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.bz2
perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.zip
Merge pull request #10874 from pauloscustodio/master
Add Python solution to challenge 067
Diffstat (limited to 'challenge-069/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-069/paulo-custodio/python/ch-2.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-069/paulo-custodio/python/ch-2.py b/challenge-069/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..d5fca17564
--- /dev/null
+++ b/challenge-069/paulo-custodio/python/ch-2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+# Challenge 069
+#
+# TASK #2 > 0/1 String
+# Submitted by: Mohammad S Anwar
+# A 0/1 string is a string in which every character is either 0 or 1.
+#
+# Write a script to perform switch and reverse to generate S30 as described
+# below:
+#
+# switch:
+#
+# Every 0 becomes 1 and every 1 becomes 0. For example, "101" becomes "010".
+#
+# reverse:
+#
+# The string is reversed. For example, "001" becomes "100".
+# UPDATE (2020-07-13 17:00:00):
+# It was brought to my notice that generating S1000 string would be nearly
+# impossible. So I have decided to lower it down to S30. Please follow the rule
+# as below:
+#
+# S0 = ""
+# S1 = "0"
+# S2 = "001"
+# S3 = "0010011"
+#
+# SN = SN-1 + "0" + switch(reverse(SN-1))
+
+# Note: modified to S20, as S30 was taking forever
+
+N = 20
+
+def bits_switch(s):
+ return s.translate(str.maketrans('01', '10'))
+
+def bits_reverse(s):
+ return ''.join(reversed(s))
+
+prev = ""
+s = ""
+for _ in range(1, N + 1):
+ s = prev + "0" + bits_switch(bits_reverse(prev))
+ prev = s
+
+print(s)