diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-04-15 18:53:03 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-04-15 18:53:03 +0200 |
| commit | fc7f30eaf4f1b81354ea7a10b3240efccf28a084 (patch) | |
| tree | 7c661271e9e6262a4804dd715d5d0862a6a11676 /challenge-051 | |
| parent | 226037534e68a697ee01c2689e7585fd537f1b15 (diff) | |
| download | perlweeklychallenge-club-fc7f30eaf4f1b81354ea7a10b3240efccf28a084.tar.gz perlweeklychallenge-club-fc7f30eaf4f1b81354ea7a10b3240efccf28a084.tar.bz2 perlweeklychallenge-club-fc7f30eaf4f1b81354ea7a10b3240efccf28a084.zip | |
Challenge 051 LK Perl Python
Diffstat (limited to 'challenge-051')
| -rw-r--r-- | challenge-051/lubos-kolouch/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-051/lubos-kolouch/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-051/lubos-kolouch/python/ch-1.py | 37 | ||||
| -rw-r--r-- | challenge-051/lubos-kolouch/python/ch-2.py | 23 |
4 files changed, 128 insertions, 0 deletions
diff --git a/challenge-051/lubos-kolouch/perl/ch-1.pl b/challenge-051/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..9d56a4b87c --- /dev/null +++ b/challenge-051/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub find_unique_triplets { + my ( $target, @list ) = @_; + @list = sort { $a <=> $b } @list; + + my @triplets; + my $len = scalar @list; + + for ( my $i = 0 ; $i < $len - 2 ; $i++ ) { + my $left = $i + 1; + my $right = $len - 1; + + while ( $left < $right ) { + my $sum = $list[$i] + $list[$left] + $list[$right]; + + if ( $sum == $target ) { + push @triplets, [ $list[$i], $list[$left], $list[$right] ]; + $left++; + $right--; + } + elsif ( $sum < $target ) { + $left++; + } + else { + $right--; + } + } + } + + return \@triplets; +} + +my @L = ( -25, -10, -7, -3, 2, 4, 8, 10 ); +my $target = 0; + +my $triplets = find_unique_triplets( $target, @L ); +foreach my $triplet ( @{$triplets} ) { + print "[" . join( ", ", @{$triplet} ) . "]\n"; +} diff --git a/challenge-051/lubos-kolouch/perl/ch-2.pl b/challenge-051/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ba1425080d --- /dev/null +++ b/challenge-051/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub is_colorful { + my $number = shift; + my %products; + + my @digits = split //, $number; + my $len = scalar @digits; + + for ( my $i = 0 ; $i < $len ; $i++ ) { + my $product = 1; + for ( my $j = $i ; $j < $len ; $j++ ) { + $product *= $digits[$j]; + return 0 if exists $products{$product}; + $products{$product} = 1; + } + } + + return 1; +} + +for my $num ( 100 .. 999 ) { + print "$num\n" if is_colorful($num); +} diff --git a/challenge-051/lubos-kolouch/python/ch-1.py b/challenge-051/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..2338c572cf --- /dev/null +++ b/challenge-051/lubos-kolouch/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List, Tuple + + +def find_unique_triplets(target: int, lst: List[int]) -> List[Tuple[int, int, int]]: + lst.sort() + + triplets = [] + length = len(lst) + + for i in range(length - 2): + left = i + 1 + right = length - 1 + + while left < right: + s = lst[i] + lst[left] + lst[right] + + if s == target: + triplets.append((lst[i], lst[left], lst[right])) + left += 1 + right -= 1 + elif s < target: + left += 1 + else: + right -= 1 + + return triplets + +if __name__ == "__main__": + L = [-25, -10, -7, -3, 2, 4, 8, 10] + target = 0 + + triplets = find_unique_triplets(target, L) + for triplet in triplets: + print(triplet) diff --git a/challenge-051/lubos-kolouch/python/ch-2.py b/challenge-051/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..56605c1568 --- /dev/null +++ b/challenge-051/lubos-kolouch/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def is_colorful(number: int) -> bool: + products = set() + + digits = [int(d) for d in str(number)] + length = len(digits) + + for i in range(length): + product = 1 + for j in range(i, length): + product *= digits[j] + if product in products: + return False + products.add(product) + + return True + +if __name__ == "__main__": + for num in range(100, 1000): + if is_colorful(num): + print(num) |
