aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorsaiftynet <saiftynet@gmail.com>2020-05-07 12:53:38 +0100
committersaiftynet <saiftynet@gmail.com>2020-05-07 12:53:38 +0100
commit9859892106de52182108cf03728343d9f5a00994 (patch)
tree1e731c0662a9ca804c2cb13d05253aeff2e33295 /challenge-059
parent8aec513ca9d2fa7b2256bd44307bf3f327e9fb6d (diff)
downloadperlweeklychallenge-club-9859892106de52182108cf03728343d9f5a00994.tar.gz
perlweeklychallenge-club-9859892106de52182108cf03728343d9f5a00994.tar.bz2
perlweeklychallenge-club-9859892106de52182108cf03728343d9f5a00994.zip
Challenge-059 solutions by saiftynet
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/saiftynet/perl/ch-1.pl31
-rw-r--r--challenge-059/saiftynet/perl/ch-2.pl26
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-059/saiftynet/perl/ch-1.pl b/challenge-059/saiftynet/perl/ch-1.pl
new file mode 100644
index 0000000000..31e750d415
--- /dev/null
+++ b/challenge-059/saiftynet/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/env/perl
+# Task 1 Challenge 059 Solution by saiftynet
+
+# Linked List: PWC 059
+# k = 3# Expected Output: 1 → 2 → 2 → 4 → 3 → 5.
+# given a "linked list" partition about a number, but retain
+# relative order of other numbers
+
+my @l=qw/1 4 3 2 5 2/;
+
+print join "->",partition(3);
+
+sub partition{
+
+# get the pivot and list
+ my ($k,@list)=@_;
+
+# prepare anonymnous lists containing equal, after and before numbers
+ my @seq=([],[],[]);
+
+# <=> return -1 if less (before), 0 if equal (pivot) and 1 of greater (after);
+# use this result as an index to push the numbers into one of these lists
+ foreach my $t (@l){
+ push @{$seq[$t<=>$k]},$t
+ };
+
+# return partitioned data
+ return @{$seq[-1]},@{$seq[0]},@{$seq[1]};
+
+}
+
diff --git a/challenge-059/saiftynet/perl/ch-2.pl b/challenge-059/saiftynet/perl/ch-2.pl
new file mode 100644
index 0000000000..b8831a1e26
--- /dev/null
+++ b/challenge-059/saiftynet/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/env/perl
+# Task 2 Challenge 059 Solution by saiftynet
+# -1
+
+# Bitsum- sum of bits that are different. This is effectively and xor
+# operation, followed by count of the bits that are 1. One can convert
+# the yiled of the xor operation into a binar string (e.g. using
+# sprintf "%b"), and counting the ones. Getting the list context yield
+# of a match operation, the reading that in scalar context gives us what
+# we want. A for loop that gets all possible pairs gives us a bit sum
+# of a list of numbers
+
+print bitsum(2,3,4);
+sub bitsum{
+ my @list=@_;
+ my $sum=0; # accumulator
+ foreach my $m (0..@list-2){ # usual 2 fors to get
+ foreach my $n ($m+1..$#list){ # all combinations
+ $sum += # get scalar
+ ()= # cast intoa array
+ (sprintf "%b", $list[$m]^$list[$n])# covert to binary
+ =~m/1/g; # get matches
+ }
+ }
+ return $sum;
+}