aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-04 09:13:38 +0100
committerGitHub <noreply@github.com>2024-05-04 09:13:38 +0100
commita64a66880bb08e46128dcaf71ec3f62294c4610f (patch)
tree1c8c02b19cfad8b3696c46a4d3529e7578ae4350
parenta7c46968cf13c7ef534553da5a97512196ffda4e (diff)
parent41314b6fe9f42f433f2151d7762017e792f932e8 (diff)
downloadperlweeklychallenge-club-a64a66880bb08e46128dcaf71ec3f62294c4610f.tar.gz
perlweeklychallenge-club-a64a66880bb08e46128dcaf71ec3f62294c4610f.tar.bz2
perlweeklychallenge-club-a64a66880bb08e46128dcaf71ec3f62294c4610f.zip
Merge pull request #10036 from adamcrussell/challenge-267
initial commit
-rw-r--r--challenge-267/adam-russell/perl/ch-1.pl23
-rw-r--r--challenge-267/adam-russell/perl/ch-2.pl25
-rw-r--r--challenge-267/adam-russell/python/ch-1.py16
-rw-r--r--challenge-267/adam-russell/python/ch-2.py18
4 files changed, 82 insertions, 0 deletions
diff --git a/challenge-267/adam-russell/perl/ch-1.pl b/challenge-267/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..5eb7c382f5
--- /dev/null
+++ b/challenge-267/adam-russell/perl/ch-1.pl
@@ -0,0 +1,23 @@
+use v5.38;
+
+sub product_sign{
+ my @z = grep { $_ == 0 } @_;
+ return 0 if @z > 0;
+ my @n = grep { $_ < 1 } @_;
+ return 1 if @n % 2 == 0;
+ return -1 if @n >= 1;
+ return 1;
+}
+
+
+
+
+
+
+my @ints;
+@ints = (-1, -2, -3, -4, 3, 2, 1);
+say product_sign @ints;
+@ints = (1, 2, 0, -2, -1);
+say product_sign @ints;
+@ints = (-1, -1, 1, -1, 2);
+say product_sign @ints;
diff --git a/challenge-267/adam-russell/perl/ch-2.pl b/challenge-267/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..3df271ae5a
--- /dev/null
+++ b/challenge-267/adam-russell/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use v5.38;
+
+use constant WIDTH => 100;
+
+sub line_counts{
+ my $s = shift;
+ my($width, $line_count) = (0, 1);
+ do{
+ $width = $width + $_[ord($_) - ord(q/a/)];
+ if($width > WIDTH){
+ $width = $_[ord($_) - ord(q/a/)];
+ $line_count++;
+ }
+ } for split q//, $s;
+ return $line_count, $width;
+}
+
+my($s, @w);
+$s = q/abcdefghijklmnopqrstuvwxyz/;
+@w = (10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10);
+say join q/ /, line_counts $s, @w;
+
+$s = q/bbbcccdddaaa/;
+@w = (4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10);
+say join q/ /, line_counts $s, @w;
diff --git a/challenge-267/adam-russell/python/ch-1.py b/challenge-267/adam-russell/python/ch-1.py
new file mode 100644
index 0000000000..47f228adb7
--- /dev/null
+++ b/challenge-267/adam-russell/python/ch-1.py
@@ -0,0 +1,16 @@
+def product_sign(ints):
+ z = list(filter(lambda x: x == 0, ints))
+ if(len(z) > 0):
+ return 0
+ n = list(filter(lambda x: x < 0, ints))
+ if(len(n) % 2 == 1):
+ return -1
+ elif(len(n) > 0 and len(n) % 2 == 0):
+ return 1
+
+ints = (-1, -2, -3, -4, 3, 2, 1)
+print(product_sign(ints))
+ints = (1, 2, 0, -2, -1)
+print(product_sign(ints))
+ints = (-1, -1, 1, -1, 2)
+print(product_sign(ints))
diff --git a/challenge-267/adam-russell/python/ch-2.py b/challenge-267/adam-russell/python/ch-2.py
new file mode 100644
index 0000000000..a2a476eb12
--- /dev/null
+++ b/challenge-267/adam-russell/python/ch-2.py
@@ -0,0 +1,18 @@
+WIDTH = 100
+
+def line_counts(s, widths):
+ width = 0
+ line_count = 1
+ for c in s:
+ width = width + widths[ord(c) - ord("a")]
+ if(width > WIDTH):
+ width = widths[ord(c) - ord("a")]
+ line_count = line_count + 1
+ return line_count, width
+
+s = "abcdefghijklmnopqrstuvwxyz"
+widths = (10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
+print(line_counts(s, widths))
+s = "bbbcccdddaaa"
+widths = (4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
+print(line_counts(s, widths))