diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-06-05 14:14:30 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-06-05 14:14:30 +0200 |
| commit | c968d01a52b99e050fead41f876ccbc119e03dab (patch) | |
| tree | 7d64fd530e677f295dd419376e0d1e5f49e422ea | |
| parent | 401be1861472af6d62bbdeb0fe65f6ced1ca8f31 (diff) | |
| download | perlweeklychallenge-club-c968d01a52b99e050fead41f876ccbc119e03dab.tar.gz perlweeklychallenge-club-c968d01a52b99e050fead41f876ccbc119e03dab.tar.bz2 perlweeklychallenge-club-c968d01a52b99e050fead41f876ccbc119e03dab.zip | |
feat(challenge-220/lubos-kolouch/perl,python/): Challenge 220 LK Perl Python
| -rw-r--r-- | challenge-220/lubos-kolouch/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-220/lubos-kolouch/perl/ch-2.pl | 28 | ||||
| -rw-r--r-- | challenge-220/lubos-kolouch/python/ch-1.py | 13 | ||||
| -rw-r--r-- | challenge-220/lubos-kolouch/python/ch-2.py | 18 |
4 files changed, 78 insertions, 0 deletions
diff --git a/challenge-220/lubos-kolouch/perl/ch-1.pl b/challenge-220/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..befb480d2c --- /dev/null +++ b/challenge-220/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; + +sub common_chars { + my @words = @_; + my %common = map { lc($_) => 1 } split //, $words[0]; + for my $word (@words[1..$#words]) { + my %chars = map { lc($_) => 1 } split //, $word; + for my $char (keys %common) { + delete $common{$char} unless exists $chars{$char}; + } + } + return [sort keys %common]; +} + +# Test Cases +print join(", ", @{common_chars("Perl", "Rust", "Raku")}); # r +print join(", ", @{common_chars("love", "live", "leave")}); # e, l, v + diff --git a/challenge-220/lubos-kolouch/perl/ch-2.pl b/challenge-220/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..cc598f0eba --- /dev/null +++ b/challenge-220/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use Math::Complex; +use Algorithm::Permute; + +sub is_squareful { + my @arr = @_; + foreach my $i (0..$#arr-1) { + my $sum = $arr[$i] + $arr[$i+1]; + my $sqrt = sqrt($sum); + return 0 unless $sqrt == int($sqrt); + } + return 1; +} + +sub squareful_permutations { + my @ints = @_; + my %results; + my $p = Algorithm::Permute->new(\@ints); + while (my @perm = $p->next) { + $results{join(", ", @perm)} = \@perm if is_squareful(@perm); + } + return values %results; +} + +# Test Cases +print join(", ", @$_), "\n" for squareful_permutations(1, 17, 8); # 1, 8, 17 and 17, 8, 1 +print join(", ", @$_), "\n" for squareful_permutations(2, 2, 2); # 2, 2, 2 diff --git a/challenge-220/lubos-kolouch/python/ch-1.py b/challenge-220/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b91c0fc0f1 --- /dev/null +++ b/challenge-220/lubos-kolouch/python/ch-1.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def common_chars(words): + common = set(words[0].lower()) + for word in words[1:]: + common &= set(word.lower()) + return sorted(list(common)) + + +# Test Cases +print(common_chars(["Perl", "Rust", "Raku"])) # ['r'] +print(common_chars(["love", "live", "leave"])) # ['e', 'l', 'v'] diff --git a/challenge-220/lubos-kolouch/python/ch-2.py b/challenge-220/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..dc47fd6b9a --- /dev/null +++ b/challenge-220/lubos-kolouch/python/ch-2.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from itertools import permutations +import math + + +def is_squareful(arr): + return all(math.isqrt(arr[i] + arr[i+1])**2 == arr[i] + arr[i+1] for i in range(len(arr) - 1)) + + +def squareful_permutations(ints): + return list(set(perm for perm in permutations(ints) if is_squareful(perm))) + + +# Test Cases +print(squareful_permutations([1, 17, 8])) # [(1, 8, 17), (17, 8, 1)] +print(squareful_permutations([2, 2, 2])) # [(2, 2, 2)] |
