aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-01 14:08:01 +0000
committerGitHub <noreply@github.com>2024-01-01 14:08:01 +0000
commit7c4575dde87a3440d4cde84d5f6336b8e228a9c7 (patch)
treebf21ba9c562412a4fea2c39f95f31b5d8258bee6
parent489f48cb062f503997004145ba2a41e3491f55de (diff)
parentab435ac20e78ef0026c349ad4de13345fd236ac9 (diff)
downloadperlweeklychallenge-club-7c4575dde87a3440d4cde84d5f6336b8e228a9c7.tar.gz
perlweeklychallenge-club-7c4575dde87a3440d4cde84d5f6336b8e228a9c7.tar.bz2
perlweeklychallenge-club-7c4575dde87a3440d4cde84d5f6336b8e228a9c7.zip
Merge pull request #9327 from jeanluc2020/jeanluc-250
Add solution 250.
-rw-r--r--challenge-250/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-250/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-250/jeanluc2020/perl/ch-1.pl66
-rwxr-xr-xchallenge-250/jeanluc2020/perl/ch-2.pl67
-rwxr-xr-xchallenge-250/jeanluc2020/python/ch-1.py60
-rwxr-xr-xchallenge-250/jeanluc2020/python/ch-2.py64
6 files changed, 259 insertions, 0 deletions
diff --git a/challenge-250/jeanluc2020/blog-1.txt b/challenge-250/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..6918762638
--- /dev/null
+++ b/challenge-250/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-250-1.html
diff --git a/challenge-250/jeanluc2020/blog-2.txt b/challenge-250/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..c2531c7877
--- /dev/null
+++ b/challenge-250/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-250-2.html
diff --git a/challenge-250/jeanluc2020/perl/ch-1.pl b/challenge-250/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..2988905c26
--- /dev/null
+++ b/challenge-250/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK1
+#
+# Task 1: Smallest Index
+# ======================
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the smallest index i such that i mod 10 == $ints[i]
+# otherwise return -1.
+#
+## Example 1
+##
+## Input: @ints = (0, 1, 2)
+## Output: 0
+##
+## i=0: 0 mod 10 = 0 == $ints[0].
+## i=1: 1 mod 10 = 1 == $ints[1].
+## i=2: 2 mod 10 = 2 == $ints[2].
+## All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+#
+## Example 2
+##
+## Input: @ints = (4, 3, 2, 1)
+## Output: 2
+##
+## i=0: 0 mod 10 = 0 != $ints[0].
+## i=1: 1 mod 10 = 1 != $ints[1].
+## i=2: 2 mod 10 = 2 == $ints[2].
+## i=3: 3 mod 10 = 3 != $ints[3].
+## 2 is the only index which has i mod 10 == $ints[i].
+#
+## Example 3
+##
+## Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
+## Output: -1
+## Explanation: No index satisfies i mod 10 == $ints[i].
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the array from the start to the end, return once
+# i mod 10 == $ints[i]. In the end, return -1 if no entry
+# satisfied the condition.
+
+use strict;
+use warnings;
+
+smallest_index(0, 1, 2);
+smallest_index(4, 3, 2, 1);
+smallest_index(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
+
+sub smallest_index {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ foreach my $i (0..$#ints) {
+ if( $i % 10 == $ints[$i] ) {
+ print "Output: $i\n";
+ return;
+ }
+ }
+ print "Output: -1\n";
+}
diff --git a/challenge-250/jeanluc2020/perl/ch-2.pl b/challenge-250/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..0d1ef5eec0
--- /dev/null
+++ b/challenge-250/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK2
+#
+# Task 2: Alphanumeric String Value
+# =================================
+#
+# You are given an array of alphanumeric strings.
+#
+# Write a script to return the maximum value of alphanumeric string in the
+# given array.
+#
+# The value of alphanumeric string can be defined as
+#
+# a) The numeric representation of the string in base 10 if it is made up of
+# digits only.
+# b) otherwise the length of the string
+#
+#
+## Example 1
+##
+## Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+## Output: 6
+##
+## "perl" consists of letters only so the value is 4.
+## "2" is digits only so the value is 2.
+## "000" is digits only so the value is 0.
+## "python" consits of letters so the value is 6.
+## "r4ku" consists of letters and digits so the value is 4.
+#
+## Example 2
+##
+## Input: @alphanumstr = ("001", "1", "000", "0001")
+## Output: 1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Try to match each string against a non-digit character. If that succeeds (or
+# the length is 0), use the length. If it doesn't, convert the string to an
+# integer.
+
+use strict;
+use warnings;
+
+alphanumeric_string_value("perl", "2", "000", "python", "r4ku");
+alphanumeric_string_value("001", "1", "000", "0001");
+
+sub alphanumeric_string_value {
+ my @alphanumstr = @_;
+ print "Input: (" . join(", ", @alphanumstr) . ")\n";
+ my $max = 0;
+ foreach my $string (@alphanumstr) {
+ # We skip the case where length($string) == 0, as that's the
+ # default for $max anyway
+ if(length($string) != 0) {
+ if($string =~ m/[^\d]/) {
+ $max = length($string) if length($string) > $max;
+ } else {
+ $max = int($string) if int($string) > $max;
+ }
+ }
+ }
+ print "Output: $max\n";
+}
diff --git a/challenge-250/jeanluc2020/python/ch-1.py b/challenge-250/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..000c1334d1
--- /dev/null
+++ b/challenge-250/jeanluc2020/python/ch-1.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK1
+#
+# Task 1: Smallest Index
+# ======================
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the smallest index i such that i mod 10 == $ints[i]
+# otherwise return -1.
+#
+## Example 1
+##
+## Input: @ints = (0, 1, 2)
+## Output: 0
+##
+## i=0: 0 mod 10 = 0 == $ints[0].
+## i=1: 1 mod 10 = 1 == $ints[1].
+## i=2: 2 mod 10 = 2 == $ints[2].
+## All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+#
+## Example 2
+##
+## Input: @ints = (4, 3, 2, 1)
+## Output: 2
+##
+## i=0: 0 mod 10 = 0 != $ints[0].
+## i=1: 1 mod 10 = 1 != $ints[1].
+## i=2: 2 mod 10 = 2 == $ints[2].
+## i=3: 3 mod 10 = 3 != $ints[3].
+## 2 is the only index which has i mod 10 == $ints[i].
+#
+## Example 3
+##
+## Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
+## Output: -1
+## Explanation: No index satisfies i mod 10 == $ints[i].
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the array from the start to the end, return once
+# i mod 10 == $ints[i]. In the end, return -1 if no entry
+# satisfied the condition.
+
+def smallest_index(ints: list):
+ print("Input: (", ", ".join([str(x) for x in ints]), ")")
+ for i in range(len(ints)):
+ if i % 10 == ints[i]:
+ print(f"Output: {i}")
+ return
+ print("Output: -1")
+
+smallest_index([0, 1, 2])
+smallest_index([4, 3, 2, 1])
+smallest_index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
+
diff --git a/challenge-250/jeanluc2020/python/ch-2.py b/challenge-250/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..3ffa4cf1e3
--- /dev/null
+++ b/challenge-250/jeanluc2020/python/ch-2.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK2
+#
+# Task 2: Alphanumeric String Value
+# =================================
+#
+# You are given an array of alphanumeric strings.
+#
+# Write a script to return the maximum value of alphanumeric string in the
+# given array.
+#
+# The value of alphanumeric string can be defined as
+#
+# a) The numeric representation of the string in base 10 if it is made up of
+# digits only.
+# b) otherwise the length of the string
+#
+#
+## Example 1
+##
+## Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+## Output: 6
+##
+## "perl" consists of letters only so the value is 4.
+## "2" is digits only so the value is 2.
+## "000" is digits only so the value is 0.
+## "python" consits of letters so the value is 6.
+## "r4ku" consists of letters and digits so the value is 4.
+#
+## Example 2
+##
+## Input: @alphanumstr = ("001", "1", "000", "0001")
+## Output: 1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Try to match each string against a non-digit character. If that succeeds (or
+# the length is 0), use the length. If it doesn't, convert the string to an
+# integer.
+
+import re
+
+def alphanumeric_string_value(alphanumstr: list):
+ print("Input: (", ", ".join(alphanumstr) , ")")
+ max = 0
+ for string in alphanumstr:
+ if len(string) > 0:
+ if re.match('[^\d]', string):
+ if len(string) > max:
+ max = len(string)
+ else:
+ if int(string) > max:
+ max = int(string)
+ print(f"Output: {max}")
+
+
+
+alphanumeric_string_value(["perl", "2", "000", "python", "r4ku"])
+alphanumeric_string_value(["001", "1", "000", "0001"])
+