diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-23 17:31:17 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-24 09:43:51 +0100 |
| commit | 915160b5e8bee9e5a712a9fcc6b047b788c78828 (patch) | |
| tree | e4204b787e4a7f8de97c3cb86c4a97a4fa976f1c | |
| parent | bd556209a6cc804912bb6c90aa2c5f8f99faef60 (diff) | |
| download | perlweeklychallenge-club-915160b5e8bee9e5a712a9fcc6b047b788c78828.tar.gz perlweeklychallenge-club-915160b5e8bee9e5a712a9fcc6b047b788c78828.tar.bz2 perlweeklychallenge-club-915160b5e8bee9e5a712a9fcc6b047b788c78828.zip | |
Add Python solution to challenge 073
| -rw-r--r-- | challenge-072/paulo-custodio/python/ch-2.py | 2 | ||||
| -rw-r--r-- | challenge-073/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-073/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-073/paulo-custodio/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-073/paulo-custodio/python/ch-2.py | 51 |
5 files changed, 90 insertions, 3 deletions
diff --git a/challenge-072/paulo-custodio/python/ch-2.py b/challenge-072/paulo-custodio/python/ch-2.py index 6d897d5b2b..5cace8e5eb 100644 --- a/challenge-072/paulo-custodio/python/ch-2.py +++ b/challenge-072/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 072 # diff --git a/challenge-073/paulo-custodio/perl/ch-1.pl b/challenge-073/paulo-custodio/perl/ch-1.pl index 734bef2566..904c257166 100644 --- a/challenge-073/paulo-custodio/perl/ch-1.pl +++ b/challenge-073/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 073 # -# TASK #1 › Min Sliding Window +# TASK #1 > Min Sliding Window # Submitted by: Mohammad S Anwar # You are given an array of integers @A and sliding window size $S. # diff --git a/challenge-073/paulo-custodio/perl/ch-2.pl b/challenge-073/paulo-custodio/perl/ch-2.pl index b82a968623..c670224942 100644 --- a/challenge-073/paulo-custodio/perl/ch-2.pl +++ b/challenge-073/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 073 # -# TASK #2 › Smallest Neighbour +# TASK #2 > Smallest Neighbour # Submitted by: Mohammad S Anwar # You are given an array of integers @A. # diff --git a/challenge-073/paulo-custodio/python/ch-1.py b/challenge-073/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..fb86229a6c --- /dev/null +++ b/challenge-073/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +# Challenge 073 +# +# TASK #1 > Min Sliding Window +# Submitted by: Mohammad S Anwar +# You are given an array of integers @A and sliding window size $S. +# +# Write a script to create an array of min from each sliding window. +# +# Example +# Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3 +# Output: (0, 0, 0, 2, 3, 3, 4, 4) +# +# [(1 5 0) 2 9 3 7 6 4 8] = Min (0) +# [1 (5 0 2) 9 3 7 6 4 8] = Min (0) +# [1 5 (0 2 9) 3 7 6 4 8] = Min (0) +# [1 5 0 (2 9 3) 7 6 4 8] = Min (2) +# [1 5 0 2 (9 3 7) 6 4 8] = Min (3) +# [1 5 0 2 9 (3 7 6) 4 8] = Min (3) +# [1 5 0 2 9 3 (7 6 4) 8] = Min (4) +# [1 5 0 2 9 3 7 (6 4 8)] = Min (4) + +import sys + +def min_sliding_window(s, a): + min_ = [] + for i in range(0, len(a)-s+1): + sub_ = a[i:i+s] + min_.append(min(sub_)) + return min_ + +S = int(sys.argv[1]) +M = list(map(int, sys.argv[2:])) +min_ = min_sliding_window(S, M) +print(", ".join([str(x) for x in min_])) diff --git a/challenge-073/paulo-custodio/python/ch-2.py b/challenge-073/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..8d28dc627f --- /dev/null +++ b/challenge-073/paulo-custodio/python/ch-2.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# Challenge 073 +# +# TASK #2 > Smallest Neighbour +# Submitted by: Mohammad S Anwar +# You are given an array of integers @A. +# +# Write a script to create an array that represents the smallest element to +# the left of each corresponding index. If none found then use 0. +# +# Example 1 +# Input: @A = (7, 8, 3, 12, 10) +# Output: (0, 7, 0, 3, 3) +# +# For index 0, the smallest number to the left of $A[0] i.e. 7 is none, so we +# put 0. +# For index 1, the smallest number to the left of $A[1] as compare to 8, in +# (7) is 7 so we put 7. +# For index 2, the smallest number to the left of $A[2] as compare to 3, in +# (7, 8) is none, so we put 0. +# For index 3, the smallest number to the left of $A[3] as compare to 12, in +# (7, 8, 3) is 3, so we put 3. +# For index 4, the smallest number to the left of $A[4] as compare to 10, in +# (7, 8, 3, 12) is 3, so we put 3 again. +# +# Example 2 +# Input: @A = (4, 6, 5) +# Output: (0, 4, 4) +# +# For index 0, the smallest number to the left of $A[0] is none, so we put 0. +# For index 1, the smallest number to the left of $A[1] as compare to 6, in +# (4) is 4, so we put 4. +# For index 2, the smallest number to the left of $A[2] as compare to 5, in +# (4, 6) is 4, so we put 4 again. + +import sys + +def smallest_left(a): + smallest = [0] + for i in range(1, len(a)): + min_ = min(a[:i]) + if min_ < a[i]: + smallest.append(min_) + else: + smallest.append(0) + return smallest + +A = list(map(int, sys.argv[1:])) +smallest = smallest_left(A) +print(", ".join([str(x) for x in smallest])) |
