From c4d4ea6d47d2bdb737cec5ad05472f63e7e762d3 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 4 May 2020 10:40:54 +0100 Subject: doesn't use a likned list directly because it's a weird thing to do in Perl or Raku --- challenge-059/simon-proctor/raku/ch-1.raku | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-059/simon-proctor/raku/ch-1.raku (limited to 'challenge-059') diff --git a/challenge-059/simon-proctor/raku/ch-1.raku b/challenge-059/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..3c485e913a --- /dev/null +++ b/challenge-059/simon-proctor/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + +use v6; + +#| Given an inflaction point and a list of values put all the items less than the inflection point +#| at the start of the last and all those great or equal after. Retain ordering. +sub MAIN ( + Int $inflection, #= Inflection point + *@rest where .all ~~ Int #= List of values +) { + my %c = @rest.classify( * >= $inflection ); + ( |%c{False}, |%c{True} ).say; +} -- cgit From 8d2a477f05329b16d4cf15c73814a7e63bf30ac3 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 4 May 2020 05:20:49 -0600 Subject: ch-2.p6 --- challenge-059/mark-anderson/raku/ch-2.p6 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-059/mark-anderson/raku/ch-2.p6 (limited to 'challenge-059') diff --git a/challenge-059/mark-anderson/raku/ch-2.p6 b/challenge-059/mark-anderson/raku/ch-2.p6 new file mode 100644 index 0000000000..6256e2aa69 --- /dev/null +++ b/challenge-059/mark-anderson/raku/ch-2.p6 @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +my @list = (2, 3, 4); + +my $sum; + +for @list.combinations(2) -> ($n1, $n2) { + $sum += f($n1, $n2); +} + +say $sum; + +sub f ($n1, $n2) { + ($n1 +^ $n2).fmt("%0b").comb("1").elems; +} -- cgit From 6bd84ac8178e14527fa37faa3786b99e1bde5611 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Mon, 4 May 2020 13:24:00 +0200 Subject: Solutions to the challenge 059. --- challenge-059/wanderdoc/perl/ch-1.pl | 78 ++++++++++++++++++++++++++++++++++++ challenge-059/wanderdoc/perl/ch-2.pl | 53 ++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 challenge-059/wanderdoc/perl/ch-1.pl create mode 100644 challenge-059/wanderdoc/perl/ch-2.pl (limited to 'challenge-059') diff --git a/challenge-059/wanderdoc/perl/ch-1.pl b/challenge-059/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..b1fa2ea621 --- /dev/null +++ b/challenge-059/wanderdoc/perl/ch-1.pl @@ -0,0 +1,78 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + + + +=prompt +You are given a linked list and a value k. Write a script to partition the linked list such that all nodes less than k come before nodes greater than or equal to k. Make sure you preserve the original relative order of the nodes in each of the two partitions. +For example: +Linked List: 1 → 4 → 3 → 2 → 5 → 2 +k = 3 +Expected Output: 1 → 2 → 2 → 4 → 3 → 5. + +=cut + + +my $K = shift || 3; +my @input = map {(1 .. 10)[rand 10] } 1 .. 10; # (1, 4, 3, 2, 5, 2); +print join(' -> ', @input), $/; + + +# LL-Implementation. +# https://www.slideshare.net/lembark/perly-linked-lists + +my $list = [[]]; + +my $node = $list->[0]; +for my $val ( @input ) +{ + @$node = ([], $val); + $node = $node->[0]; +} + +my $part_uK = [[]]; +my $node_uK = $part_uK->[0]; + +my $part_oK = [[]]; +my $node_oK = $part_oK->[0]; + + +$node = $list->[0]; # Back to begin. + +while ($node) +{ + if ( $node->[1] and $node->[1] < $K ) + { + @$node_uK = ([], $node->[1]); + $node_uK = $node_uK->[0]; + + } + elsif ( $node->[1] and $node->[1] >= $K ) + { + @$node_oK = ([], $node->[1]); + $node_oK = $node_oK->[0]; + } + $node = $node->[0]; +} + +@$node_uK = @{@$part_oK[0]}; # Now all the partitioned LL is in $part_uK. +# $node_uK was already at the end of the list. + +# Free memory. +undef $list; +undef $part_oK; + + + +# Output. +my $n_all = $part_uK->[0]; +while ( $n_all ) +{ + print $n_all->[1] if $n_all->[1]; + + $n_all = $n_all->[0]; + print ' -> ' if ($n_all and $n_all->[1]); + +} +print $/; \ No newline at end of file diff --git a/challenge-059/wanderdoc/perl/ch-2.pl b/challenge-059/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..e4b5b4b1e4 --- /dev/null +++ b/challenge-059/wanderdoc/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +For this task, you will most likely need a function f(a,b) which returns the count of different bits of binary representation of a and b. +For example, f(1,3) = 1, since: +Binary representation of 1 = 01 +Binary representation of 3 = 11 + +There is only 1 different bit. Therefore the subroutine should return 1. Note that if one number is longer than the other in binary, the most significant bits of the smaller number are padded (i.e., they are assumed to be zeroes). +Script Output + +You script should accept n positive numbers. Your script should sum the result of f(a,b) for every pair of numbers given: +For example, given 2, 3, 4, the output would be 6, since f(2,3) + f(2,4) + f(3,4) = 1 + 2 + 3 = 6 + +=cut + +use List::Util qw(max); + +sub pairwise_difbits +{ + my ($n1, $n2) = @_; + my $max = max(map length(sprintf("%b", $_)), ($n1, $n2)); + ($n1, $n2) = map sprintf("%0${max}b", $_) , ($n1, $n2); + + my $count = 0; + for my $i ( 0 .. $max - 1 ) + { + $count++ if substr($n1, $i, 1) != substr($n2, $i, 1); + } + return $count; +} + +sub sumdif +{ + my @numbers = @_; + my $sum; + + + for my $i ( 0 .. $#numbers - 1 ) + { + for my $j ( $i .. $#numbers ) + { + next if $i == $j; + $sum += pairwise_difbits(@numbers[$i, $j]); + } + } + + return $sum; +} + +print sumdif ( 1, 2, 3, 4, 5 ), $/; \ No newline at end of file -- cgit From aa4ec3a5b1ef22a10de33fc73de3debb0cacb824 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 4 May 2020 05:31:03 -0600 Subject: ch-2.p6 --- challenge-059/mark-anderson/raku/ch-2.p6 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-059') diff --git a/challenge-059/mark-anderson/raku/ch-2.p6 b/challenge-059/mark-anderson/raku/ch-2.p6 index 6256e2aa69..632161f883 100644 --- a/challenge-059/mark-anderson/raku/ch-2.p6 +++ b/challenge-059/mark-anderson/raku/ch-2.p6 @@ -5,11 +5,11 @@ my @list = (2, 3, 4); my $sum; for @list.combinations(2) -> ($n1, $n2) { - $sum += f($n1, $n2); + $sum += f($n1, $n2); } say $sum; sub f ($n1, $n2) { - ($n1 +^ $n2).fmt("%0b").comb("1").elems; + ($n1 +^ $n2).fmt("%b").comb("1").elems; } -- cgit