aboutsummaryrefslogtreecommitdiff
path: root/challenge-278
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-278')
-rwxr-xr-xchallenge-278/alexander-karelas/perl/ch-1.pl19
-rwxr-xr-xchallenge-278/alexander-karelas/perl/ch-2.pl18
-rw-r--r--challenge-278/athanasius/perl/ch-1.pl189
-rw-r--r--challenge-278/athanasius/perl/ch-2.pl176
-rw-r--r--challenge-278/athanasius/raku/ch-1.raku181
-rw-r--r--challenge-278/athanasius/raku/ch-2.raku170
-rw-r--r--challenge-278/conor-hoekstra/ch-1.bqn11
-rw-r--r--challenge-278/conor-hoekstra/ch-2.bqn9
-rwxr-xr-xchallenge-278/e-choroba/perl/ch-1.pl29
-rwxr-xr-xchallenge-278/e-choroba/perl/ch-2.pl21
-rwxr-xr-xchallenge-278/eric-cheung/python/ch-1.py10
-rwxr-xr-xchallenge-278/eric-cheung/python/ch-2.py18
-rwxr-xr-xchallenge-278/feng-chang/raku/ch-1.raku5
-rwxr-xr-xchallenge-278/feng-chang/raku/ch-2.raku8
-rwxr-xr-xchallenge-278/feng-chang/raku/test.raku29
-rw-r--r--challenge-278/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-278/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-278/jeanluc2020/perl/ch-1.pl60
-rwxr-xr-xchallenge-278/jeanluc2020/perl/ch-2.pl54
-rw-r--r--challenge-278/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-278/laurent-rosenfeld/perl/ch-1.pl22
-rw-r--r--challenge-278/laurent-rosenfeld/raku/ch-1.raku17
-rw-r--r--challenge-278/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-10.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-278/luca-ferrari/blog-9.txt1
-rw-r--r--challenge-278/luca-ferrari/pljava/pom.xml6
-rw-r--r--challenge-278/luca-ferrari/pljava/src/main/java/Task1.java70
-rw-r--r--challenge-278/luca-ferrari/pljava/src/main/java/Task2.java74
-rw-r--r--challenge-278/luca-ferrari/plperl/ch-1.plperl26
-rw-r--r--challenge-278/luca-ferrari/plperl/ch-2.plperl28
-rw-r--r--challenge-278/luca-ferrari/plpgsql/ch-1.sql24
-rw-r--r--challenge-278/luca-ferrari/plpgsql/ch-2.sql44
-rw-r--r--challenge-278/luca-ferrari/python/ch-1.py30
-rw-r--r--challenge-278/luca-ferrari/python/ch-2.py33
-rw-r--r--challenge-278/luca-ferrari/raku/ch-1.raku18
-rw-r--r--challenge-278/luca-ferrari/raku/ch-2.raku20
-rw-r--r--challenge-278/mark-anderson/raku/ch-1.raku18
-rw-r--r--challenge-278/mark-anderson/raku/ch-2.raku13
-rwxr-xr-xchallenge-278/mattneleigh/perl/ch-1.pl71
-rwxr-xr-xchallenge-278/mattneleigh/perl/ch-2.pl68
-rw-r--r--challenge-278/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-278/peter-campbell-smith/perl/ch-1.pl28
-rwxr-xr-xchallenge-278/peter-campbell-smith/perl/ch-2.pl29
-rwxr-xr-xchallenge-278/peter-meszaros/perl/ch-1.pl58
-rwxr-xr-xchallenge-278/peter-meszaros/perl/ch-2.pl56
-rw-r--r--challenge-278/pokgopun/go/ch-1.go124
-rw-r--r--challenge-278/pokgopun/go/ch-2.go71
-rw-r--r--challenge-278/pokgopun/python/ch-1.py67
-rw-r--r--challenge-278/pokgopun/python/ch-2.py55
-rw-r--r--challenge-278/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-278/robbie-hatley/perl/ch-1.pl94
-rwxr-xr-xchallenge-278/robbie-hatley/perl/ch-2.pl86
-rw-r--r--challenge-278/steven-wilson/python/ch-1.py27
-rw-r--r--challenge-278/steven-wilson/python/ch-2.py29
-rw-r--r--challenge-278/wlmb/blog.txt1
-rwxr-xr-xchallenge-278/wlmb/perl/ch-1.pl22
-rwxr-xr-xchallenge-278/wlmb/perl/ch-2.pl18
-rw-r--r--challenge-278/zapwai/c/ch-1.c51
-rw-r--r--challenge-278/zapwai/c/ch-2.c55
-rw-r--r--challenge-278/zapwai/javascript/ch-1.js34
-rw-r--r--challenge-278/zapwai/javascript/ch-2.js24
-rw-r--r--challenge-278/zapwai/perl/ch-1.pl35
-rw-r--r--challenge-278/zapwai/perl/ch-2.pl25
-rw-r--r--challenge-278/zapwai/python/ch-1.py32
-rw-r--r--challenge-278/zapwai/python/ch-2.py24
-rw-r--r--challenge-278/zapwai/rust/ch-1.rs36
-rw-r--r--challenge-278/zapwai/rust/ch-2.rs32
74 files changed, 2714 insertions, 3 deletions
diff --git a/challenge-278/alexander-karelas/perl/ch-1.pl b/challenge-278/alexander-karelas/perl/ch-1.pl
new file mode 100755
index 0000000000..8ec69eb514
--- /dev/null
+++ b/challenge-278/alexander-karelas/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use v5.40;
+
+use Test::More;
+
+sub do_it ($str) {
+ my @words = $str =~ /(\w+)/g;
+ @words = map [chop, $_], @words;
+ @words = sort { $a->[0] <=> $b->[0] } @words;
+ @words = map $_->[1], @words;
+ return "@words";
+}
+
+is do_it('and2 Raku3 cousins5 Perl1 are4'), 'Perl and Raku are cousins', 'Example 1';
+is do_it('guest6 Python1 most4 the3 popular5 is2 language7'), 'Python is the most popular guest language', 'Example 2';
+is do_it('Challenge3 The1 Weekly2'), 'The Weekly Challenge', 'Example 3';
+
+done_testing(); \ No newline at end of file
diff --git a/challenge-278/alexander-karelas/perl/ch-2.pl b/challenge-278/alexander-karelas/perl/ch-2.pl
new file mode 100755
index 0000000000..2ecb0a3881
--- /dev/null
+++ b/challenge-278/alexander-karelas/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+
+use v5.40;
+
+use Test::More;
+
+sub do_it ($str, $char) {
+ my ($substr1, $substr2) = $str =~ /^(.*?\Q$char\E)?(.*)\z/;
+ $substr1 //= '';
+ $substr1 = join '', sort(split //, $substr1);
+ return "$substr1$substr2";
+}
+
+is do_it('challenge', 'e'), 'acehllnge', 'Example 1';
+is do_it('programming', 'a'), 'agoprrmming', 'Example 2';
+is do_it('champion', 'b'), 'champion', 'Example 3';
+
+done_testing(); \ No newline at end of file
diff --git a/challenge-278/athanasius/perl/ch-1.pl b/challenge-278/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..0416f377ce
--- /dev/null
+++ b/challenge-278/athanasius/perl/ch-1.pl
@@ -0,0 +1,189 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 278
+=========================
+
+TASK #1
+-------
+*Sort String*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a shuffle string, $str.
+
+Write a script to return the sorted string.
+
+ A string is shuffled by appending word position to each word.
+
+Example 1
+
+ Input: $str = "and2 Raku3 cousins5 Perl1 are4"
+ Output: "Perl and Raku are cousins"
+
+Example 2
+
+ Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7"
+ Output: "Python is the most popular guest language"
+
+Example 3
+
+ Input: $str = "Challenge3 The1 Weekly2"
+ Output: "The Weekly Challenge"
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A "shuffle[d] string" is entered as a single argument on the command-line.
+
+=cut
+#===============================================================================
+
+use v5.32.1; # Enables strictures
+use warnings;
+use Const::Fast;
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 <str>
+ perl $0
+
+ <str> Shuffled string in which each word has its position appended
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 278, Task #1: Sort String (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ my $argc = scalar @ARGV;
+
+ if ($argc == 0)
+ {
+ run_tests();
+ }
+ elsif ($argc == 1)
+ {
+ my $str = $ARGV[ 0 ];
+
+ print qq[Input: \$str = "$str"\n];
+
+ my $sorted = sort_sentence( $str );
+
+ print qq[Output: "$sorted"\n];
+ }
+ else
+ {
+ error( "Expected 1 or 0 command-line arguments, found $argc" );
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub sort_sentence
+#-------------------------------------------------------------------------------
+{
+ my ($str) = @_;
+ my @words = split / \s+ /x, $str;
+ my @sorted = (undef) x scalar @words;
+
+ for my $shuffle_word (@words)
+ {
+ $shuffle_word =~ / ^ ( .* \D ) ( \d+ ) $ /x
+ or error( qq[\n"$shuffle_word" is not a valid shuffle word] );
+
+ my ($word, $ord) = ($1, $2);
+
+ $ord =~ / ^ $RE{num}{int} $ /x
+ or error( qq["$shuffle_word" does not end in a valid integer] );
+
+ $ord > 0
+ or error( qq["$shuffle_word" does not end in a positive integer] );
+
+ defined $sorted[ $ord ]
+ and error( "Duplicate position $ord found" );
+
+ $sorted[ $ord ] = $word;
+ }
+
+ for my $i (1 .. $#sorted)
+ {
+ defined $sorted[ $i ]
+ or error( "No word found for position $i" );
+ }
+
+ return join ' ', @sorted[ 1 .. $#sorted ];
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ while ($line =~ / \\ $ /x)
+ {
+ $line =~ s/ \\ $ / /x;
+
+ my $next = <DATA>;
+
+ $next =~ s/ ^ \s+ //x;
+ $line .= $next;
+ }
+
+ my ($test_name, $str, $expected) = split / \| /x, $line;
+
+ for ($test_name, $str, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my $sorted = sort_sentence( $str );
+
+ is $sorted, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1|and2 Raku3 cousins5 Perl1 are4|Perl and Raku are cousins
+Example 2|guest6 Python1 most4 the3 popular5 is2 language7 \
+ |Python is the most popular guest language
+Example 3|Challenge3 The1 Weekly2|The Weekly Challenge
diff --git a/challenge-278/athanasius/perl/ch-2.pl b/challenge-278/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..15c40daf8c
--- /dev/null
+++ b/challenge-278/athanasius/perl/ch-2.pl
@@ -0,0 +1,176 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 278
+=========================
+
+TASK #2
+-------
+*Reverse Word*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a word, $word and a character, $char.
+
+Write a script to replace the substring up to and including $char with its
+characters sorted alphabetically. If the $char doesn't exist then DON'T do
+anything.
+
+Example 1
+
+ Input: $str = "challenge", $char = "e"
+ Output: "acehllnge"
+
+Example 2
+
+ Input: $str = "programming", $char = "a"
+ Output: "agoprrmming"
+
+Example 3
+
+ Input: $str = "champion", $char = "b"
+ Output: "champion"
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Assumptions
+-----------
+1. From Example 1 it appears that the substring to be sorted extends from the
+ start of $word up to (and including) the FIRST occurrence of $char ONLY.
+2. Character-matching is case-sensitive, so, e.g., "E" does NOT match "e".
+3. In alphabetical sorting, "Z" comes before "a".
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A word and a single character are entered as two positional arguments on the
+ command-line.
+
+=cut
+#===============================================================================
+
+use v5.32.1; # Enables strictures
+use warnings;
+use Const::Fast;
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 <word> <char>
+ perl $0
+
+ <word> A word
+ <char> A single character
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 278, Task #2: Reverse Word (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ my $argc = scalar @ARGV;
+
+ if ($argc == 0)
+ {
+ run_tests();
+ }
+ elsif ($argc == 2)
+ {
+ my ($word, $char) = @ARGV;
+
+ length $char == 1 or error( qq[Invalid character "$char"] );
+
+ print qq[Input: \$word = "$word", \$char = "$char"\n];
+
+ my $reversed = reverse_word( $word, $char );
+
+ print qq[Output: "$reversed"\n];
+ }
+ else
+ {
+ error( "Expected 0 or 2 command-line arguments, found $argc" );
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub reverse_word
+#-------------------------------------------------------------------------------
+{
+ my ($word, $char) = @_;
+
+ length $char == 1 or die qq[Invalid character "$char"];
+
+ my $reversed = $word;
+
+ if ($word =~ / ^ (.*? $char) (.*) $ /x)
+ {
+ $reversed = join( '', sort split '', $1 ) . $2;
+ }
+ # else leave the word unchanged
+
+ return $reversed;
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $word, $char, $expected) = split / \| /x, $line;
+
+ for ($test_name, $word, $char, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my $reversed = reverse_word( $word, $char );
+
+ is $reversed, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1 |challenge |e|acehllnge
+Example 2 |programming|a|agoprrmming
+Example 3 |champion |b|champion
+Final char|champagne |e|aaceghmnp
+First char|xabc |x|xabc
+Empty | |a|
+One char |t |t|t
+Reversed |zyxwvutsrqp|p|pqrstuvwxyz
diff --git a/challenge-278/athanasius/raku/ch-1.raku b/challenge-278/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..3f3e4d1319
--- /dev/null
+++ b/challenge-278/athanasius/raku/ch-1.raku
@@ -0,0 +1,181 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 278
+=========================
+
+TASK #1
+-------
+*Sort String*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a shuffle string, $str.
+
+Write a script to return the sorted string.
+
+ A string is shuffled by appending word position to each word.
+
+Example 1
+
+ Input: $str = "and2 Raku3 cousins5 Perl1 are4"
+ Output: "Perl and Raku are cousins"
+
+Example 2
+
+ Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7"
+ Output: "Python is the most popular guest language"
+
+Example 3
+
+ Input: $str = "Challenge3 The1 Weekly2"
+ Output: "The Weekly Challenge"
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A "shuffle[d] string" is entered as a single argument on the command-line.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+subset Pos of Int where * > 0;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 278, Task #1: Sort String (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ Str:D $str #= Shuffled string in which each word has its position appended
+)
+#===============================================================================
+{
+ qq[Input: \$str = "$str"].put;
+
+ my Str $sorted = sort-sentence( $str );
+
+ qq[Output: "$sorted"].put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub sort-sentence( Str:D $str --> Str:D )
+#-------------------------------------------------------------------------------
+{
+ my Str @words = $str.split: / \s+ /, :skip-empty;
+ my Str @sorted;
+
+ for @words -> Str $shuffle-word
+ {
+