diff options
| author | Andinus <andinus@nand.sh> | 2020-11-15 14:11:42 +0530 |
|---|---|---|
| committer | Andinus <andinus@nand.sh> | 2020-11-15 14:11:42 +0530 |
| commit | a7850e5b04adfc898a0812d1f532fb19e490f987 (patch) | |
| tree | 50f36d22e989e7fae4e0c45cf92f61683bbddb35 | |
| parent | f1b99967e1fd48afaa02968bbb923c8af0cc0ff1 (diff) | |
| download | perlweeklychallenge-club-a7850e5b04adfc898a0812d1f532fb19e490f987.tar.gz perlweeklychallenge-club-a7850e5b04adfc898a0812d1f532fb19e490f987.tar.bz2 perlweeklychallenge-club-a7850e5b04adfc898a0812d1f532fb19e490f987.zip | |
Add challenge-086's ch-1 solution in Perl
| -rw-r--r-- | challenge-086/andinus/README | 68 | ||||
| -rw-r--r-- | challenge-086/andinus/README.org | 48 | ||||
| -rw-r--r-- | challenge-086/andinus/blog-1.txt | 1 | ||||
| -rwxr-xr-x | challenge-086/andinus/perl/ch-01.pl | 18 |
4 files changed, 134 insertions, 1 deletions
diff --git a/challenge-086/andinus/README b/challenge-086/andinus/README index 5c0544600b..b372bec8d3 100644 --- a/challenge-086/andinus/README +++ b/challenge-086/andinus/README @@ -1 +1,67 @@ -Solutions by Andinus. + ━━━━━━━━━━━━━━━ + CHALLENGE 086 + + Andinus + ━━━━━━━━━━━━━━━ + + + 2020-11-15 + + +Table of Contents +───────────────── + +1. Task 1 - Pair Difference +.. 1. Perl + + + + + +1 Task 1 - Pair Difference +══════════════════════════ + + You are given an array of integers @N and an integer $A. + + Write a script to find find if there exists a pair of elements in the + array whose difference is $A. + + Print 1 if exists otherwise 0. + + +1.1 Perl +──────── + + • Program: <file:perl/ch-1.pl> + + @N & $A are taken from stdin, $A is the last argument. + ┌──── + │ die "usage: ./ch-1.pl <integers \@N> <integer \$A>\n" + │ unless scalar @ARGV >= 3; + │ + │ my $A = pop @ARGV; + │ my @N = @ARGV; + └──── + + We just loop over @N over a loop over @N & find the difference. If + it's equal to `$A' or `-$A' then we print `1' & exit. The first loop + is `shift''ing the numbers out of array `@N' because we are matching + for both `$A' & `-$A' so we don't need the number again. + + For example, if `@N = [1, 2]' & we don't `shift' in first loop then + we'll perform 2 subtraction operations: `1 - 2' & `2 - 1' & we won't + have to match for `-$A' but if we just match for `-$A' then we can use + `shift' & we'll only have to perform 1 subtraction operation `1 - 2'. + + We assume subtraction costs more than matching with `-$A', that makes + this more efficient. But it doesn't matter much. + ┌──── + │ while (my $int = shift @N) { + │ foreach (@N) { + │ my $diff = $int - $_; + │ print "1\n" and exit 0 if ($diff == $A or $diff == -$A); + │ } + │ } + │ print "0\n"; + │ + └──── diff --git a/challenge-086/andinus/README.org b/challenge-086/andinus/README.org new file mode 100644 index 0000000000..46f1916e57 --- /dev/null +++ b/challenge-086/andinus/README.org @@ -0,0 +1,48 @@ +#+SETUPFILE: ~/.emacs.d/org-templates/level-2.org +#+HTML_LINK_UP: ../../writings/ +#+OPTIONS: toc:2 +#+EXPORT_FILE_NAME: index +#+DATE: 2020-11-15 +#+TITLE: Challenge 086 + +* Task 1 - Pair Difference +You are given an array of integers @N and an integer $A. + +Write a script to find find if there exists a pair of elements in the +array whose difference is $A. + +Print 1 if exists otherwise 0. +** Perl +- Program: [[file:perl/ch-1.pl]] + +@N & $A are taken from stdin, $A is the last argument. +#+BEGIN_SRC perl +die "usage: ./ch-1.pl <integers \@N> <integer \$A>\n" + unless scalar @ARGV >= 3; + +my $A = pop @ARGV; +my @N = @ARGV; +#+END_SRC + +We just loop over @N over a loop over @N & find the difference. If it's +equal to =$A= or =-$A= then we print =1= & exit. The first loop is =shift='ing +the numbers out of array =@N= because we are matching for both =$A= & =-$A= so +we don't need the number again. + +For example, if =@N = [1, 2]= & we don't =shift= in first loop then we'll +perform 2 subtraction operations: =1 - 2= & =2 - 1= & we won't have to match +for =-$A= but if we just match for =-$A= then we can use =shift= & we'll only +have to perform 1 subtraction operation =1 - 2=. + +We assume subtraction costs more than matching with =-$A=, that makes this +more efficient. But it doesn't matter much. +#+BEGIN_SRC perl +while (my $int = shift @N) { + foreach (@N) { + my $diff = $int - $_; + print "1\n" and exit 0 if ($diff == $A or $diff == -$A); + } +} +print "0\n"; + +#+END_SRC diff --git a/challenge-086/andinus/blog-1.txt b/challenge-086/andinus/blog-1.txt new file mode 100644 index 0000000000..67bbe6d0db --- /dev/null +++ b/challenge-086/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.nand.sh/pwc/challenge-086 diff --git a/challenge-086/andinus/perl/ch-01.pl b/challenge-086/andinus/perl/ch-01.pl new file mode 100755 index 0000000000..37b9a84153 --- /dev/null +++ b/challenge-086/andinus/perl/ch-01.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +die "usage: ./ch-1.pl <integers \@N> <integer \$A>\n" + unless scalar @ARGV >= 3; + +my $A = pop @ARGV; +my @N = @ARGV; + +while (my $int = shift @N) { + foreach (@N) { + my $diff = $int - $_; + print "1\n" and exit 0 if ($diff == $A or $diff == -$A); + } +} +print "0\n"; |
