diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-20 09:41:59 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-20 09:41:59 +0100 |
| commit | e2d0ee960837bca9ad2e85a88be5a0375e112ee5 (patch) | |
| tree | fece814e1076485b256b1216b2bda67e1b3d03d9 | |
| parent | 6efda219c95b36111a3be91d0b7908f0f7e7bd55 (diff) | |
| download | perlweeklychallenge-club-e2d0ee960837bca9ad2e85a88be5a0375e112ee5.tar.gz perlweeklychallenge-club-e2d0ee960837bca9ad2e85a88be5a0375e112ee5.tar.bz2 perlweeklychallenge-club-e2d0ee960837bca9ad2e85a88be5a0375e112ee5.zip | |
Add Python solution to challenge 069
| -rw-r--r-- | challenge-069/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-069/paulo-custodio/perl/ch-2.pl | 16 | ||||
| -rw-r--r-- | challenge-069/paulo-custodio/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-069/paulo-custodio/python/ch-2.py | 47 |
4 files changed, 90 insertions, 9 deletions
diff --git a/challenge-069/paulo-custodio/perl/ch-1.pl b/challenge-069/paulo-custodio/perl/ch-1.pl index 1bbbf03f2e..04132dac76 100644 --- a/challenge-069/paulo-custodio/perl/ch-1.pl +++ b/challenge-069/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 069 # -# TASK #1 › Strobogrammatic Number +# TASK #1 > Strobogrammatic Number # Submitted by: Mohammad S Anwar # A strobogrammatic number is a number that looks the same when looked at upside # down. diff --git a/challenge-069/paulo-custodio/perl/ch-2.pl b/challenge-069/paulo-custodio/perl/ch-2.pl index 699d9f7240..e09eaa7437 100644 --- a/challenge-069/paulo-custodio/perl/ch-2.pl +++ b/challenge-069/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 069 # -# TASK #2 › 0/1 String +# 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. # @@ -11,22 +11,22 @@ # # switch: # -# Every 0 becomes 1 and every 1 becomes 0. For example, “101” becomes “010”. +# Every 0 becomes 1 and every 1 becomes 0. For example, "101" becomes "010". # # reverse: # -# The string is reversed. For example, "001” becomes “100”. +# 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” +# S0 = "" +# S1 = "0" +# S2 = "001" +# S3 = "0010011" # -# SN = SN-1 + “0” + switch(reverse(SN-1)) +# SN = SN-1 + "0" + switch(reverse(SN-1)) # Note: modified to S20, as S30 was taking forever diff --git a/challenge-069/paulo-custodio/python/ch-1.py b/challenge-069/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6fa58d6338 --- /dev/null +++ b/challenge-069/paulo-custodio/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Challenge 069 +# +# TASK #1 > Strobogrammatic Number +# Submitted by: Mohammad S Anwar +# A strobogrammatic number is a number that looks the same when looked at upside +# down. +# +# You are given two positive numbers $A and $B such that 1 <= $A <= $B <= 10^15. +# +# Write a script to print all strobogrammatic numbers between the given two +# numbers. +# +# Example +# Input: $A = 50, $B = 100 +# Output: 69, 88, 96 + +import sys + +A, B = map(int, sys.argv[1:3]) +out = [] + +def is_strobogrammatic(n): + if not all(c in '0689' for c in str(n)): + return False + inv = str(n).translate(str.maketrans('69', '96')) + return int(inv[::-1]) == n + +for n in range(A, B + 1): + if is_strobogrammatic(n): + out.append(n) + +print(", ".join(map(str, out))) 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..2987057e01 --- /dev/null +++ b/challenge-069/paulo-custodio/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# 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) |
