From 064b59f192b3dabead4a0f3d67eb3a060981e585 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Thu, 20 Jul 2023 13:01:30 -0400 Subject: Somehow forgot 211 --- challenge-211/dave-jacoby/perl/ch-1.pl | 44 ++++++++++++++++++++++++++++++++ challenge-211/dave-jacoby/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 challenge-211/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-211/dave-jacoby/perl/ch-2.pl diff --git a/challenge-211/dave-jacoby/perl/ch-1.pl b/challenge-211/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..7d11487056 --- /dev/null +++ b/challenge-211/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max sum uniq }; + +my @examples = ( + + [ [ 4, 3, 2, 1 ], [ 5, 4, 3, 2 ], [ 6, 5, 4, 3 ], ], + [ [ 1, 2, 3 ], [ 3, 2, 1 ], ] +); + +for my $e (@examples) { + my $pad = ' ' x 26; + my $matrix = join "\n$pad", map { qq{[$_],} } map { join ', ', $_->@* } + grep { scalar $_->@* } $e->@*; + my $o = toeplitz($e); + + say <<"END"; + Input: \@matrix = [ $matrix + ] + Output: $o +END +} + +sub toeplitz ($array) { + for my $i ( 0 .. -1 + scalar $array->@* ) { + my $t = _toeplitz( $array, $i, 0, $array->[$i][0] ); + return 'false' unless $t; + } + for my $i ( 1 .. -1 + scalar $array->[0]->@* ) { + my $t = _toeplitz( $array, 0, $i, $array->[0][$i] ); + return 'false' unless $t; + } + return 'true'; +} + +sub _toeplitz ( $array, $x = 0, $y = 0, $v = 0 ) { + if ( !defined $array->[$x][$y] ) { return 1 } + if ( $array->[$x][$y] ne $v ) { return 0 } + return _toeplitz( $array, $x + 1, $y + 1, $v ); +} diff --git a/challenge-211/dave-jacoby/perl/ch-2.pl b/challenge-211/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..1ba9d7ce26 --- /dev/null +++ b/challenge-211/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Algorithm::Permute; +use Getopt::Long; +use List::Util qw{ sum }; + +my @examples = ( + + [ 1, 2, 3, 4, 5, 6, 7, 8 ], + [ 1, 3 ], + +); + +my $v = 0; +GetOptions( 'verbose' => \$v, ); + +for my $e (@examples) { + my $o = sse( $e->@* ); + my $i = join ', ', $e->@*; + + say <<"END"; + Input: \@list = ($i) + Output: $o +END +} + +sub sse (@array) { + my $permute = Algorithm::Permute->new( \@array ); + while ( my @result = $permute->next ) { + for my $i ( 0 .. -2 + scalar @result ) { + my @a1 = @result[ 0 .. $i ]; + my @a2 = @result[ $i + 1 .. -1 + scalar @result ]; + my $av1 = ( sum @a1 ) / ( scalar @a1 ); + my $av2 = ( sum @a2 ) / ( scalar @a2 ); + say join " ", $i, ( join ',', @a1 ), ( join ',', @a2 ), $av1, + $av2 + if $v; + return 'true' if $av1 == $av2; + } + } + return 'false'; +} -- cgit From 9a38bd2ff080991f73d7f49cebf2fbab06c69d94 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 24 Jul 2023 16:22:14 -0400 Subject: DAJ ccxxvii --- challenge-227/dave-jacoby/blog.txt | 1 + challenge-227/dave-jacoby/perl/ch-1.pl | 44 ++++++++++++++++++++++++++++++++++ challenge-227/dave-jacoby/perl/ch-2.pl | 38 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 challenge-227/dave-jacoby/blog.txt create mode 100644 challenge-227/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-227/dave-jacoby/perl/ch-2.pl diff --git a/challenge-227/dave-jacoby/blog.txt b/challenge-227/dave-jacoby/blog.txt new file mode 100644 index 0000000000..0e391a293c --- /dev/null +++ b/challenge-227/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/07/24/if-sept-seven-why-is-it-the-ninth-month-weekly-challenge-ccxxvii.html diff --git a/challenge-227/dave-jacoby/perl/ch-1.pl b/challenge-227/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..bf3f29e656 --- /dev/null +++ b/challenge-227/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use DateTime; + +my @examples = ( + 1753, + 1800, + 1900, + 2000, + 2009, + 2023, + 2100, + 3000, + 4000, + 9000, + 9999 +); + +for my $year (@examples) { + my $count = find_friday13s($year); + say <<~"END"; + Input: \$year = $year + Output: $count + END +} + +sub find_friday13s ($year) { + my $count = 0; + my @months; + for my $month ( 1 .. 12 ) { + my $dt = DateTime->now(); + $dt->set_year($year); + $dt->set_day(13); + $dt->set_month($month); + $count++ if $dt->day_of_week == 5; + push @months, $month if $dt->day_of_week == 5; + + } + return $count; +} diff --git a/challenge-227/dave-jacoby/perl/ch-2.pl b/challenge-227/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..5a1c3617f7 --- /dev/null +++ b/challenge-227/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Roman; + +my @examples = ( + "IV + V", + "M - I", + "X / II", + "XI * VI", + "VII ** III", + "V - V", + "V / II", + "MMM + M", + "V - X", +); + +for my $e (@examples) { + my $pad = ' ' x (10 - length $e); + my $output = roman_maths($e); + print <<~"END"; + $e $pad => $output + END +} + +sub roman_maths ($equation) { + my ( $first, $expression, $second ) = split /\s+/mx, $equation; + my ( $f, $s ) = map { arabic($_) } $first, $second; + my $arabic = eval( join ' ', $f, $expression, $s ); + my $roman = Roman($arabic); + $roman = undef if $arabic =~ /\./mx; + return $roman if defined $roman && $arabic > 0; + return 'nulla' if $arabic == 0; + return 'non potest' ; +} -- cgit From 20a23e69f0fc6a4edbc309955f3160921aa36e1f Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 31 Jul 2023 08:06:31 +0000 Subject: w228 - Task 1 & 2 --- challenge-228/perlboy1967/perl/ch1.pl | 37 ++++++++++++++++++++++++++++++ challenge-228/perlboy1967/perl/ch2.pl | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100755 challenge-228/perlboy1967/perl/ch1.pl create mode 100755 challenge-228/perlboy1967/perl/ch2.pl diff --git a/challenge-228/perlboy1967/perl/ch1.pl b/challenge-228/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..371fc09cc2 --- /dev/null +++ b/challenge-228/perlboy1967/perl/ch1.pl @@ -0,0 +1,37 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 228 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-228 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Unique Sum +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to find out the sum of unique elements in the given array. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +use List::Util qw(sum0); +use List::MoreUtils qw(frequency); + +sub uniqueSum (@) { + my %f = frequency @_; + return sum0 grep { $f{$_} == 1 } keys %f; +} + +is(uniqueSum(2,1,3,2),4); +is(uniqueSum(1,1,1,1),0); +is(uniqueSum(2,1,3,4),10); + +done_testing; diff --git a/challenge-228/perlboy1967/perl/ch2.pl b/challenge-228/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..0dd250f875 --- /dev/null +++ b/challenge-228/perlboy1967/perl/ch2.pl @@ -0,0 +1,42 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 228 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-228 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Empty Array +Submitted by: Mohammad S Anwar + +You are given an array of integers in which all elements are unique. + +Write a script to perform the following operations until the array is +empty and return the total count of operations. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +use List::Util qw(min); + +sub emptyArray (@) { + my $n = 0; + + while (@_) { + my $v = shift @_; push(@_,$v) if (min(@_,$v) != $v); + $n++; + } + + return $n; +} + +is(emptyArray(3,4,2),5); +is(emptyArray(1,2,3),3); + +done_testing; -- cgit From 95ca8aa310f519e5a9298d37988ed625dd39f05f Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 31 Jul 2023 10:27:59 +0200 Subject: challange-228 --- challenge-228/peter-meszaros/perl/ch-1.pl | 57 ++++++++++++++++++++++++++++ challenge-228/peter-meszaros/perl/ch-2.pl | 63 +++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100755 challenge-228/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-228/peter-meszaros/perl/ch-2.pl diff --git a/challenge-228/peter-meszaros/perl/ch-1.pl b/challenge-228/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..a7fecbf5db --- /dev/null +++ b/challenge-228/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +# You are given an array of integers. +# +# Write a script to find out the sum of unique elements in the given array. +# Example 1 +# +# Input: @int = (2, 1, 3, 2) +# Output: 4 +# +# In the given array we have 2 unique elements (1, 3). +# +# Example 2 +# +# Input: @int = (1, 1, 1, 1) +# Output: 0 +# +# In the given array no unique element found. +# +# Example 3 +# +# Input: @int = (2, 1, 3, 4) +# Output: 10 +# +# In the given array every element is unique. + +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [2, 1, 3, 2], + [1, 1, 1, 1], + [2, 1, 3, 4], +]; + +sub unique_sum +{ + my $aref = shift; + + my %h; + $h{$_}++ for @$aref; + + my $sum = 0; + for my $k (keys %h) { + $sum += $k if $h{$k} == 1; + } + return $sum; +} + +is(unique_sum($cases->[0]), 4, '[2, 1, 3, 2]'); +is(unique_sum($cases->[1]), 0, '[1, 1, 1, 1]'); +is(unique_sum($cases->[2]), 10, '[2, 1, 3, 4]'); +done_testing(); + +exit 0; diff --git a/challenge-228/peter-meszaros/perl/ch-2.pl b/challenge-228/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..8ecda4691b --- /dev/null +++ b/challenge-228/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +# You are given an array of integers in which all elements are unique. +# +# Write a script to perform the following operations until the array is empty and +# return the total count of operations. +# +# If the first element is the smallest then remove it otherwise move it to the +# end. +# +# +# Example 1 +# +# Input: @int = (3, 4, 2) +# Ouput: 5 +# +# Operation 1: move 3 to the end: (4, 2, 3) +# Operation 2: move 4 to the end: (2, 3, 4) +# Operation 3: remove element 2: (3, 4) +# Operation 4: remove element 3: (4) +# Operation 5: remove element 4: () +# +# Example 2 +# +# Input: @int = (1, 2, 3) +# Ouput: 3 +# +# Operation 1: remove element 1: (2, 3) +# Operation 2: remove element 2: (3) +# Operation 3: remove element 3: () + +use strict; +use warnings; +use List::Util qw/min/; +use Test::More; +use Data::Dumper; + +my $cases = [ + [3, 4, 2], + [1, 2, 3], +]; + +sub empty_array +{ + my @a = $_[0]->@*; + + my $numop = 0; + while (@a) { + if (min(@a) < $a[0]) { + push @a, $a[0]; + } + shift @a; + ++$numop; + + } + return $numop; +} + +is(empty_array($cases->[0]), 5, '[3, 4, 2]'); +is(empty_array($cases->[1]), 3, '[1, 2, 3]'); +done_testing(); + +exit 0; -- cgit From fc5cb8f9944974380ec4b67f29c2c717c5e93187 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 31 Jul 2023 08:57:22 +0000 Subject: Challenge 228 Solutions (Raku) --- challenge-228/mark-anderson/raku/ch-1.raku | 12 ++++++++++++ challenge-228/mark-anderson/raku/ch-2.raku | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-228/mark-anderson/raku/ch-1.raku create mode 100644 challenge-228/mark-anderson/raku/ch-2.raku diff --git a/challenge-228/mark-anderson/raku/ch-1.raku b/challenge-228/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..4d961b6d77 --- /dev/null +++ b/challenge-228/mark-anderson/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use Test; + +is unique-sum(<2 1 3 2>), 4; +is unique-sum(<1 1 1 1>), 0; +is unique-sum(<2 1 3 4>), 10; +say unique-sum((^1000).roll(1000)); + +sub unique-sum(*@a) +{ + sum do map {.key}, @a.Bag.grep(*.value == 1) +} diff --git a/challenge-228/mark-anderson/raku/ch-2.raku b/challenge-228/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..1c0abef944 --- /dev/null +++ b/challenge-228/mark-anderson/raku/ch-2.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku +use Test; + +is empty-array(<3 4 2>), 5; +is empty-array(<1 2 3>), 3; +is empty-array(<16 17 8 15 13 11 19 5 12 6 20 2 4 10 3 14 1 7 9 18>), 127; +say empty-array((^1000).pick(1000)); + +sub empty-array(*@a) +{ + sum do while @a + { + my $v = @a.antipairs.min.value; + @a .= rotate($v); + @a.shift; + $v.succ + } +} -- cgit From 41dbb452693f46c89af624291d7bdb4171f09801 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 31 Jul 2023 12:21:07 +0200 Subject: feat(challenge-228/lubos-kolouch/perl,python/): Challenge 228 LK Perl Python --- challenge-228/lubos-kolouch/perl/ch-1.pl | 17 +++++++++++++++++ challenge-228/lubos-kolouch/perl/ch-2.pl | 22 ++++++++++++++++++++++ challenge-228/lubos-kolouch/python/ch-1.py | 17 +++++++++++++++++ challenge-228/lubos-kolouch/python/ch-2.py | 16 ++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 challenge-228/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-228/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-228/lubos-kolouch/python/ch-1.py create mode 100644 challenge-228/lubos-kolouch/python/ch-2.py diff --git a/challenge-228/lubos-kolouch/perl/ch-1.pl b/challenge-228/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..a7a8e679cb --- /dev/null +++ b/challenge-228/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; +use List::Util qw(sum); + +sub unique_sum { + my @array = @_; + my %counts; + + $counts{$_}++ for @array; + my @unique = grep { $counts{$_} == 1 } keys %counts; + + return sum(@unique) // 0; +} + +print unique_sum(2, 1, 3, 2), "\n"; # Output: 4 +print unique_sum(1, 1, 1, 1), "\n"; # Output: 0 +print unique_sum(2, 1, 3, 4), "\n"; # Output: 10 diff --git a/challenge-228/lubos-kolouch/perl/ch-2.pl b/challenge-228/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..6849d970e5 --- /dev/null +++ b/challenge-228/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use List::Util 'min'; + +sub count_operations { + my @array = @_; + my $count = 0; + while (@array) { + if ($array[0] == min @array) { + shift @array; + } else { + push @array, shift @array; + } + $count++; + } + return $count; +} + +print count_operations(3, 4, 2); # Output: 5 +print "\n"; +print count_operations(1, 2, 3); # Output: 3 +print "\n"; diff --git a/challenge-228/lubos-kolouch/python/ch-1.py b/challenge-228/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..4470977f61 --- /dev/null +++ b/challenge-228/lubos-kolouch/python/ch-1.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def unique_sum(lst): + counts = {} + for num in lst: + if num in counts: + counts[num] += 1 + else: + counts[num] = 1 + + return sum(num for num, count in counts.items() if count == 1) + + +print(unique_sum([2, 1, 3, 2])) # Output: 4 +print(unique_sum([1, 1, 1, 1])) # Output: 0 +print(unique_sum([2, 1, 3, 4])) # Output: 10 diff --git a/challenge-228/lubos-kolouch/python/ch-2.py b/challenge-228/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..aa8178b649 --- /dev/null +++ b/challenge-228/lubos-kolouch/python/ch-2.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def count_operations(lst): + count = 0 + while lst: + if lst[0] == min(lst): + lst.pop(0) + else: + lst.append(lst.pop(0)) + count += 1 + return count + + +print(count_operations([3, 4, 2])) # Output: 5 +print(count_operations([1, 2, 3])) # Output: 3 -- cgit From 0defc5eb9c087a7f8362b9d67f0020235734d2a9 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Mon, 31 Jul 2023 15:38:59 +0330 Subject: TWC228 --- challenge-228/deadmarshal/blog.txt | 1 + challenge-228/deadmarshal/cl/ch1.lisp | 15 +++++++++++++++ challenge-228/deadmarshal/cl/ch2.lisp | 12 ++++++++++++ challenge-228/deadmarshal/lua/ch-1.lua | 18 ++++++++++++++++++ challenge-228/deadmarshal/lua/ch-2.lua | 15 +++++++++++++++ challenge-228/deadmarshal/perl/ch-1.pl | 15 +++++++++++++++ challenge-228/deadmarshal/perl/ch-2.pl | 17 +++++++++++++++++ challenge-228/deadmarshal/raku/ch-1.raku | 13 +++++++++++++ challenge-228/deadmarshal/raku/ch-2.raku | 16 ++++++++++++++++ challenge-228/deadmarshal/ruby/ch1.rb | 12 ++++++++++++ challenge-228/deadmarshal/ruby/ch2.rb | 14 ++++++++++++++ 11 files changed, 148 insertions(+) create mode 100644 challenge-228/deadmarshal/blog.txt create mode 100644 challenge-228/deadmarshal/cl/ch1.lisp create mode 100644 challenge-228/deadmarshal/cl/ch2.lisp create mode 100644 challenge-228/deadmarshal/lua/ch-1.lua create mode 100644 challenge-228/deadmarshal/lua/ch-2.lua create mode 100644 challenge-228/deadmarshal/perl/ch-1.pl create mode 100644 challenge-228/deadmarshal/perl/ch-2.pl create mode 100644 challenge-228/deadmarshal/raku/ch-1.raku create mode 100644 challenge-228/deadmarshal/raku/ch-2.raku create mode 100644 challenge-228/deadmarshal/ruby/ch1.rb create mode 100644 challenge-228/deadmarshal/ruby/ch2.rb diff --git a/challenge-228/deadmarshal/blog.txt b/challenge-228/deadmarshal/blog.txt new file mode 100644 index 0000000000..1aa770ca37 --- /dev/null +++ b/challenge-228/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2023/07/twc228.html diff --git a/challenge-228/deadmarshal/cl/ch1.lisp b/challenge-228/deadmarshal/cl/ch1.lisp new file mode 100644 index 0000000000..d3ab63c2f0 --- /dev/null +++ b/challenge-228/deadmarshal/cl/ch1.lisp @@ -0,0 +1,15 @@ +(defun unique-sum (list) + (flet ((get-keys (ht num) + (loop :for k :being :the :hash-keys :of ht + :when (= (gethash k ht) num) + :collect k))) + (let ((ht (make-hash-table :size (list-length list)))) + (loop :for i :in list + :do (incf (gethash i ht 0))) + (apply #'+ (get-keys ht 1))))) + +(progn + (print (unique-sum '(2 1 3 2))) + (print (unique-sum '(1 1 1 1))) + (print (unique-sum '(2 1 3 4)))) + diff --git a/challenge-228/deadmarshal/cl/ch2.lisp b/challenge-228/deadmarshal/cl/ch2.lisp new file mode 100644 index 0000000000..7c75af8ca3 --- /dev/null +++ b/challenge-228/deadmarshal/cl/ch2.lisp @@ -0,0 +1,12 @@ +(defun empty-array (list count) + (if (null list) + count + (if (= (car list) (apply #'min list)) + (empty-array (cdr list) (1+ count)) + (empty-array (append (cdr list) (list (car list))) + (1+ count))))) + +(progn + (print (empty-array '(3 4 2) 0)) + (print (empty-array '(1 2 3) 0))) + diff --git a/challenge-228/deadmarshal/lua/ch-1.lua b/challenge-228/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..0de103fe4e --- /dev/null +++ b/challenge-228/deadmarshal/lua/ch-1.lua @@ -0,0 +1,18 @@ +#!/usr/bin/env lua + +local function unique_sum(t) + local hash,sum = {},0 + setmetatable(hash,{__index = function(t,k) return 0 end}) + for i=1,#t do + hash[t[i]] = hash[t[i]] + 1 + end + for k,v in pairs(hash) do + if v == 1 then sum = sum + k end + end + return sum +end + +print(unique_sum({2,1,3,2})) +print(unique_sum({1,1,1,1})) +print(unique_sum({2,1,3,4})) + diff --git a/challenge-228/deadmarshal/lua/ch-2.lua b/challenge-228/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..8f3245f208 --- /dev/null +++ b/challenge-228/deadmarshal/lua/ch-2.lua @@ -0,0 +1,15 @@ +#!/usr/bin/env lua + +local function empty_array(t) + local count = 0 + while #t ~= 0 do + if t[1] == math.min(table.unpack(t)) then table.remove(t,1) + else table.insert(t,table.remove(t,1)) end + count = count + 1 + end + return count +end + +print(empty_array({3,4,2})) +print(empty_array({1,2,3})) + diff --git a/challenge-228/deadmarshal/perl/ch-1.pl b/challenge-228/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..fe599e35a9 --- /dev/null +++ b/challenge-228/deadmarshal/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(sum0); + +sub unique_sum{ + my %hash; + $hash{$_}++ foreach(@{$_[0]}); + sum0 grep{$hash{$_} == 1} keys %hash; +} + +printf "%d\n", unique_sum([2,1,3,2]); +printf "%d\n", unique_sum([1,1,1,1]); +printf "%d\n", unique_sum([2,1,3,4]); + diff --git a/challenge-228/deadmarshal/perl/ch-2.pl b/challenge-228/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..1098233ec7 --- /dev/null +++ b/challenge-228/deadmarshal/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(min); + +sub empty_array{ + my ($count,$arr) = (0,@_); + while(@$arr){ + $arr->[0] == min(@$arr) ? shift @$arr : push @$arr,shift @$arr; + $count++ + } + $count +} + +printf "%d\n", empty_array([3,4,2]); +printf "%d\n", empty_array([1,2,3]); + diff --git a/challenge-228/deadmarshal/raku/ch-1.raku b/challenge-228/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..b9dac8d01b --- /dev/null +++ b/challenge-228/deadmarshal/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + +sub unique-sum(@arr) +{ + my %hash; + %hash{$_}++ for @arr; + %hash.keys.grep({%hash{$_} == 1}).sum +} + +say unique-sum([2,1,3,2]); +say unique-sum([1,1,1,1]); +say unique-sum([2,1,3,4]); + diff --git a/challenge-228/deadmarshal/raku/ch-2.raku b/challenge-228/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..62d71595e0 --- /dev/null +++ b/challenge-228/deadmarshal/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +sub empty-array(@arr) +{ + my $count = 0; + while @arr + { + @arr[0] == @arr.min ?? @arr.shift !! @arr.push(@arr.shift); + $count++; + } + $count +} + +say empty-array([3,4,2]); +say empty-array([1,2,3]); + diff --git a/challenge-228/deadmarshal/ruby/ch1.rb b/challenge-228/deadmarshal/ruby/ch1.rb new file mode 100644 index 0000000000..e16a4f31c3 --- /dev/null +++ b/challenge-228/deadmarshal/ruby/ch1.rb @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby + +def unique_sum(arr) + hash = Hash.new(0) + arr.each {|n| hash[n] += 1} + hash.keys.select {|k| hash[k] == 1}.sum +end + +puts unique_sum([2,1,3,2]) +puts unique_sum([1,1,1,1]) +puts unique_sum([2,1,3,4]) + diff --git a/challenge-228/deadmarshal/ruby/ch2.rb b/challenge-228/deadmarshal/ruby/ch2.rb new file mode 100644 index 0000000000..1c906e629d --- /dev/null +++ b/challenge-228/deadmarshal/ruby/ch2.rb @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +def empty_array(arr) + count = 0 + while arr.length != 0 + arr[0] == arr.min ? arr.shift : arr.push(arr.shift) + count += 1 + end + count +end + +puts empty_array([3,4,2]) +puts empty_array([1,2,3]) + -- cgit From 95b752b0db17acabcd8c3e8ebb1d828795fb51df Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 31 Jul 2023 10:36:20 -0400 Subject: DAJ 228 --- challenge-228/dave-jacoby/blog.txt | 1 + challenge-228/dave-jacoby/perl/ch-1.pl | 32 ++++++++++++++++++++++++++++ challenge-228/dave-jacoby/perl/ch-2.pl | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-228/dave-jacoby/blog.txt create mode 100644 challenge-228/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-228/dave-jacoby/perl/ch-2.pl diff --git a/challenge-228/dave-jacoby/blog.txt b/challenge-228/dave-jacoby/blog.txt new file mode 100644 index 0000000000..2b1a2630d8 --- /dev/null +++ b/challenge-228/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/07/31/get-sum-weekly-challenge-228.html diff --git a/challenge-228/dave-jacoby/perl/ch-1.pl b/challenge-228/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..4996d31312 --- /dev/null +++ b/challenge-228/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw( sum0 ); + +my @examples = ( + + [ 2, 1, 3, 2 ], + [ 1, 1, 1, 1 ], + [ 2, 1, 3, 4 ], +); + +for my $e (@examples) { + my @array = $e->@*; + my $array = join ', ', @array; + my $sum = uniq_sum(@array); + say <<~"END"; + Input: \@int = ($array) + Output: $sum + END +} + +sub uniq_sum (@array) { + my %hash; + for my $int (@array) { + $hash{$int}++; + } + return sum0 grep { $hash{$_} == 1 } keys %hash; +} diff --git a/challenge-228/dave-jacoby/perl/ch-2.pl b/challenge-228/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..512108a149 --- /dev/null +++ b/challenge-228/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw( min ); + +my @examples = ( + + [ 3, 4, 2 ], + [ 1, 2, 3 ], + +); + +for my $e (@examples) { + my @array = $e->@*; + my $array = join ', ', @array; + my $output = empty_array(@array); + say <<~"END"; + Input: \@int = ($array) + Output: $output + END +} + +# if the first element is the smallest +# then remove it +# else +# move it to the end +sub empty_array (@array) { + my $c = 0; + while ( scalar @array ) { + my $min = min @array; + my $next = shift @array; + if ( $min != $next ) { push @array, $next; } + $c++; + } + return $c; +} -- cgit From f99ae8edc964cc79f9462b53d2b614f14b0f5829 Mon Sep 17 00:00:00 2001 From: Conor Hoekstra Date: Mon, 31 Jul 2023 11:18:59 -0400 Subject: Week 228P1 in Python, Rust, BQN & APL --- challenge-228/conor-hoekstra/ch-01.apl | 7 +++++++ challenge-228/conor-hoekstra/ch-01.bqn | 6 ++++++ challenge-228/conor-hoekstra/ch-01.py | 7 +++++++ challenge-228/conor-hoekstra/ch-01.rs | 14 ++++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 challenge-228/conor-hoekstra/ch-01.apl create mode 100644 challenge-228/conor-hoekstra/ch-01.bqn create mode 100644 challenge-228/conor-hoekstra/ch-01.py create mode 100644 challenge-228/conor-hoekstra/ch-01.rs diff --git a/challenge-228/conor-hoekstra/ch-01.apl b/challenge-228/conor-hoekstra/ch-01.apl new file mode 100644 index 0000000000..a7bc25997d --- /dev/null +++ b/challenge-228/conor-hoekstra/ch-01.apl @@ -0,0 +1,7 @@ +UniqueSum ← +/(∪×1=(+⌿⊢∘.=∪)) + +⍝ Tests + +UniqueSum 2 1 3 2 ⍝ 4 +UniqueSum 1 1 1 1 ⍝ 0 +UniqueSum 1 2 3 4 ⍝ 10 diff --git a/challenge-228/conor-hoekstra/ch-01.bqn b/challenge-228/conor-hoekstra/ch-01.bqn new file mode 100644 index 0000000000..060ee089ec --- /dev/null +++ b/challenge-228/conor-hoekstra/ch-01.bqn @@ -0,0 +1,6 @@ +UniqueSum ← +´⍷×1=·+˝=⌜⟜⍷ + +# Tests +UniqueSum 2‿1‿3‿2 # 4 +UniqueSum 1‿1‿1‿1 # 0 +UniqueSum 1‿2‿3‿4 # 10 diff --git a/challenge-228/conor-hoekstra/ch-01.py b/challenge-228/conor-hoekstra/ch-01.py new file mode 100644 index 0000000000..28b58a09e6 --- /dev/null +++ b/challenge-228/conor-hoekstra/ch-01.py @@ -0,0 +1,7 @@ +def unique_sum(nums): + return sum(k for k,v in Counter(nums).items() if v == 1) + +# Tests +unique_sum([2,1,3,2]) # 4 +unique_sum([1,1,1,1]) # 0 +unique_sum([2,1,3,4]) # 10 diff --git a/challenge-228/conor-hoekstra/ch-01.rs b/challenge-228/conor-hoekstra/ch-01.rs new file mode 100644 index 0000000000..be2eb5b2c2 --- /dev/null +++ b/challenge-228/conor-hoekstra/ch-01.rs @@ -0,0 +1,14 @@ +fn unique_sum(nums: Vec) -> i32 { + nums.iter() + .counts() + .iter() + .map(|(k, v)| if v == &1 { k } else { &0 }) + .sum() +} + +// Tests +fn main() { + println!("{}", unique_sum(vec![2, 1, 3, 2])); // 4 + println!("{}", unique_sum(vec![1, 1, 1, 1])); // 0 + println!("{}", unique_sum(vec![2, 1, 3, 4])); // 10 +} -- cgit From f55055faeb76ab3ed8c29167e03e7f0ed29e1ce6 Mon Sep 17 00:00:00 2001 From: Conor Hoekstra Date: Mon, 31 Jul 2023 11:19:55 -0400 Subject: remove line --- challenge-228/conor-hoekstra/ch-01.apl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-228/conor-hoekstra/ch-01.apl b/challenge-228/conor-hoekstra/ch-01.apl index a7bc25997d..ad2c531256 100644 --- a/challenge-228/conor-hoekstra/ch-01.apl +++ b/challenge-228/conor-hoekstra/ch-01.apl @@ -1,7 +1,6 @@ UniqueSum ← +/(∪×1=(+⌿⊢∘.=∪)) -⍝ Tests - +⍝ Tests UniqueSum 2 1 3 2 ⍝ 4 UniqueSum 1 1 1 1 ⍝ 0 UniqueSum 1 2 3 4 ⍝ 10 -- cgit From 086b8f58c640824120c0f76b966db08a0a60f1e6 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 31 Jul 2023 19:22:24 +0200 Subject: Task 1 done Task 2 done Task 1 plperl done Task 2 plperl done Task 1 plpgsql done Task plpgsql done Blog references --- challenge-228/luca-ferrari/blog-1.txt | 1 + challenge-228/luca-ferrari/blog-2.txt | 1 + challenge-228/luca-ferrari/blog-3.txt | 1 + challenge-228/luca-ferrari/blog-4.txt | 1 + challenge-228/luca-ferrari/blog-5.txt | 1 + challenge-228/luca-ferrari/blog-6.txt | 1 + challenge-228/luca-ferrari/postgresql/ch-1.plperl | 24 +++++++++++++ challenge-228/luca-ferrari/postgresql/ch-1.sql | 23 ++++++++++++ challenge-228/luca-ferrari/postgresql/ch-2.plperl | 37 +++++++++++++++++++ challenge-228/luca-ferrari/postgresql/ch-2.sql | 43 +++++++++++++++++++++++ challenge-228/luca-ferrari/raku/ch-1.p6 | 13 +++++++ challenge-228/luca-ferrari/raku/ch-2.p6 | 21 +++++++++++ 12 files changed, 167 insertions(+) create mode 100644 challenge-228/luca-ferrari/blog-1.txt create mode 100644 challenge-228/luca-ferrari/blog-2.txt create mode 100644 challenge-228/luca-ferrari/blog-3.txt create mode 100644 challenge-228/luca-ferrari/blog-4.txt create mode 100644 challenge-228/luca-ferrari/blog-5.txt create mode 100644 challenge-228/luca-ferrari/blog-6.txt create mode 100644 challenge-228/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-228/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-228/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-228/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-228/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-228/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-228/luca-ferrari/blog-1.txt b/challenge-228/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..1640e63f24 --- /dev/null +++ b/challenge-228/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/31/PerlWeeklyChallenge228.html#task1 diff --git a/challenge-228/luca-ferrari/blog-2.txt b/challenge-228/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..cf52c38f6e --- /dev/null +++ b/challenge-228/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/31/PerlWeeklyChallenge228.html#task2 diff --git a/challenge-228/luca-ferrari/blog-3.txt b/challenge-228/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..9a38aadf55 --- /dev/null +++ b/challenge-228/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/31/PerlWeeklyChallenge228.html#task1plperl diff --git a/challenge-228/luca-ferrari/blog-4.txt b/challenge-228/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..9ec986875c --- /dev/null +++ b/challenge-228/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/31/PerlWeeklyChallenge228.html#task2plperl diff --git a/challenge-228/luca-ferrari/blog-5.txt b/challenge-228/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..e785f9634e --- /dev/null +++ b/challenge-228/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/31/PerlWeeklyChallenge228.html#task1plpgsql diff --git a/challenge-228/luca-ferrari/blog-6.txt b/challenge-228/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..f9d2f884c2 --- /dev/null +++ b/challenge-228/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/31/PerlWeeklyChallenge228.html#task2plpgsql diff --git a/challenge-228/luca-ferrari/postgresql/ch-1.plperl b/challenge-228/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..8bc689b1ab --- /dev/null +++ b/challenge-228/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 228 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc228; + +CREATE OR REPLACE FUNCTION +pwc228.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $array ) = @_; + my $bag = {}; + + # classify the elements + $bag->{ $_ }++ for ( $array->@* ); + + my ( @uniques ) = grep( { $bag->{ $_ } == 1 } keys( $bag->%* ) ); + my $sum = 0; + $sum += $_ for ( @uniques ); + return $sum; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-228/luca-ferrari/postgresql/ch-1.sql b/challenge-228/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..2926c98fda --- /dev/null +++ b/challenge-228/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 228 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc228; + +CREATE OR REPLACE FUNCTION +pwc228.task1_plpgsql( a int[] ) +RETURNS int +AS $CODE$ + WITH BAG as ( + SELECT v + FROM unnest( a ) v + GROUP BY v + HAVING count(*) = 1 + ) + SELECT sum( v ) + FROM bag; +$CODE$ +LANGUAGE sql; diff --git a/challenge-228/luca-ferrari/postgresql/ch-2.plperl b/challenge-228/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..750cc836d9 --- /dev/null +++ b/challenge-228/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 228 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc228; + +CREATE OR REPLACE FUNCTION +pwc228.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $array ) = @_; + my $moves = 0; + + # a function to find out the min value + my $min = sub { + my $min = undef; + for ( $_[0]->@* ) { + $min = $_ if ( ! $min || $min > $_ ); + } + + return $min; + }; + + while ( scalar $array->@* ) { + my ( $swap, $min ) = ( shift( $array->@* ), $min->( $array ) ); + $moves++; + last if ! $swap; + last if ! $min; + push $array->@*, $swap if ( $swap > $min ); + + } + + return $moves; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-228/luca-ferrari/postgresql/ch-2.sql b/challenge-228/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..0cb1ea4788 --- /dev/null +++ b/challenge-228/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,43 @@ +-- +-- Perl Weekly Challenge 228 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc228; + +CREATE OR REPLACE FUNCTION +pwc228.task2_plpgsql( a int[] ) +RETURNS int +AS $CODE$ +DECLARE + current_min int; + current_swap int; + moves int := 0; +BEGIN + WHILE array_length( a, 1 ) > 1 LOOP + -- find the min value + SELECT min( v ) + INTO current_min + FROM unnest( a ) v; + + + + -- unshift the first element + current_swap := a[ 1 ]; + a := a[ 2 : array_length( a, 1 ) ]; + + IF current_swap > current_min THEN + a := array_append( a, current_swap ); + END IF; + + moves := moves + 1; + + END LOOP; + + RETURN moves; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-228/luca-ferrari/raku/ch-1.p6 b/challenge-228/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..c1385d616f --- /dev/null +++ b/challenge-228/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,13 @@ +#!raku + +# +# Perl Weekly Challenge 228 +# Task 1 +# +# See +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + my $bag = @nums.Bag; + $bag.keys.grep( { $bag{ $_ } == 1 } ).sum.say; +} diff --git a/challenge-228/luca-ferrari/raku/ch-2.p6 b/challenge-228/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..62ab83f368 --- /dev/null +++ b/challenge-228/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,21 @@ +#!raku + +# +# Perl Weekly Challenge 228 +# Task 2 +# +# See +# + +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + my @current = @nums; + my $moves = 0; + + while ( @current ) { + my $swap = @current.shift; + @current.push: $swap if $swap > @current.min; + $moves++; + } + + $moves.say; +} -- cgit From 040c25f0a6877449480160d2d0113a409b502b09 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 31 Jul 2023 11:37:55 -0400 Subject: Fix filenames. --- challenge-227/packy-anderson/perl/ch-1.pl | 36 --------- challenge-227/packy-anderson/perl/ch-2.pl | 79 ------------------- challenge-227/packy-anderson/perl/task-1.pl | 36 +++++++++ challenge-227/packy-anderson/perl/task-2.pl | 79 +++++++++++++++++++ challenge-227/packy-anderson/raku/ch-1.raku | 29 ------- challenge-227/packy-anderson/raku/ch-2.raku | 106 -------------------------- challenge-227/packy-anderson/raku/task-1.raku | 29 +++++++ challenge-227/packy-anderson/raku/task-2.raku | 106 ++++++++++++++++++++++++++ 8 files changed, 250 insertions(+), 250 deletions(-) delete mode 100755 challenge-227/packy-anderson/perl/ch-1.pl delete mode 100755 challenge-227/packy-anderson/perl/ch-2.pl create mode 100755 challenge-227/packy-anderson/perl/task-1.pl create mode 100755 challenge-227/packy-anderson/perl/task-2.pl delete mode 100755 challenge-227/packy-anderson/raku/ch-1.raku delete mode 100755 challenge-227/packy-anderson/raku/ch-2.raku create mode 100755 challenge-227/packy-anderson/raku/task-1.raku create mode 100755 challenge-227/packy-anderson/raku/task-2.raku diff --git a/challenge-227/packy-anderson/perl/ch-1.pl b/challenge-227/packy-anderson/perl/ch-1.pl deleted file mode 100755 index 1bfd2a1d13..0000000000 --- a/challenge-227/packy-anderson/perl/ch-1.pl +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env perl -use v5.38; - -# let's use the core modules for date manipulation -use Time::Piece; -use Time::Seconds qw( ONE_DAY ); - -# get the year from the command line -my $year = shift @ARGV - or die "usage: $0 year\n"; - -# do bounds checking as specified in the problem -if ($year < 1753 || $year > 9999) { - die "Only years between 1753 to 9999 are allowed ($year is out of range)\n"; -} - -# create an object for Jan 01 of the given year -my $t = Time::Piece->strptime("$year-01-01", "%Y-%m-%d") - ->truncate(to => 'day'); - -# find the first friday -# in Time::Piece->wday, 1 = Sunday, 6 = Friday -while ( $t->wday != 6) { - $t += ONE_DAY; # add 1 day -} - -# now keep adding 7 days to the date until the year changes, -# noting how many times the day of the month is 13 -my $thirteen_count = 0; -while ( $t->year == $year ) { - $thirteen_count++ if $t->mday == 13; - $t += ONE_DAY * 7; -} - -say "Input: \$year = $year"; -say "Output: $thirteen_count"; \ No newline at end of file diff --git a/challenge-227/packy-anderson/perl/ch-2.pl b/challenge-227/packy-anderson/perl/ch-2.pl deleted file mode 100755 index 425a4341ea..0000000000 --- a/challenge-227/packy-anderson/perl/ch-2.pl +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env perl -use v5.38; - -use Roman; # there's a module for handling Roman Numerals! - -sub do_arithmetic { - my $line = shift; - # split the inout line into the three parts: - # the two operands and the infix operator - my($operand1r, $operator, $operand2r) = split /\s+/, $line; - unless (defined $operand1r && - defined $operator && - defined $operand2r) { - say q{Lines must be of the form "operand1 operator operand2"}; - say q{where both operands are valid roman numerals and the}; - say q{operator is one of the following: + - * / **}; - return; - } - - my($operand1a, $operand2a); - - # check that the first operand is a roman numeral - if (isroman($operand1r)) { - # it is a roman numeral, convert it - $operand1a = arabic($operand1r); - } - else { - say "'$operand1r' is not a roman numberal!"; - return; - } - - # check that the second operand is a roman numeral - if (isroman($operand2r)) { - # it is a roman numeral, convert it - $operand2a = arabic($operand2r); - } - else { - say "'$operand2r' is not a roman numberal!"; - return; - } - - # calculate the results - my $result; - if ($operator eq '+') { $result = $operand1a + $operand2a; } - elsif ($operator eq '-') { $result = $operand1a - $operand2a; } - elsif ($operator eq '*') { $result = $operand1a * $operand2a; } - elsif ($operator eq '/') { $result = $operand1a / $operand2a; } - elsif ($operator eq '**') { $result = $operand1a ** $operand2a; } - else { - die "Unknown operator '$operator'; valid operators are + - * / **\n"; - } - - # handle all the special output cases - if ($result == 0) { - say "$operand1r $operator $operand2r => nulla " - . "(they knew about zero but didn't have a symbol)"; - } - elsif (int($result) != $result) { - say "$operand1r $operator $operand2r => non potest " - . "(they didn't do fractions)"; - } - elsif ($result > 3999) { - say "$operand1r $operator $operand2r => non potest " - . "(they only went up to 3999)"; - } - elsif ($result < 0) { - say "$operand1r $operator $operand2r => non potest " - . "(they didn't do negative numbers)"; - } - else { - say "$operand1r $operator $operand2r => " . uc roman($result); - } -} - -# while we have input on STDIN, process the calculations -while (my $line = <>) { - chomp $line; - do_arithmetic($line); -} \ No newline at end of file diff --git a/challenge-227/packy-anderson/perl/task-1.pl b/challenge-227/packy-anderson/perl/task-1.pl new file mode 100755 index 0000000000..1bfd2a1d13 --- /dev/null +++ b/challenge-227/packy-anderson/perl/task-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use v5.38; + +# let's use the core modules for date manipulation +use Time::Piece; +use Time::Seconds qw( ONE_DAY ); + +# get the year from the command line +my $year = shift @ARGV + or die "usage: $0 year\n"; + +# do bounds checking as specified in the problem +if ($year < 1753 || $year > 9999) { + die "Only years between 1753 to 9999 are allowed ($year is out of range)\n"; +} + +# create an object for Jan 01 of the given year +my $t = Time::Piece->strptime("$year-01-01", "%Y-%m-%d") + ->truncate(to => 'day'); + +# find the first friday +# in Time::Piece->wday, 1 = Sunday, 6 = Friday +while ( $t->wday != 6) { + $t += ONE_DAY; # add 1 day +} + +# now keep adding 7 days to the date until the year changes, +# noting how many times the day of the month is 13 +my $thirteen_count = 0; +while ( $t->year == $year ) { + $thirteen_count++ if $t->mday == 13; + $t += ONE_DAY * 7; +} + +say "Input: \$year = $year"; +say "Output: $thirteen_count"; \ No newline at end of file diff --git a/challenge-227/packy-anderson/perl/task-2.pl b/challenge-227/packy-anderson/perl/task-2.pl new file mode 100755 index 0000000000..425a4341ea --- /dev/null +++ b/challenge-227/packy-anderson/perl/task-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +use v5.38; + +use Roman; # there's a module for handling Roman Numerals! + +sub do_arithmetic { + my $line = shift; + # split the inout line into the three parts: + # the two operands and the infix operator + my($operand1r, $operator, $operand2r) = split /\s+/, $line; + unless (defined $operand1r && + defined $operator && + defined $operand2r) { + say q{Lines must be of the form "operand1 operator operand2"}; + say q{where both operands are valid roman numerals and the}; + say q{operator is one of the following: + - * / **}; + return; + } + + my($operand1a, $operand2a); + + # check that the first operand is a roman numeral + if (isroman($operand1r)) { + # it is a roman numeral, convert it + $operand1a = arabic($operand1r); + } + else { + say "'$operand1r' is not a roman numberal!"; + return; + } + + # check that the second operand is a roman numeral + if (isroman($operand2r)) { + # it is a roman numeral, convert it + $operand2a = arabic($operand2r); + } + else { + say "'$operand2r' is not a roman numberal!"; + return; + } + + # calculate the results + my $result; + if ($operator eq '+') { $result = $operand1a + $operand2a; } + elsif ($operator eq '-') { $result = $operand1a - $operand2a; } + elsif ($operator eq '*') { $result = $operand1a * $operand2a; } + elsif ($operator eq '/') { $result = $operand1a / $operand2a; } + elsif ($operator eq '**') { $result = $operand1a ** $operand2a; } + else { + die "Unknown operator '$operator'; valid operators are + - * / **\n"; + } + + # handle all the special output cases + if ($result == 0) { + say "$operand1r $operator $operand2r => nulla " + . "(they knew about zero but didn't have a symbol)"; + } + elsif (int($result) != $result) { + say "$operand1r $operator $operand2r => non potest " + . "(they didn't do fractions)"; + } + elsif ($result > 3999) { + say "$operand1r $operator $operand2r => non potest " + . "(they only went up to 3999)"; + } + elsif ($result < 0) { + say "$operand1r $operator $operand2r => non potest " + . "(they didn't do negative numbers)"; + } + else { + say "$operand1r $operator $operand2r => " . uc roman($result); + } +} + +# while we have input on STDIN, process the calculations +while (my $line = <>) { + chomp $line; + do_arithmetic($line); +} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/ch-1.raku b/challenge-227/packy-anderson/raku/ch-1.raku deleted file mode 100755 index d9fef52c0d..0000000000 --- a/challenge-227/packy-anderson/raku/ch-1.raku +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env raku - -sub MAIN($year) { - # do bounds checking as specified in the problem - if ($year < 1753 || $year > 9999) { - say "Only years between 1753 to 9999 are allowed ($year is out of range)"; - exit 1; - } - - # create an object for Jan 01 of the given year - my $t = Date.new($year, 1, 1); - - # find the first friday - # in Date.day-of-week, 0 = Sunday, 5 = Friday - while ( $t.day-of-week != 5) { - $t++; # add 1 day - } - - # now keep adding 7 days to the date until the year changes, - # noting how many times the day of the month is 13 - my $thirteen_count = 0; - while ( $t.year == $year ) { - $thirteen_count++ if $t.day == 13; - $t += 7; - } - - say "Input: \$year = $year"; - say "Output: $thirteen_count"; -} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/ch-2.raku b/challenge-227/packy-anderson/raku/ch-2.raku deleted file mode 100755 index 252340956f..0000000000 --- a/challenge-227/packy-anderson/raku/ch-2.raku +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env raku -use Math::Roman; # it's v0.0.1, but usable - -sub isroman ( $var ) { - # Math::Roman doesn't have a test to see if a string is - # a Roman numeral, but it does throw an exception if it - # cannot convert it - my $result; - try { - CATCH { - default { - return False; - } - } - $result = Math::Roman.new: $var; - } - # Math::Roman also doesn't respect the maximum of 3999 - if ($result.as-arabic > 3999) { - return False; - } - - return True; -} - -sub do_arithmetic (Str $line) { - # split the inout line into the three parts: - # the two operands and the infix operator - my ($operand1, $operator, $operand2) = $line.split(/\s+/); - - unless (defined $operand1 && - defined $operator && - defined $operand2) { - say q{Lines must be of the form "operand1 operator operand2"}; - say q{where both operands are valid roman numerals and the}; - say q{operator is one of the following: + - * / **}; - return; - } - - # check that the first operand is a roman numeral - if (isroman($operand1)) { - # it is a roman numeral, convert it - $operand1 = Math::Roman.new: $operand1; - } - else { - say "'$operand1' is not a roman numberal!"; - return; - } - - # check that the second operand is a roman numeral - if (isroman($operand2)) { - # it is a roman numeral, convert it - $operand2 = Math::Roman.new: $operand2; - } - else { - say "'$operand2' is not a roman numberal!"; - return; - } - - # # calculate the results - my $result; - if ($operator eq '+') { - $result = $operand1.as-arabic + $operand2.as-arabic; - } - elsif ($operator eq '-') { - $result = $operand1.as-arabic - $operand2.as-arabic; - } - elsif ($operator eq '*') { - $result = $operand1.as-arabic * $operand2.as-arabic; - } - elsif ($operator eq '/') { - $result = $operand1.as-arabic / $operand2.as-arabic; - } - elsif ($operator eq '**') { - $result = $operand1.as-arabic ** $operand2.as-arabic; - } - else { - die "Unknown operator '$operator'; valid operators are + - * / **\n"; - } - - # handle all the special output cases - if ($result == 0) { - say "$operand1 $operator $operand2 => nulla " - ~ "(they knew about zero but didn't have a symbol)"; - } - elsif ($result.truncate != $result) { - say "$operand1 $operator $operand2 => non potest " - ~ "(they didn't do fractions)"; - } - elsif ($result > 3999) { - say "$operand1 $operator $operand2 => non potest " - ~ "(they only went up to 3999)"; - } - elsif ($result < 0) { - say "$operand1 $operator $operand2 => non potest " - ~ "(they didn't do negative numbers)"; - } - else { - $result = Math::Roman.new: value => $result.Int; - say "$operand1 $operator $operand2 => $result"; - } -} - -# while we have input on STDIN, process the calculations -for $*IN.lines -> $line { - do_arithmetic($line); -} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/task-1.raku b/challenge-227/packy-anderson/raku/task-1.raku new file mode 100755 index 0000000000..d9fef52c0d --- /dev/null +++ b/challenge-227/packy-anderson/raku/task-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub MAIN($year) { + # do bounds checking as specified in the problem + if ($year < 1753 || $year > 9999) { + say "Only years between 1753 to 9999 are allowed ($year is out of range)"; + exit 1; + } + + # create an object for Jan 01 of the given year + my $t = Date.new($year, 1, 1); + + # find the first friday + # in Date.day-of-week, 0 = Sunday, 5 = Friday + while ( $t.day-of-week != 5) { + $t++; # add 1 day + } + + # now keep adding 7 days to the date until the year changes, + # noting how many times the day of the month is 13 + my $thirteen_count = 0; + while ( $t.year == $year ) { + $thirteen_count++ if $t.day == 13; + $t += 7; + } + + say "Input: \$year = $year"; + say "Output: $thirteen_count"; +} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/task-2.raku b/challenge-227/packy-anderson/raku/task-2.raku new file mode 100755 index 0000000000..252340956f --- /dev/null +++ b/challenge-227/packy-anderson/raku/task-2.raku @@ -0,0 +1,106 @@ +#!/usr/bin/env raku +use Math::Roman; # it's v0.0.1, but usable + +sub isroman ( $var ) { + # Math::Roman doesn't have a test to see if a string is + # a Roman numeral, but it does throw an exception if it + # cannot convert it + my $result; + try { + CATCH { + default { + return False; + } + } + $result = Math::Roman.new: $var; + } + # Math::Roman also doesn't respect the maximum of 3999 + if ($result.as-arabic > 3999) { + return False; + } + + return True; +} + +sub do_arithmetic (Str $line) { + # split the inout line into the three parts: + # the two operands and the infix operator + my ($operand1, $operator, $operand2) = $line.split(/\s+/); + + unless (defined $operand1 && + defined $operator && + defined $operand2) { + say q{Lines must be of the form "operand1 operator operand2"}; + say q{where both operands are valid roman numerals and the}; + say q{operator is one of the following: + - * / **}; + return; + } + + # check that the first operand is a roman numeral + if (isroman($operand1)) { + # it is a roman numeral, convert it + $operand1 = Math::Roman.new: $operand1; + } + else { + say "'$operand1' is not a roman numberal!"; + return; + } + + # check that the second operand is a roman numeral + if (isroman($operand2)) { + # it is a roman numeral, convert it + $operand2 = Math::Roman.new: $operand2; + } + else { + say "'$operand2' is not a roman numberal!"; + return; + } + + # # calculate the results + my $result; + if ($operator eq '+') { + $result = $operand1.as-arabic + $operand2.as-arabic; + } + elsif ($operator eq '-') { + $result = $operand1.as-arabic - $operand2.as-arabic; + } + elsif ($operator eq '*') { + $result = $operand1.as-arabic * $operand2.as-arabic; + } + elsif ($operator eq '/') { + $result = $operand1.as-arabic / $operand2.as-arabic; + } + elsif ($operator eq '**') { + $result = $operand1.as-arabic ** $operand2.as-arabic; + } + else { + die "Unknown operator '$operator'; valid operators are + - * / **\n"; + } + + # handle all the special output cases + if ($result == 0) { + say "$operand1 $operator $operand2 => nulla " + ~ "(they knew about zero but didn't have a symbol)"; + } + elsif ($result.truncate != $result) { + say "$operand1 $operator $operand2 => non potest " + ~ "(they didn't do fractions)"; + } + elsif ($result > 3999) { + say "$operand1 $operator $operand2 => non potest " + ~ "(they only went up to 3999)"; + } + elsif ($result < 0) { + say "$operand1 $operator $operand2 => non potest " + ~ "(they didn't do negative numbers)"; + } + else { + $result = Math::Roman.new: value => $result.Int; + say "$operand1 $operator $operand2 => $result"; + } +} + +# while we have input on STDIN, process the calculations +for $*IN.lines -> $line { + do_arithmetic($line); +} \ No newline at end of file -- cgit From 87f2e36eff988be70318ba16552579c8981529d2 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 31 Jul 2023 11:55:12 -0400 Subject: More filename fixes --- challenge-227/packy-anderson/README | 68 ---------------------------------- challenge-227/packy-anderson/README.md | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 challenge-227/packy-anderson/README create mode 100644 challenge-227/packy-anderson/README.md diff --git a/challenge-227/packy-anderson/README b/challenge-227/packy-anderson/README deleted file mode 100644 index 1b230ba294..0000000000 --- a/challenge-227/packy-anderson/README +++ /dev/null @@ -1,68 +0,0 @@ -# Solutions by Packy Anderson - -## Perl - -* [Task 1](perl/task-1.pl) - -Sample output -``` -$ perl/task-1.pl 2023 -Input: $year = 2023 -Output: 2 -``` -* [Task 2](perl/task-2.pl) - -Sample output -``` -$ perl/task-2.pl < task-2-input.txt -IV + V => IX -M - I => CMXCIX -X / II => V -XI * VI => LXVI -VII ** III => CCCXLIII -V - V => nulla (they knew about zero but didn't have a symbol) -V / II => non potest (they didn't do fractions) -MMM + M => non potest (they only went up to 3999) -V - X => non potest (they didn't do negative numbers) -$ echo "MMMM" | perl/task-2.pl -Lines must be of the form "operand1 operator operand2" -where both operands are valid roman numerals and the -operator is one of the following: + - * / ** -$ echo "X + Y" | perl/task-2.pl -'Y' is not a roman numberal! -``` - -## Raku - -* [Task 1](raku/task-1.raku) - -Sample output -``` -$ raku/task-1.raku 2023 -Input: $year = 2023 -Output: 2 -``` - -* [Task 2](raku/task-2.raku) - -Sample output -``` -$ raku/task-2.raku < task-2-input.txt -IV + V => IX -M - I => CMXCIX -X / II => V -XI * VI => LXVI -VII ** III => CCCXLIII -V - V => nulla (they knew about zero but didn't have a symbol) -V / II => non potest (they didn't do fractions) -MMM + M => non potest (they only went up to 3999) -V - X => non potest (they didn't do negative numbers) -``` - -## Data File - -[task-2-input.txt](task-2-input.txt) - -## Blog Post - -[Perl Weekly Challenge 227](http://packy.dardan.com/2023/07/27/perl-weekly-challenge-227/) \ No newline at end of file diff --git a/challenge-227/packy-anderson/README.md b/challenge-227/packy-anderson/README.md new file mode 100644 index 0000000000..1b230ba294 --- /dev/null +++ b/challenge-227/packy-anderson/README.md @@ -0,0 +1,68 @@ +# Solutions by Packy Anderson + +## Perl + +* [Task 1](perl/task-1.pl) + +Sample output +``` +$ perl/task-1.pl 2023 +Input: $year = 2023 +Output: 2 +``` +* [Task 2](perl/task-2.pl) + +Sample output +``` +$ perl/task-2.pl < task-2-input.txt +IV + V => IX +M - I => CMXCIX +X / II => V +XI * VI => LXVI +VII ** III => CCCXLIII +V - V => nulla (they knew about zero but didn't have a symbol) +V / II => non potest (they didn't do fractions) +MMM + M => non potest (they only went up to 3999) +V - X => non potest (they didn't do negative numbers) +$ echo "MMMM" | perl/task-2.pl +Lines must be of the form "operand1 operator operand2" +where both operands are valid roman numerals and the +operator is one of the following: + - * / ** +$ echo "X + Y" | perl/task-2.pl +'Y' is not a roman numberal! +``` + +## Raku + +* [Task 1](raku/task-1.raku) + +Sample output +``` +$ raku/task-1.raku 2023 +Input: $year = 2023 +Output: 2 +``` + +* [Task 2](raku/task-2.raku) + +Sample output +``` +$ raku/task-2.raku < task-2-input.txt +IV + V => IX +M - I => CMXCIX +X / II => V +XI * VI => LXVI +VII ** III => CCCXLIII +V - V => nulla (they knew about zero but didn't have a symbol) +V / II => non potest (they didn't do fractions) +MMM + M => non potest (they only went up to 3999) +V - X => non potest (they didn't do negative numbers) +``` + +## Data File + +[task-2-input.txt](task-2-input.txt) + +## Blog Post + +[Perl Weekly Challenge 227](http://packy.dardan.com/2023/07/27/perl-weekly-challenge-227/) \ No newline at end of file -- cgit From bd5074fb310d4f64f48dcd2d6cbe187897321f1f Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 31 Jul 2023 16:56:36 +0100 Subject: Week 228 submission --- challenge-228/peter-campbell-smith/blog.txt | 1 + challenge-228/peter-campbell-smith/perl/ch-1.pl | 28 +++++++++++++++ challenge-228/peter-campbell-smith/perl/ch-2.pl | 47 +++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 challenge-228/peter-campbell-smith/blog.txt create mode 100755 challenge-228/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-228/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-228/peter-campbell-smith/blog.txt b/challenge-228/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..2e884314fc --- /dev/null +++ b/challenge-228/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/228 diff --git a/challenge-228/peter-campbell-smith/perl/ch-1.pl b/challenge-228/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..4bd5617017 --- /dev/null +++ b/challenge-228/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-31 +use utf8; # Week 228 task 1 - Unique sum +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use List::Uniq 'uniq'; + +my ($j, @int); + +unique_sum(2, 1, 3, 2); +unique_sum(1, 1, 1, 1); + +# longer example +for $j (1 .. 12) { + push (@int, int(rand(9) + 1)); +} +unique_sum(@int); + +sub unique_sum { + + my $sum; + $sum += $_ for uniq(@_); + + say qq[\nInput: \@int = (] . join(', ', @_) . ')'; + say qq[Output: ] . (defined($sum) ? $sum : 0); +} diff --git a/challenge-228/peter-campbell-smith/perl/ch-2.pl b/challenge-228/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d46fea1613 --- /dev/null +++ b/challenge-228/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-31 +use utf8; # Week 228 task 2 - Empty array +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +my ($j, @int); + +empty_array(3, 4, 2); +empty_array(1, 2, 3); +empty_array(1); +empty_array(); +empty_array(8, 3, 4, 7, 6, 2, 9, 1); + +sub empty_array { + + my (@array, $smallest, $operations, $explain); + + @array = @_; + $operations = 0; + $explain = ''; + while (1) { + + # if it's empty we're done + unless (@array) { + say qq[\nInput: \@int = (] . join (', ', @_) . ')'; + say qq[Output: $operations$explain]; + return; + } + + # if the first element is the smallest then remove it + $operations ++; + $smallest = $array[0]; + $smallest = $_ < $smallest ? $_ : $smallest for @array; + + if ($array[0] == $smallest) { + shift @array; + $explain .= qq[\n Operation $operations: remove element $smallest: (] . join(', ', @array) . ')'; + + # otherwise move it to the end + } else { + push(@array, shift @array); + $explain .= qq[\n Operation $operations: move element $array[-1] to the end: (] . join(', ', @array) . ')'; + } + } +} -- cgit From 6ae2664bf24eb719a58990e5485274c7609f201f Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 31 Jul 2023 11:20:15 -0600 Subject: SOlve PWC228 --- challenge-228/wlmb/blog.txt | 1 + challenge-228/wlmb/perl/ch-1.pl | 14 ++++++++++++++ challenge-228/wlmb/perl/ch-2.pl | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 challenge-228/wlmb/blog.txt create mode 100755 challenge-228/wlmb/perl/ch-1.pl create mode 100755 challenge-228/wlmb/perl/ch-2.pl diff --git a/challenge-228/wlmb/blog.txt b/challenge-228/wlmb/blog.txt new file mode 100644 index 0000000000..be2a50f129 --- /dev/null +++ b/challenge-228/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/07/31/PWC228/ diff --git a/challenge-228/wlmb/perl/ch-1.pl b/challenge-228/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..fd5a773be1 --- /dev/null +++ b/challenge-228/wlmb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 228 +# Task 1: Unique Sum +# +# See https://wlmb.github.io/2023/07/31/PWC228/#task-1-unique-sum +use v5.36; +use List::Util qw(sum0); +die <<~"FIN" unless @ARGV; + Usage $0 N1 [N2... ] + to sum unique elements of the array (N1 N2...) + FIN +my %count; +++$count{$_} for @ARGV; +say "@ARGV -> ", sum0 grep {$count{$_}==1} @ARGV diff --git a/challenge-228/wlmb/perl/ch-2.pl b/challenge-228/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..214720e552 --- /dev/null +++ b/challenge-228/wlmb/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 228 +# Task 2: Empty Array +# +# See https://wlmb.github.io/2023/07/31/PWC228/#task-2-empty-array +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV; + Usage $0 N1 [N2...] + to count how many operations are required to empty the + array (N1 N2...) + FIN +my $x=(my $in=pdl(@ARGV))->copy; +my $count=0; +while($x->nelem>1){ # while there are two or more elements + my $min=$x->min; + my $idx=which($x==$min)->at(0); # index of minimum element + $count+=$idx+1; # num. of ops. to empty minimum element + $x=$x->rotate(-$idx) # bring smallest element to the front, rotating to the + # end the initial larger elements + ->slice([1,-1,1]); # remove smallest element, keep the rest +} +++$count; # add the removal of the last element +say "$in -> $count"; -- cgit From c3f8b7f1b9db49c31cae54702aaefa86ddbd11f5 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 31 Jul 2023 19:08:19 +0100 Subject: add solutions for week 228 in perl --- challenge-228/steven-wilson/perl/ch-01.pl | 24 ++++++++++