diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-20 10:23:09 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-20 10:23:09 +0100 |
| commit | 5354c57e27196ca67ad5e821b3e999867c1aaef3 (patch) | |
| tree | 8b038891ff63bb639f48d4b97bddd6f194c4d92a | |
| parent | e2d0ee960837bca9ad2e85a88be5a0375e112ee5 (diff) | |
| download | perlweeklychallenge-club-5354c57e27196ca67ad5e821b3e999867c1aaef3.tar.gz perlweeklychallenge-club-5354c57e27196ca67ad5e821b3e999867c1aaef3.tar.bz2 perlweeklychallenge-club-5354c57e27196ca67ad5e821b3e999867c1aaef3.zip | |
Add Python solution to challenge 070
| -rw-r--r-- | challenge-061/paulo-custodio/python/ch-1.py | 2 | ||||
| -rw-r--r-- | challenge-069/paulo-custodio/python/ch-1.py | 2 | ||||
| -rw-r--r-- | challenge-069/paulo-custodio/python/ch-2.py | 2 | ||||
| -rw-r--r-- | challenge-070/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-070/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-070/paulo-custodio/python/ch-1.py | 55 | ||||
| -rw-r--r-- | challenge-070/paulo-custodio/python/ch-2.py | 68 |
7 files changed, 128 insertions, 5 deletions
diff --git a/challenge-061/paulo-custodio/python/ch-1.py b/challenge-061/paulo-custodio/python/ch-1.py index 47e232e6aa..4af02885dd 100644 --- a/challenge-061/paulo-custodio/python/ch-1.py +++ b/challenge-061/paulo-custodio/python/ch-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 061 # diff --git a/challenge-069/paulo-custodio/python/ch-1.py b/challenge-069/paulo-custodio/python/ch-1.py index 6fa58d6338..ca37f65b7d 100644 --- a/challenge-069/paulo-custodio/python/ch-1.py +++ b/challenge-069/paulo-custodio/python/ch-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 069 # diff --git a/challenge-069/paulo-custodio/python/ch-2.py b/challenge-069/paulo-custodio/python/ch-2.py index 2987057e01..d5fca17564 100644 --- a/challenge-069/paulo-custodio/python/ch-2.py +++ b/challenge-069/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 069 # diff --git a/challenge-070/paulo-custodio/perl/ch-1.pl b/challenge-070/paulo-custodio/perl/ch-1.pl index fc3c102124..6c931b3227 100644 --- a/challenge-070/paulo-custodio/perl/ch-1.pl +++ b/challenge-070/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 070 # -# TASK #1 › Character Swapping +# TASK #1 > Character Swapping # Submitted by: Mohammad S Anwar # You are given a string $S of size $N. # diff --git a/challenge-070/paulo-custodio/perl/ch-2.pl b/challenge-070/paulo-custodio/perl/ch-2.pl index c2873d5bbb..b2106b8252 100644 --- a/challenge-070/paulo-custodio/perl/ch-2.pl +++ b/challenge-070/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 070 # -# TASK #2 › Gray Code Sequence +# TASK #2 > Gray Code Sequence # Submitted by: Mohammad S Anwar # You are given an integer 2 <= $N <= 5. # diff --git a/challenge-070/paulo-custodio/python/ch-1.py b/challenge-070/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2c3ee74ebb --- /dev/null +++ b/challenge-070/paulo-custodio/python/ch-1.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +# Challenge 070 +# +# TASK #1 > Character Swapping +# Submitted by: Mohammad S Anwar +# You are given a string $S of size $N. +# +# You are also given swap count $C and offset $O such that +# $C >= 1, $O >= 1, $C <= $O and $C + $O <= $N. +# +# +# UPDATE: 2020-07-20 16:10:00 +# Pete Houston suggested to put additional constraint i.e. $C <= $O. +# He presented the use case $S = 'abcd', $C = 2, $O = 1. +# +# Write a script to perform character swapping like below: +# +# $S[ 1 % $N ] <=> $S[ (1 + $O) % $N ] +# $S[ 2 % $N ] <=> $S[ (2 + $O) % $N ] +# $S[ 3 % $N ] <=> $S[ (3 + $O) % $N ] +# ... +# ... +# $S[ $C % $N ] <=> $S[ ($C + $O) % $N ] +# Example 1 +# Input: +# $S = 'perlandraku' +# $C = 3 +# $O = 4 +# +# Character Swapping: +# swap 1: e <=> n = pnrlaedraku +# swap 2: r <=> d = pndlaerraku +# swap 3: l <=> r = pndraerlaku +# +# Output: +# pndraerlaku + +import sys + +def swap(s, c, o): + def swap1(s, c, o): + s_list = list(s) + p1 = c % len(s_list) + p2 = (c + o) % len(s_list) + s_list[p1], s_list[p2] = s_list[p2], s_list[p1] + return ''.join(s_list) + + for _ in range(1, c+1): + s = swap1(s, _, o) + return s + + +S, C, O = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) +print(swap(S, C, O)) diff --git a/challenge-070/paulo-custodio/python/ch-2.py b/challenge-070/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..3064383aef --- /dev/null +++ b/challenge-070/paulo-custodio/python/ch-2.py @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +# Challenge 070 +# +# TASK #2 > Gray Code Sequence +# Submitted by: Mohammad S Anwar +# You are given an integer 2 <= $N <= 5. +# +# Write a script to generate $N-bit gray code sequence. +# +# 2-bit Gray Code Sequence +# [0, 1, 3, 2] +# To generate the 3-bit Gray code sequence from the 2-bit Gray code sequence, +# follow the step below: +# +# 2-bit Gray Code sequence +# [0, 1, 3, 2] +# +# Binary form of the sequence +# a) S1 = [00, 01, 11, 10] +# +# Reverse of S1 +# b) S2 = [10, 11, 01, 00] +# +# Prefix all entries of S1 with '0' +# c) S1 = [000, 001, 011, 010] +# +# Prefix all entries of S2 with '1' +# d) S2 = [110, 111, 101, 100] +# +# Concatenate S1 and S2 gives 3-bit Gray Code sequence +# e) [000, 001, 011, 010, 110, 111, 101, 100] +# +# 3-bit Gray Code sequence +# [0, 1, 3, 2, 6, 7, 5, 4] +# Example +# Input: $N = 4 +# +# Output: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] + +import sys + +def gray(n): + if n < 2: + raise ValueError("N must be at least 2") + elif n == 2: + return [0, 1, 3, 2] + else: + # gray sequence of N-1 + prev = gray(n-1) + # binary form to S1 + s1 = [format(x, f'0{n-1}b') for x in prev] + # reverse to S2 + s2 = s1[::-1] + # prefix S1 with 0 + s1 = ['0' + x for x in s1] + # prefix S2 with 1 + s2 = ['1' + x for x in s2] + # concatenate + gray_seq = s1 + s2 + # convert to decimal + gray_seq = [int(x, 2) for x in gray_seq] + + return gray_seq + +if __name__ == "__main__": + N = int(sys.argv[1]) if len(sys.argv) > 1 else 2 + print(", ".join(map(str, gray(N)))) |
