aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-08-07 13:13:13 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-08-07 13:13:13 +0200
commitac173eb5298f8c1dfed037418c64e654f2442bbf (patch)
treeb53b24a9743cfbb90cbc9dc8242034f3a7ae70d2
parentabf00e571de9f15eba4b43afe92ca532f2543479 (diff)
downloadperlweeklychallenge-club-ac173eb5298f8c1dfed037418c64e654f2442bbf.tar.gz
perlweeklychallenge-club-ac173eb5298f8c1dfed037418c64e654f2442bbf.tar.bz2
perlweeklychallenge-club-ac173eb5298f8c1dfed037418c64e654f2442bbf.zip
feat(challenge-191/lubos-kolouch/perl,python/): Challenge 191 LK Perl Python
-rw-r--r--challenge-191/lubos-kolouch/perl/ch-1.pl35
-rw-r--r--challenge-191/lubos-kolouch/perl/ch-2.pl33
-rw-r--r--challenge-191/lubos-kolouch/python/ch-1.py28
-rw-r--r--challenge-191/lubos-kolouch/python/ch-2.py31
4 files changed, 127 insertions, 0 deletions
diff --git a/challenge-191/lubos-kolouch/perl/ch-1.pl b/challenge-191/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..417ae0be62
--- /dev/null
+++ b/challenge-191/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+sub twice_largest {
+ my (@list) = @_;
+ my $max = -1;
+ my $max_index = -1;
+
+ # Find the largest element and its index
+ for my $i ( 0 .. $#list ) {
+ if ( $list[$i] > $max ) {
+ $max = $list[$i];
+ $max_index = $i;
+ }
+ }
+
+ # Check if the largest element is at least twice as large as each of the other items
+ for my $i ( 0 .. $#list ) {
+ if ( $i != $max_index && $list[$i] * 2 > $max ) {
+ return -1;
+ }
+ }
+
+ return 1;
+}
+
+is( twice_largest( 1, 2, 3, 4 ), -1, 'Example 1' );
+is( twice_largest( 1, 2, 0, 5 ), 1, 'Example 2' );
+is( twice_largest( 2, 6, 3, 1 ), 1, 'Example 3' );
+is( twice_largest( 4, 5, 2, 3 ), -1, 'Example 4' );
+
+done_testing();
diff --git a/challenge-191/lubos-kolouch/perl/ch-2.pl b/challenge-191/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6a3ae1eb8c
--- /dev/null
+++ b/challenge-191/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use Algorithm::Permute;
+
+sub cute_list_count {
+ my ($n) = @_;
+ my @list = ( 1 .. $n );
+ my $count = 0;
+
+ Algorithm::Permute::permute {
+ if ( is_cute(@list) ) {
+ $count++;
+ }
+ }
+ @list;
+
+ return $count;
+}
+
+sub is_cute {
+ my (@list) = @_;
+ for my $i ( 0 .. $#list ) {
+ return 0 if ( $list[$i] % ( $i + 1 ) != 0 ) && ( ( $i + 1 ) % $list[$i] != 0 );
+ }
+ return 1;
+}
+
+is( cute_list_count(2), 2, 'Example' );
+
+done_testing();
diff --git a/challenge-191/lubos-kolouch/python/ch-1.py b/challenge-191/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..9bdf586c05
--- /dev/null
+++ b/challenge-191/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+
+
+def twice_largest(list_):
+ max_val = max(list_)
+ max_index = list_.index(max_val)
+
+ # Check if the largest element is at least twice as large as each of the other items
+ for i, val in enumerate(list_):
+ if i != max_index and val * 2 > max_val:
+ return -1
+
+ return 1
+
+
+class TestTwiceLargest(unittest.TestCase):
+ def test_examples(self):
+ self.assertEqual(twice_largest([1, 2, 3, 4]), -1)
+ self.assertEqual(twice_largest([1, 2, 0, 5]), 1)
+ self.assertEqual(twice_largest([2, 6, 3, 1]), 1)
+ self.assertEqual(twice_largest([4, 5, 2, 3]), -1)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-191/lubos-kolouch/python/ch-2.py b/challenge-191/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..1f6e9b4c36
--- /dev/null
+++ b/challenge-191/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from itertools import permutations
+import unittest
+
+
+def cute_list_count(n):
+ list_ = list(range(1, n + 1))
+ count = 0
+
+ for perm in permutations(list_):
+ if is_cute(perm):
+ count += 1
+
+ return count
+
+
+def is_cute(list_):
+ for i, val in enumerate(list_):
+ if val % (i + 1) != 0 and (i + 1) % val != 0:
+ return False
+ return True
+
+
+class TestCuteListCount(unittest.TestCase):
+ def test_example(self):
+ self.assertEqual(cute_list_count(2), 2)
+
+
+if __name__ == '__main__':
+ unittest.main()