diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-15 16:14:59 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-15 16:14:59 -0700 |
| commit | 5cef1ec4cd06891b4369560dab9c5ae7d7e635df (patch) | |
| tree | 3e187112e3a28d56639ea9ee052e0969e5971b8f | |
| parent | 962c735ba8477e8fc4524901eaee3c5519911652 (diff) | |
| parent | f726b239a28fd76164c8c6404fce38130ec5ab01 (diff) | |
| download | perlweeklychallenge-club-5cef1ec4cd06891b4369560dab9c5ae7d7e635df.tar.gz perlweeklychallenge-club-5cef1ec4cd06891b4369560dab9c5ae7d7e635df.tar.bz2 perlweeklychallenge-club-5cef1ec4cd06891b4369560dab9c5ae7d7e635df.zip | |
Merge pull request #1410 from user-person/branch-for-challenge-051
User-person's solutions for challenge 51.
| -rw-r--r-- | challenge-051/user-person/.DS_Store | bin | 0 -> 6148 bytes | |||
| -rwxr-xr-x | challenge-051/user-person/perl/ch-1.pl | 79 | ||||
| -rwxr-xr-x | challenge-051/user-person/perl/ch-2.pl | 68 | ||||
| -rwxr-xr-x | challenge-051/user-person/python/ch-1.py | 72 | ||||
| -rwxr-xr-x | challenge-051/user-person/python/ch-2.py | 69 |
5 files changed, 288 insertions, 0 deletions
diff --git a/challenge-051/user-person/.DS_Store b/challenge-051/user-person/.DS_Store Binary files differnew file mode 100644 index 0000000000..921f2b82b8 --- /dev/null +++ b/challenge-051/user-person/.DS_Store diff --git a/challenge-051/user-person/perl/ch-1.pl b/challenge-051/user-person/perl/ch-1.pl new file mode 100755 index 0000000000..6c046f2891 --- /dev/null +++ b/challenge-051/user-person/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-051/ # +# # +# 3 Sum # +# Given an array @L of integers. Write a script to find all unique # +# triplets such that a + b + c is same as the given target T. Also make # +# sure a <= b <= c. # +# # +# https://en.wikipedia.org/wiki/3SUM # +# # +# Example: # +# # +# @L = (-25, -10, -7, -3, 2, 4, 8, 10); # +# # +# One such triplet for target 0 i.e. -10 + 2 + 8 = 0. # +# # +########################################################################### + +use strict; +use warnings; + +my @L = (-25, -10, -7, -3, 2, 4, 8, 10); +my $T = 0; + +if (@ARGV) { + + my $input = "@ARGV"; + my %digSeen = (); + $input =~ s{[][)(, ]+}{ }g; + $input =~ s{\A\s+|\s+\Z}{}; + @L = split m{ }, $input; + + foreach (@L) { + $_ =~ s{\A0+\Z}{0}; + $_ =~ s{\A0+(-?\d+)}{$1}; + } + + @L = sort { $a <=> $b } @L; + +} else { + my $prettyL = join ', ', @L; + print "Using default input \@L: [$prettyL]\n"; + +} + +my %seen = (); + +for ( my $i = 0; $i <= $#L-2; ++$i ) { + for ( my $j = $i+1; $j <= $#L-1; ++$j ) { + INNER: + for ( my $k = $j+1; $k <= $#L ; ++$k ) { + + my $sum = $L[$i] + $L[$j] + $L[$k]; + + if ($sum == $T) { + my $string = "$L[$i] + $L[$j] + $L[$k] = $T"; + if (exists($seen{$string})) { + next INNER; + } else { + print $string . "\n" if $sum == $T; + $seen{$string}++; + } + } + } + } +} + +__END__ +output: + +Using default input @L: [-25, -10, -7, -3, 2, 4, 8, 10] +-10 + 2 + 8 = 0 +-7 + -3 + 10 = 0 diff --git a/challenge-051/user-person/perl/ch-2.pl b/challenge-051/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..36a692878c --- /dev/null +++ b/challenge-051/user-person/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-051/ # +# # +# Colourful Number # +# Write a script to display all Colorful Number with 3 digits. # +# # +# A number can be declare Colorful Number where all the products of # +# consecutive subsets of digit are different. # +# # +# For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 # +# are unique. # +# # +########################################################################### + +use strict; +use warnings; +use diagnostics; + +# The range to search can be narrowed. +# 3 digit numbers gives us a beginning range of 100 .. 999 + +# Q(first appearance) == Q(second appearance) +# Numbers with repetitions are not colourful numbers. + +# 0 == ( Q x 0 ) +# Numbers with 0s are not colorful numbers. + +# Q == ( Q x 1 ) +# Numbers with 1s are not colorful numbers. + +# 100 no, 200, 230, 231, 232, 233 ... 234 seems right. +# 999 no, 989, 988 ... 987 seems right. + +my $MIN = 234; +my $MAX = 987; + +LOOP: +for (my $i = $MIN; $i <= $MAX; ++$i) { + my %seen = (); + my @d = (split //, $i ); + + foreach ( $d[0], $d[1], $d[2], $d[0]*$d[1], $d[1]*$d[2], $d[0]*$d[1]*$d[2]) { + + exists $seen{$_} ? next LOOP : $seen{$_}++ ; + + } + print "$i\n"; +} + +__END__ +Output is 328 lines / numbers. +output: + +234 +235 +237 +. +. +. +985 +986 +987 diff --git a/challenge-051/user-person/python/ch-1.py b/challenge-051/user-person/python/ch-1.py new file mode 100755 index 0000000000..e469149cd1 --- /dev/null +++ b/challenge-051/user-person/python/ch-1.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-051/ # +# # +# 3 Sum # +# Given an array @L of integers. Write a script to find all unique # +# triplets such that a + b + c is same as the given target T. Also make # +# sure a <= b <= c. # +# # +# https://en.wikipedia.org/wiki/3SUM # +# # +# Example: # +# # +# @L = (-25, -10, -7, -3, 2, 4, 8, 10); # +# # +# One such triplet for target 0 i.e. -10 + 2 + 8 = 0. # +# # +########################################################################### + +import re +import sys + +L = [-25, -10, -7, -3, 2, 4, 8, 10] +T = 0 + +if len(sys.argv) > 1: + + input = ' '.join(sys.argv[1:]) + input = re.sub(r'[][)(, ]+', ' ', input) + input = re.sub(r'\A\s+|\s+\Z', '', input) + L = re.split(r' ', input) + + for num in L: + num = re.sub(r'\A0+\Z', '0', num) + numPat = re.compile(r'\A0+(-?\d+)') + num = re.sub(numPat, r'\1', num) + + L = list(map(int,L)) + +else: + + print('Using default input L:',L) + +L.sort() +seen = {} + +for i in range( len(L)-2): + for j in range(i+1, len(L)-1): + for k in range(j+1, len(L) ): + + sum = L[i] + L[j] + L[k] + + if sum == T: + digString = '%i + %i + %i = %i' % ( L[i], L[j], L[k], T) + if digString in seen: + continue + else: + print(digString) + seen.update({digString : True}) + +# output: +# +# Using default input L: [-25, -10, -7, -3, 2, 4, 8, 10] +# -10 + 2 + 8 = 0 +# -7 + -3 + 10 = 0 + + diff --git a/challenge-051/user-person/python/ch-2.py b/challenge-051/user-person/python/ch-2.py new file mode 100755 index 0000000000..85b3394bb6 --- /dev/null +++ b/challenge-051/user-person/python/ch-2.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-051/ # +# # +# Colourful Number # +# Write a script to display all Colorful Number with 3 digits. # +# # +# A number can be declare Colorful Number where all the products of # +# consecutive subsets of digit are different. # +# # +# For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 # +# are unique. # +# # +########################################################################### + +# The range to search can be narrowed. +# 3 digit numbers gives us a beginning range of 100 .. 999 + +# Q(first appearance) == Q(second appearance) +# Numbers with repetitions are not colourful numbers. + +# 0 == ( Q x 0 ) +# Numbers with 0s are not colorful numbers. + +# Q == ( Q x 1 ) +# Numbers with 1s are not colorful numbers. + +# 100 no, 200, 230, 231, 232, 233 ... 234 seems right. +# 999 no, 989, 988 ... 987 seems right. + +MIN = 234 +MAX = 987 + +for i in range(MIN, MAX+1): + + printThis = True +# seen = {} + + d = list(str(i)) + d = list(map(int,d)) + + products = [ d[0], d[1], d[2], d[0]*d[1], d[1]*d[2], d[0]*d[1]*d[2] ] + + for prdt in products: + if products.count(prdt) > 1: + printThis = False +# else: +# seen.update({i : True}) + + if printThis: + print(i) + +# Output is 328 lines / numbers. +# output: +# +# 234 +# 235 +# 237 +# . +# . +# . +# 985 +# 986 +# 987 |
