From c81724b6cb3ec4a583ef80d2376a08e92be208fd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Feb 2021 09:58:18 +0100 Subject: Task1 and 2 done. --- challenge-098/luca-ferrari/raku/ch-1.p6 | 14 ++++++++++++++ challenge-098/luca-ferrari/raku/ch-2.p6 | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 challenge-098/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-098/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-098/luca-ferrari/raku/ch-1.p6 b/challenge-098/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..9835075cef --- /dev/null +++ b/challenge-098/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#!raku + + +sub readN( Str $file-name, Int $how-many-chars where { $how-many-chars >= 1 } ) { + state %handles = (); + %handles{ $file-name } //= $file-name.IO.open; + return %handles{ $file-name }.readchars: $how-many-chars; +} + +sub MAIN( Str $file-name ) { + say readN( $file-name, 4 ); + say readN( $file-name, 4 ); + say readN( '/home/luca/.zshrc', 10 ); +} diff --git a/challenge-098/luca-ferrari/raku/ch-2.p6 b/challenge-098/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..dc9bbdd1dd --- /dev/null +++ b/challenge-098/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,27 @@ +#!raku + + +sub MAIN( ) { + my @N = 1,2,3,4, 7; + my $N = 6; + say "Array { @N } searching for $N"; + + # get the index if the element is there + given @N.grep: $N, :k { .say && exit if $_ } + + # if here the element is not there + # for @N.List.kv -> $k, $v { + # if ( $v > $N ) { + # @N = |@N[ 0 .. $k - 1 ], $N, |@N[ $k .. * ]; + # "Insert value $N at index $k".say; + # } + # } + + + given @N.grep: { $_ > $N }, :k { + @N = |@N[ 0 .. $_ - 1 ], $N, |@N[ $_ .. * ]; + "Insert value $N at index $_".say; + exit; + } + +} -- cgit From c1bbef5b5c58f150588b2939b4177f030af4f0a3 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 1 Feb 2021 09:18:45 +0000 Subject: first pass at code - with annotations --- challenge-098/james-smith/perl/bob.txt | 1 + challenge-098/james-smith/perl/ch-1.pl | 62 ++++++++++++++++++++++++++++++++ challenge-098/james-smith/perl/ch-2.pl | 32 +++++++++++++++++ challenge-098/james-smith/perl/input.txt | 1 + 4 files changed, 96 insertions(+) create mode 100644 challenge-098/james-smith/perl/bob.txt create mode 100644 challenge-098/james-smith/perl/ch-1.pl create mode 100644 challenge-098/james-smith/perl/ch-2.pl create mode 100644 challenge-098/james-smith/perl/input.txt diff --git a/challenge-098/james-smith/perl/bob.txt b/challenge-098/james-smith/perl/bob.txt new file mode 100644 index 0000000000..70e6a83d8d --- /dev/null +++ b/challenge-098/james-smith/perl/bob.txt @@ -0,0 +1 @@ +abcdefghijklmnop \ No newline at end of file diff --git a/challenge-098/james-smith/perl/ch-1.pl b/challenge-098/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..2d12b507cc --- /dev/null +++ b/challenge-098/james-smith/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +my %handles; + +is( readN( 'bob.txt', 4 ), 'abcd' ); +is( readN( 'input.txt', 4 ), '1234' ); +is( readN( 'bob.txt', 4 ), 'efgh' ); +is( readN( 'input.txt', 4 ), '5678' ); +is( readN( 'bob.txt', 4 ), 'ijkl' ); +is( readN( 'bob.txt', 4 ), 'mnop' ); +is( readN( 'input.txt', 4 ), '90' ); +is( readN( 'bob.txt', 4 ), '' ); +is( readN( 'input.txt', 4 ), '' ); +is( readN( 'bob.txt', 4 ), '' ); +is( readN( 'bob.txt', 4 ), '' ); + +## Ignore this bit checking clean up works! +done_testing(); + print "Open @{[ show_open() ]}\n"; +my $t = $handles{'bob.txt'}; +cleanup('fred.txt'); +cleanup('bob.txt'); print "Open @{[ show_open() ]}\n"; +<$t>; ## This will warn - prove that file handle has been closed +cleanup(); print "Open @{[ show_open() ]}\n"; +## End of bit to ignore.. + +sub readN { + my( $fn, $bytes ) = @_; + + ## Create a file handle if we don't already have one + unless( exists $handles{$fn} ) { + open $handles{$fn}, '<', $fn; + } + + ## Create a buffer for the return value + my $t = ''; + + ## Use "read" to read the $bytes bytes - these are put into 2nd parameter + ## If read returns undef it means it has reached the end of the file... + warn "Reached end of file $fn\n" unless read ${$handles{$fn}}, $t, $bytes; + + ## Return string + return $t; +} + +sub cleanup { + ## For neatness close all handles + ## delete returns the value of the has deleted + ## if filenames are passed then only those are cleaned up + close delete $handles{$_} foreach @_ ? grep { exists $handles{$_} } @_ : keys %handles; +} + +sub show_open { + ## Return a list of open filenames + return keys %handles; +} diff --git a/challenge-098/james-smith/perl/ch-2.pl b/challenge-098/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..072cc04d3c --- /dev/null +++ b/challenge-098/james-smith/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( insert_pos( [qw( 1 2 3 4)], 3), 2 ); +is( insert_pos( [qw( 1 3 5 7)], 6), 3 ); +is( insert_pos( [qw(12 14 16 18)], 10), 0 ); +is( insert_pos( [qw(11 13 15 17)], 19), 4 ); + +done_testing(); + +sub insert_pos { + my( $t, $l, $val ) = (0,@_); + + ## Repeat unless we have got to end of list or the new entry is greater than val + $t++ while ($t < @{$l}) && ($l->[$t] < $val); + + ## If we are after the end of the list (to avoid warning) OR + ## If we haven't found the entry then we use splice to insert it + splice @{$l},$t,0,$val if $t == @{$l} || $l->[$t] != $val; + + ## Warn to show splice has worked... + warn ">> $t ( @{$l} )\n"; ## Demonstrate splice + + ## Return the index of the number! + return $t; +} + diff --git a/challenge-098/james-smith/perl/input.txt b/challenge-098/james-smith/perl/input.txt new file mode 100644 index 0000000000..6a537b5b36 --- /dev/null +++ b/challenge-098/james-smith/perl/input.txt @@ -0,0 +1 @@ +1234567890 \ No newline at end of file -- cgit From f409a7d8397e21e4d31b4132e22ed8881d925eb0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Feb 2021 10:10:05 +0100 Subject: Task 2 done right. --- challenge-098/luca-ferrari/raku/ch-2.p6 | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/challenge-098/luca-ferrari/raku/ch-2.p6 b/challenge-098/luca-ferrari/raku/ch-2.p6 index dc9bbdd1dd..e0a24eddc5 100644 --- a/challenge-098/luca-ferrari/raku/ch-2.p6 +++ b/challenge-098/luca-ferrari/raku/ch-2.p6 @@ -1,27 +1,24 @@ #!raku - -sub MAIN( ) { - my @N = 1,2,3,4, 7; - my $N = 6; - say "Array { @N } searching for $N"; +# Get an array of integers, the last value is the need to search for +# while all the other values are the array to search into. +# For example: +# ch-2.p6 1 2 3 7 8 9 5 +# means +# @N = 1,2,3,7,8,9 +# $N = 5 +sub MAIN( *@values where { @values.grep: * ~~ Int } ) { + my @N = @values[0 .. *-2 ]; + my $N = @values[ *-1 ]; +# say "Array { @N } searching for $N"; # get the index if the element is there - given @N.grep: $N, :k { .say && exit if $_ } - - # if here the element is not there - # for @N.List.kv -> $k, $v { - # if ( $v > $N ) { - # @N = |@N[ 0 .. $k - 1 ], $N, |@N[ $k .. * ]; - # "Insert value $N at index $k".say; - # } - # } - + given @N.grep( $N, :k ).first { .say && exit if $_ } - given @N.grep: { $_ > $N }, :k { + # if here the key is not there, let's see where to insert + given @N.grep( { $_ >= $N }, :k ).first { @N = |@N[ 0 .. $_ - 1 ], $N, |@N[ $_ .. * ]; - "Insert value $N at index $_".say; - exit; + .say && exit; } } -- cgit From 8ec4565fe8979eebefd0e3932d4b2e2c73b87dda Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 1 Feb 2021 09:33:29 +0000 Subject: remove unneeded parantheses --- challenge-098/james-smith/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-098/james-smith/perl/ch-2.pl b/challenge-098/james-smith/perl/ch-2.pl index 072cc04d3c..7e2867ade6 100644 --- a/challenge-098/james-smith/perl/ch-2.pl +++ b/challenge-098/james-smith/perl/ch-2.pl @@ -17,7 +17,7 @@ sub insert_pos { my( $t, $l, $val ) = (0,@_); ## Repeat unless we have got to end of list or the new entry is greater than val - $t++ while ($t < @{$l}) && ($l->[$t] < $val); + $t++ while $t < @{$l} && $l->[$t] < $val; ## If we are after the end of the list (to avoid warning) OR ## If we haven't found the entry then we use splice to insert it -- cgit From 16b6fa1d65e6e81d54dcbbd56e0fbbd1baffab38 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Feb 2021 10:33:48 +0100 Subject: Blog references --- challenge-098/luca-ferrari/blog-1.txt | 1 + challenge-098/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-098/luca-ferrari/blog-1.txt create mode 100644 challenge-098/luca-ferrari/blog-2.txt diff --git a/challenge-098/luca-ferrari/blog-1.txt b/challenge-098/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..ea83d9e18e --- /dev/null +++ b/challenge-098/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/02/01/PerlWeeklyChallenge98.html#task1 diff --git a/challenge-098/luca-ferrari/blog-2.txt b/challenge-098/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..2510cdb1cc --- /dev/null +++ b/challenge-098/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/02/01/PerlWeeklyChallenge98.html#task2 -- cgit From 903d7f9729a3debf23370afa22edcbfd92c25789 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 1 Feb 2021 02:54:55 -0700 Subject: Challenge 98 Solutions (Raku) --- challenge-098/mark-anderson/raku/ch-1.raku | 17 +++++++++++++++++ challenge-098/mark-anderson/raku/ch-2.raku | 13 +++++++++++++ challenge-098/mark-anderson/raku/input.txt | 1 + 3 files changed, 31 insertions(+) create mode 100644 challenge-098/mark-anderson/raku/ch-1.raku create mode 100644 challenge-098/mark-anderson/raku/ch-2.raku create mode 100644 challenge-098/mark-anderson/raku/input.txt diff --git a/challenge-098/mark-anderson/raku/ch-1.raku b/challenge-098/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..91c9b769b5 --- /dev/null +++ b/challenge-098/mark-anderson/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use Test; +plan 3; + +is readN("input.txt", 4), "1234"; +is readN("input.txt", 4), "5678"; +is readN("input.txt", 4), "90"; + +sub readN($FILE, $number) +{ + my $fh = open $FILE, :r; + my $head = $fh.readchars: $number; + my $tail = $fh.slurp; + $fh.close; + $FILE.IO.spurt: $tail; + return $head; +} diff --git a/challenge-098/mark-anderson/raku/ch-2.raku b/challenge-098/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..12bf7ec466 --- /dev/null +++ b/challenge-098/mark-anderson/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; +plan 4; + +is position(3, (1, 2, 3, 4)), 2; +is position(6, (1, 3, 5, 7)), 3; +is position(10, (12, 14, 16, 18)), 0; +is position(19, (11, 13, 15, 17)), 4; + +multi position($N, @N) +{ + (first * >= $N, :k, @N) // +@N; +} diff --git a/challenge-098/mark-anderson/raku/input.txt b/challenge-098/mark-anderson/raku/input.txt new file mode 100644 index 0000000000..6a537b5b36 --- /dev/null +++ b/challenge-098/mark-anderson/raku/input.txt @@ -0,0 +1 @@ +1234567890 \ No newline at end of file -- cgit From 1e3200d445f9dfed501541e9b52f6d8985af06f1 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 1 Feb 2021 03:05:39 -0700 Subject: Challenge 98 Solutions (Raku) --- challenge-098/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-098/mark-anderson/raku/ch-2.raku b/challenge-098/mark-anderson/raku/ch-2.raku index 12bf7ec466..0840d339bd 100644 --- a/challenge-098/mark-anderson/raku/ch-2.raku +++ b/challenge-098/mark-anderson/raku/ch-2.raku @@ -7,7 +7,7 @@ is position(6, (1, 3, 5, 7)), 3; is position(10, (12, 14, 16, 18)), 0; is position(19, (11, 13, 15, 17)), 4; -multi position($N, @N) +sub position($N, @N) { (first * >= $N, :k, @N) // +@N; } -- cgit From 87f34df3cbddc3c530c7984f31b2d442ffe2f184 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 1 Feb 2021 12:01:57 +0000 Subject: Doing part 2 first this week. Will look at the seek stuff in a bit. --- challenge-098/simon-proctor/raku/ch-2.raku | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenge-098/simon-proctor/raku/ch-2.raku diff --git a/challenge-098/simon-proctor/raku/ch-2.raku b/challenge-098/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..092b3fe86f --- /dev/null +++ b/challenge-098/simon-proctor/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku + +use v6; + +#| Given an int and a sorted list of ints output the index the first valus has (or should have) in the list +multi sub MAIN ( Int $N, #= Integer to check the location of + *@N where @N.sort ~~ @N #= Sorted list of integers + ) { + say find-in-list($N,@N); +} + +multi sub MAIN( "test" ) is hidden-from-USAGE { + use Test; + is( find-in-list( 3, [1,2,3,4] ), 2 ); + is( find-in-list( 6, [1,3,5,7] ), 3 ); + is( find-in-list( 10, [12,14,16,18] ), 0 ); + is( find-in-list( 19, [11,13,15,17] ), 4 ); + done-testing; +} + +sub find-in-list( Int $n, @n ) { + for @n.kv -> $i, $v { + return $i if $v ~~ $n; + return $i if $v > $n; + } + return @n.elems; +} -- cgit From 086638477ac9b5d3847507e6275d8e5342767893 Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Mon, 1 Feb 2021 07:04:20 -0500 Subject: 1st commit on 098_raku --- challenge-098/stuart-little/raku/ch-1.p6 | 6 ++++++ challenge-098/stuart-little/raku/ch-2.p6 | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100755 challenge-098/stuart-little/raku/ch-1.p6 create mode 100755 challenge-098/stuart-little/raku/ch-2.p6 diff --git a/challenge-098/stuart-little/raku/ch-1.p6 b/challenge-098/stuart-little/raku/ch-1.p6 new file mode 100755 index 0000000000..0add37ddb7 --- /dev/null +++ b/challenge-098/stuart-little/raku/ch-1.p6 @@ -0,0 +1,6 @@ +#!/usr/bin/env perl6 +use v6; + +# run