aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-07 02:11:42 +0000
committerGitHub <noreply@github.com>2024-01-07 02:11:42 +0000
commit683d39b7b0f3256ce396eb91b1fc541cc1f823f8 (patch)
tree3253456a8047c9eb2dd2d193f5107fcc336853e6
parent994ef981f08217dba148893caf99068d9321c1d3 (diff)
parent7db266caa8179f83519b8597a5a2f5dac0e13650 (diff)
downloadperlweeklychallenge-club-683d39b7b0f3256ce396eb91b1fc541cc1f823f8.tar.gz
perlweeklychallenge-club-683d39b7b0f3256ce396eb91b1fc541cc1f823f8.tar.bz2
perlweeklychallenge-club-683d39b7b0f3256ce396eb91b1fc541cc1f823f8.zip
Merge pull request #9351 from LubosKolouch/master
feat(challenge-250/lubos-kolouch/perl,python,raku,blog): Challenge 250 LK Perl Python Raku blog
-rw-r--r--challenge-250/lubos-kolouch/blog.txt1
-rw-r--r--challenge-250/lubos-kolouch/perl/ch-1.pl16
-rw-r--r--challenge-250/lubos-kolouch/perl/ch-2.pl24
-rw-r--r--challenge-250/lubos-kolouch/python/ch-1.py17
-rw-r--r--challenge-250/lubos-kolouch/python/ch-2.py42
-rw-r--r--challenge-250/lubos-kolouch/raku/ch-1.raku18
-rw-r--r--challenge-250/lubos-kolouch/raku/ch-2.raku21
7 files changed, 139 insertions, 0 deletions
diff --git a/challenge-250/lubos-kolouch/blog.txt b/challenge-250/lubos-kolouch/blog.txt
new file mode 100644
index 0000000000..5e09ec54c5
--- /dev/null
+++ b/challenge-250/lubos-kolouch/blog.txt
@@ -0,0 +1 @@
+https://egroup.kolouch.org/nextcloud/sites/lubos/2023-12-25_Weekly_challenge_250
diff --git a/challenge-250/lubos-kolouch/perl/ch-1.pl b/challenge-250/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..9d9fe4aabf
--- /dev/null
+++ b/challenge-250/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+sub smallest_index {
+ my @ints = @_;
+ foreach my $i (0 .. $#ints) {
+ return $i if $i % 10 == $ints[$i];
+ }
+ return -1;
+}
+
+is(smallest_index(0, 1, 2), 0, 'Example 1');
+is(smallest_index(4, 3, 2, 1), 2, 'Example 2');
+is(smallest_index(1, 2, 3, 4, 5, 6, 7, 8, 9, 0), -1, 'Example 3');
diff --git a/challenge-250/lubos-kolouch/perl/ch-2.pl b/challenge-250/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..fc6f091fae
--- /dev/null
+++ b/challenge-250/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+use Test::More tests => 5;
+
+sub max_alphanum_value {
+ my @array = @_;
+ my $max_value = 0;
+
+ foreach my $s (@array) {
+ my $value = ($s =~ /^\d+$/ ? int($s) : length($s));
+ $max_value = $value if $value > $max_value;
+ }
+
+ return $max_value;
+}
+
+# Tests
+is(max_alphanum_value("perl", "2", "000", "python", "r4ku"), 6, 'Test Example 1');
+is(max_alphanum_value("001", "1", "000", "0001"), 1, 'Test Example 2');
+is(max_alphanum_value(), 0, 'Test Empty Array');
+is(max_alphanum_value("123", "456", "789"), 789, 'Test All Digits');
+is(max_alphanum_value("abc", "def", "ghi"), 3, 'Test All Letters');
+
+done_testing();
diff --git a/challenge-250/lubos-kolouch/python/ch-1.py b/challenge-250/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..0db8453abe
--- /dev/null
+++ b/challenge-250/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+
+import typing
+
+
+# Define a function to find the smallest index i such that i mod 10 == ints[i] using list comprehension
+def smallest_index(ints: list[int]) -> int:
+ # Create a list of indices that satisfy the condition using list comprehension
+ indices = [i for i in range(len(ints)) if i % 10 == ints[i]]
+ # Return the smallest index if the list is not empty, otherwise return -1
+ return min(indices) if indices else -1
+
+
+# Test the function with some examples
+print(smallest_index([0, 1, 2])) # Output: 0
+print(smallest_index([4, 3, 2, 1])) # Output: 2
+print(smallest_index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) # Output: -1
diff --git a/challenge-250/lubos-kolouch/python/ch-2.py b/challenge-250/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..c71b040533
--- /dev/null
+++ b/challenge-250/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,42 @@
+# Define a function to return the maximum value of alphanumeric string in an array
+def max_alphanum_value(array: list[str]) -> int:
+ # Initialize the maximum value to 0
+ max_value = 0
+ # Loop through each string in the array
+ for s in array:
+ # If the string is made up of digits only, convert it to an integer in base 10
+ if s.isdigit():
+ value = int(s, 10)
+ # Otherwise, the value is the length of the string
+ else:
+ value = len(s)
+ # Update the maximum value if the current value is greater
+ if value > max_value:
+ max_value = value
+ # Return the maximum value
+ return max_value
+
+
+# Test the function with some examples
+import unittest
+
+
+class TestMaxAlphanumValue(unittest.TestCase):
+ def test_example_1(self):
+ self.assertEqual(max_alphanum_value(["perl", "2", "000", "python", "r4ku"]), 6)
+
+ def test_example_2(self):
+ self.assertEqual(max_alphanum_value(["001", "1", "000", "0001"]), 1)
+
+ def test_empty_array(self):
+ self.assertEqual(max_alphanum_value([]), 0)
+
+ def test_all_digits(self):
+ self.assertEqual(max_alphanum_value(["123", "456", "789"]), 789)
+
+ def test_all_letters(self):
+ self.assertEqual(max_alphanum_value(["abc", "def", "ghi"]), 3)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-250/lubos-kolouch/raku/ch-1.raku b/challenge-250/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..0c3d885812
--- /dev/null
+++ b/challenge-250/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import typing
+
+
+# Define a function to find the smallest index i such that i mod 10 == ints[i] using list comprehension
+def smallest_index(ints: typing.List[int]) -> int:
+ # Create a list of indices that satisfy the condition using list comprehension
+ indices = [i for i in range(len(ints)) if i % 10 == ints[i]]
+ # Return the smallest index if the list is not empty, otherwise return -1
+ return min(indices) if indices else -1
+
+
+# Test the function with some examples
+print(smallest_index([0, 1, 2])) # Output: 0
+print(smallest_index([4, 3, 2, 1])) # Output: 2
+print(smallest_index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) # Output: -1
diff --git a/challenge-250/lubos-kolouch/raku/ch-2.raku b/challenge-250/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..5bdbfcd404
--- /dev/null
+++ b/challenge-250/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,21 @@
+use Test;
+
+sub max-alphanum-value(*@array) {
+ my $max-value = 0;
+
+ for @array -> $s {
+ my $value = $s ~~ /^\d+$/ ?? $s.Int !! $s.chars;
+ $max-value = $value if $value > $max-value;
+ }
+
+ return $max-value;
+}
+
+# Tests
+is max-alphanum-value("perl", "2", "000", "python", "r4ku"), 6, 'Test Example 1';
+is max-alphanum-value("001", "1", "000", "0001"), 1, 'Test Example 2';
+is max-alphanum-value(), 0, 'Test Empty Array';
+is max-alphanum-value("123", "456", "789"), 789, 'Test All Digits';
+is max-alphanum-value("abc", "def", "ghi"), 3, 'Test All Letters';
+
+done-testing;