aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2020-05-04 18:01:58 -0400
committerDave Jacoby <jacoby.david@gmail.com>2020-05-04 18:01:58 -0400
commit1474fb75aa8c1424addb901b955bcb823a70f63a (patch)
treec7bbe6bbdc93fc5ddade60830f0a377388ec8e40 /challenge-059
parent7ce5b5ddb80747dcbab87fb650c44d5a5c31cd29 (diff)
downloadperlweeklychallenge-club-1474fb75aa8c1424addb901b955bcb823a70f63a.tar.gz
perlweeklychallenge-club-1474fb75aa8c1424addb901b955bcb823a70f63a.tar.bz2
perlweeklychallenge-club-1474fb75aa8c1424addb901b955bcb823a70f63a.zip
Code for Challenge #59
Diffstat (limited to 'challenge-059')
-rwxr-xr-xchallenge-059/dave-jacoby/perl/ch-1.pl44
-rwxr-xr-xchallenge-059/dave-jacoby/perl/ch-2.pl45
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-059/dave-jacoby/perl/ch-1.pl b/challenge-059/dave-jacoby/perl/ch-1.pl
new file mode 100755
index 0000000000..0326409892
--- /dev/null
+++ b/challenge-059/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw{ postderef say signatures state switch };
+no warnings qw{ experimental };
+
+my $k = 3;
+my $input = [ 1, 4, 3, 2, 5, 2 ];
+
+say display_ll($input);
+my $output = task_1( $k, $input );
+say display_ll($output);
+
+# the "simple" version, not using actual linked lists
+# but not doing anything you can't regularly due with
+# singly-linked lists, basically shift/remove-first
+# and push/add-last. Adding the first element of the
+# second linked list to the end of the first linked
+# list makes it a longer linked list.
+
+# I think I'll have to make my Node code into an actual
+# linked list eventually
+
+sub task_1 ( $k, $array ) {
+ my $output = [];
+ my @below ;
+ my @above ;
+ while ( $array->@* ) {
+ my $l = shift $array->@*;
+ if ( $l < $k ) {
+ push @below, $l;
+ next;
+ }
+ push @above, $l;
+ }
+ push $output->@*, @below, @above;
+ return $output;
+}
+
+sub display_ll($array) {
+ return join ' -> ', $array->@*;
+}
diff --git a/challenge-059/dave-jacoby/perl/ch-2.pl b/challenge-059/dave-jacoby/perl/ch-2.pl
new file mode 100755
index 0000000000..7642c3e7ee
--- /dev/null
+++ b/challenge-059/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw{ postderef say signatures state switch };
+no warnings qw{ experimental };
+
+use List::Util qw{ sum };
+use Algorithm::Combinatorics 'combinations';
+
+use JSON;
+my $json = JSON->new->canonical->allow_nonref;
+
+say f2();
+say f2(1);
+say f2( 1, 3 );
+say f2( 2, 3, 4 );
+say f2( 2, 3, 4, 5 );
+say f2( 99,101 );
+
+# if array has < 2 entries, return 0 because there's not enough
+# to work with.
+# use combinations to get all possible combinations of n vals
+# - for example, w/ 1,2,3: [1,2],[1,3],[2,3]
+sub f2 ( @array ) {
+ return 0 if scalar @array < 2;
+ my $sum = 0;
+ for my $combo ( combinations( \@array, 2 ) ) {
+ my $f = f( $combo->@* );
+ $sum += $f;
+ }
+ return $sum;
+}
+
+# back to front:
+# $i ^ $j - XOR, which is $i or $j but not $i and $j
+# sprintf - make a string representation of a
+# binary number of the result
+# split // - turn '00001111' into [0,0,0,0,1,1,1,1]
+# sum - add all the numbers in the array together
+sub f ( $i, $j ) {
+ return sum split //, sprintf '%b', $i ^ $j;
+}
+