aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-03 00:06:05 +0100
committerGitHub <noreply@github.com>2023-04-03 00:06:05 +0100
commit1f9e69e190c8bd944f2c0ee230b0317376791c5d (patch)
tree09a08cd9e943cd2aded97956f7ecd65e8366b133
parent41a72990876f7df2118e62da5046d66d160d1a89 (diff)
parent81d0737ace1c412703eb9b8d6f31883a63f0ead9 (diff)
downloadperlweeklychallenge-club-1f9e69e190c8bd944f2c0ee230b0317376791c5d.tar.gz
perlweeklychallenge-club-1f9e69e190c8bd944f2c0ee230b0317376791c5d.tar.bz2
perlweeklychallenge-club-1f9e69e190c8bd944f2c0ee230b0317376791c5d.zip
Merge pull request #7810 from LubosKolouch/master
Challenges 043 044 LK Perl Python
-rw-r--r--challenge-043/lubos-kolouch/perl/ch-1.pl56
-rw-r--r--challenge-043/lubos-kolouch/perl/ch-2.pl25
-rw-r--r--challenge-043/lubos-kolouch/python/ch-1.py29
-rw-r--r--challenge-043/lubos-kolouch/python/ch-2.py18
-rw-r--r--challenge-044/lubos-kolouch/perl/ch-1.pl28
-rw-r--r--challenge-044/lubos-kolouch/perl/ch-2.pl20
-rw-r--r--challenge-044/lubos-kolouch/python/ch-1.py20
-rw-r--r--challenge-044/lubos-kolouch/python/ch-2.py16
8 files changed, 212 insertions, 0 deletions
diff --git a/challenge-043/lubos-kolouch/perl/ch-1.pl b/challenge-043/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..b30d576f86
--- /dev/null
+++ b/challenge-043/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+use Algorithm::Combinatorics qw(permutations);
+
+# Initialize the ring numbers and available numbers
+my %rings = (
+ Blue => 8,
+ Black => undef,
+ Red => 9,
+ Yellow => 7,
+ Green => 5,
+);
+my @available = ( 1, 2, 3, 4, 6 );
+
+# Generate all possible permutations of the available numbers
+my $iter = permutations( \@available );
+
+# Try each permutation for a valid solution
+my @solutions;
+while ( my $p = $iter->next ) {
+ my ( $b, $k, $r, $y, $g ) = @$p;
+
+ # Skip if any number is repeated
+ next if ( $b == $k || $b == $r || $b == $y || $b == $g );
+ next if ( $k == $r || $k == $y || $k == $g );
+ next if ( $r == $y || $r == $g );
+ next if ( $y == $g );
+
+ # Check if the sum of numbers in each ring is 11
+ if ( $b + $k + $r == 11 && $r + $y + $g == 11 ) {
+
+ # Save the solution
+ $rings{Blue} = $b;
+ $rings{Black} = $k;
+ $rings{Red} = $r;
+ $rings{Yellow} = $y;
+ $rings{Green} = $g;
+ push @solutions, {%rings};
+ }
+}
+
+# Print the solutions
+if (@solutions) {
+ print "Found ", scalar @solutions, " solutions:\n";
+ for my $i ( 0 .. $#solutions ) {
+ print "Solution ", $i + 1, ":\n";
+ print " Blue: ", $solutions[$i]{Blue}, "\n";
+ print " Black: ", $solutions[$i]{Black}, "\n";
+ print " Red: ", $solutions[$i]{Red}, "\n";
+ print " Yellow: ", $solutions[$i]{Yellow}, "\n";
+ print " Green: ", $solutions[$i]{Green}, "\n";
+ }
+}
+else {
+ print "No solutions found.\n";
+}
diff --git a/challenge-043/lubos-kolouch/perl/ch-2.pl b/challenge-043/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..00ff0a69f1
--- /dev/null
+++ b/challenge-043/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+
+sub self_descriptive {
+ my ($base) = @_;
+ my $n = $base;
+ while (1) {
+ if ( sd( $n, $base ) ) {
+ print "$n\n";
+ }
+ $n++;
+ }
+}
+
+sub sd {
+ my ( $n, $base ) = @_;
+ my @digits = split //, $n;
+ my @counts = (0) x $base;
+ for my $d (@digits) {
+ $counts[$d]++;
+ }
+ return join( '', @counts ) == $n;
+}
+
+self_descriptive(10);
diff --git a/challenge-043/lubos-kolouch/python/ch-1.py b/challenge-043/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..39d4aa8bb9
--- /dev/null
+++ b/challenge-043/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from itertools import permutations
+
+# Initialize the ring numbers and available numbers
+rings = {"Blue": 8, "Black": None, "Red": 9, "Yellow": 7, "Green": 5}
+available = [1, 2, 3, 4, 6]
+
+# Generate all possible permutations of the available numbers
+for b, k, r, y, g in permutations(available):
+ # Skip if any number is repeated
+ if b == k or b == r or b == y or b == g:
+ continue
+ if k == r or k == y or k == g:
+ continue
+ if r == y or r == g:
+ continue
+ if y == g:
+ continue
+ # Check if the sum of numbers in each ring is 11
+ if b + k + r == 11 and r + y + g == 11:
+ # Save the solution
+ rings["Blue"] = b
+ rings["Black"] = k
+ rings["Red"] = r
+ rings["Yellow"] = y
+ rings["Green"] = g
+ print(rings)
diff --git a/challenge-043/lubos-kolouch/python/ch-2.py b/challenge-043/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7faf8d6be1
--- /dev/null
+++ b/challenge-043/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,18 @@
+def self_descriptive(base):
+ def sd(n):
+ digits = [int(d) for d in str(n)]
+ counts = [0] * base
+ for d in digits:
+ counts[d] += 1
+ return int("".join(map(str, counts))) == n
+
+ n = base
+ while True:
+ if sd(n):
+ yield n
+ n += 1
+
+
+gen = self_descriptive(10)
+for _ in range(5):
+ print(next(gen))
diff --git a/challenge-044/lubos-kolouch/perl/ch-1.pl b/challenge-044/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..f76b375086
--- /dev/null
+++ b/challenge-044/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+sub insert_operators {
+ my ( $str, $target ) = @_;
+ my @results;
+ helper( $str, $target, "", \@results );
+ return @results;
+}
+
+sub helper {
+ my ( $str, $target, $expr, $results ) = @_;
+ if ( $str eq "" ) {
+ if ( eval($expr) == $target ) {
+ push @$results, $expr;
+ }
+ return;
+ }
+ for my $i ( 1 .. length($str) ) {
+ my $left = substr( $str, 0, $i );
+ my $right = substr( $str, $i );
+ helper( $right, $target, "$expr+$left", $results );
+ helper( $right, $target, "$expr-$left", $results );
+ }
+}
+
+my @results = insert_operators( "123456789", 100 );
+print "$_\n" for @results;
diff --git a/challenge-044/lubos-kolouch/perl/ch-2.pl b/challenge-044/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..75a6d5429b
--- /dev/null
+++ b/challenge-044/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+
+my $goal = 200; # Target amount
+my $current = 1; # Current amount
+my $moves = 0; # Number of moves
+
+while ( $current < $goal ) {
+
+ # Check if doubling the current amount gets us closer to the goal
+ if ( $current * 2 <= $goal - $current - 1 ) {
+ $current *= 2;
+ }
+ else {
+ $current += 1;
+ }
+ $moves += 1;
+}
+
+print "To reach $goal, you need $moves moves.\n";
diff --git a/challenge-044/lubos-kolouch/python/ch-1.py b/challenge-044/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..4e58bd9a26
--- /dev/null
+++ b/challenge-044/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,20 @@
+def insert_operators(s, target):
+ results = []
+
+ def helper(s, expr):
+ if not s:
+ if eval(expr) == target:
+ results.append(expr)
+ return
+ for i in range(1, len(s) + 1):
+ left, right = s[:i], s[i:]
+ helper(right, expr + "+" + left)
+ helper(right, expr + "-" + left)
+
+ helper(s, "")
+ return results
+
+
+results = insert_operators("123456789", 100)
+for r in results:
+ print(r)
diff --git a/challenge-044/lubos-kolouch/python/ch-2.py b/challenge-044/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..4ad8efde17
--- /dev/null
+++ b/challenge-044/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+goal = 200 # Target amount
+current = 1 # Current amount
+moves = 0 # Number of moves
+
+while current < goal:
+ # Check if doubling the current amount gets us closer to the goal
+ if current * 2 <= goal - current - 1:
+ current *= 2
+ else:
+ current += 1
+ moves += 1
+
+print("To reach $", goal, ", you need", moves, "moves.")