From ac82e7424e28301f50fc01312e80fc40a3e58d6c Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 14 Sep 2020 19:29:25 -0400 Subject: perl code for challenge 78 task 1 --- challenge-078/walt-mankowski/perl/ch-1.pl | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-078/walt-mankowski/perl/ch-1.pl diff --git a/challenge-078/walt-mankowski/perl/ch-1.pl b/challenge-078/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..74240b922a --- /dev/null +++ b/challenge-078/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# TASK #1 › Leader Element +# Submitted by: Mohammad S Anwar +# +# You are given an array @A containing distinct integers. +# +# Write a script to find all leader elements in the array @A. Print +# (0) if none found. +# +# An element is leader if it is greater than all the elements to +# its right side. +# +# Example 1: +# +# Input: @A = (9, 10, 7, 5, 6, 1) +# Output: (10, 7, 6, 1) +# +# Example 2: +# +# Input: @A = (3, 4, 5) +# Output: (5) + +my @a = @ARGV; +my $max; +my @out; + +for (my $i = $#a; $i >= 0; $i--) { + if (!defined $max || $a[$i] > $max) { + unshift @out, $a[$i]; + $max = $a[$i]; + } +} + +say "@out"; -- cgit From b76525e04815af3aeaef0ef65962100e31bdb8e8 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 14 Sep 2020 19:39:12 -0400 Subject: perl code for challenge 78 task 2 --- challenge-078/walt-mankowski/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-078/walt-mankowski/perl/ch-2.pl diff --git a/challenge-078/walt-mankowski/perl/ch-2.pl b/challenge-078/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..f02de2052a --- /dev/null +++ b/challenge-078/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# TASK #2 › Left Rotation +# Submitted by: Mohammad S Anwar +# +# You are given array @A containing positive numbers and @B containing +# one or more indices from the array @A. +# +# Write a script to left rotate @A so that the number at the first +# index of @B becomes the first element in the array. Similary, left +# rotate @A again so that the number at the second index of @B becomes +# the first element in the array. + +my @a = split / /, $ARGV[0]; +my @b = split / /, $ARGV[1]; + +for my $b (@b) { + local $, = " "; + say rotate_by($b, @a); +} + +sub rotate_by($b, @a) { + return $b == 0 ? @a : (@a[$b..$#a], @a[0..$b-1]); +} -- cgit From 84851ae1c6c51cd582ba450707b34cc47939f060 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 14 Sep 2020 19:56:08 -0400 Subject: python 3 code for challenge 78 task 1 --- challenge-078/walt-mankowski/python/ch-1.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 challenge-078/walt-mankowski/python/ch-1.py diff --git a/challenge-078/walt-mankowski/python/ch-1.py b/challenge-078/walt-mankowski/python/ch-1.py new file mode 100644 index 0000000000..48fa69fb88 --- /dev/null +++ b/challenge-078/walt-mankowski/python/ch-1.py @@ -0,0 +1,12 @@ +from sys import argv + +a = [int(x) for x in argv[1:]] +best = None +out = [] + +for i in range(len(a)-1, -1, -1): + if best is None or a[i] > best: + out.insert(0, a[i]) + best = a[i] + +print(out) -- cgit From 753f17889618f3da31e9d0317065105818ba1d5a Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 14 Sep 2020 20:07:00 -0400 Subject: python 3 code for challenge 78 task 2 --- challenge-078/walt-mankowski/python/ch-2.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 challenge-078/walt-mankowski/python/ch-2.py diff --git a/challenge-078/walt-mankowski/python/ch-2.py b/challenge-078/walt-mankowski/python/ch-2.py new file mode 100644 index 0000000000..0d72428167 --- /dev/null +++ b/challenge-078/walt-mankowski/python/ch-2.py @@ -0,0 +1,10 @@ +from sys import argv + +def rotate_by(b, a): + return a[b:] + a[0:b] + +a = [int(x) for x in argv[1].split(' ')] +b = [int(x) for x in argv[2].split(' ')] + +for x in b: + print(rotate_by(x, a)) -- cgit From 044b8cef921ab1b550289a4e989c17a013bf0314 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 14 Sep 2020 20:25:47 -0400 Subject: I don't need the special case for $b==0 --- challenge-078/walt-mankowski/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-078/walt-mankowski/perl/ch-2.pl b/challenge-078/walt-mankowski/perl/ch-2.pl index f02de2052a..91095a9e6c 100644 --- a/challenge-078/walt-mankowski/perl/ch-2.pl +++ b/challenge-078/walt-mankowski/perl/ch-2.pl @@ -24,5 +24,5 @@ for my $b (@b) { } sub rotate_by($b, @a) { - return $b == 0 ? @a : (@a[$b..$#a], @a[0..$b-1]); + return (@a[$b..$#a], @a[0..$b-1]); } -- cgit From 9467640e2c7b6e28ddb8b3babc2b39442aeb9b68 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Mon, 14 Sep 2020 20:37:19 -0400 Subject: blog post for challenge 78 --- challenge-078/walt-mankowski/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-078/walt-mankowski/blog.txt diff --git a/challenge-078/walt-mankowski/blog.txt b/challenge-078/walt-mankowski/blog.txt new file mode 100644 index 0000000000..11dea55250 --- /dev/null +++ b/challenge-078/walt-mankowski/blog.txt @@ -0,0 +1 @@ +http://www.mawode.com/blog/blog/2020/09/14/perl-weekly-challenge-78/ -- cgit