From a29bbc7dd407a5cf8d78b111a494ffa5b460298d Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 9 Sep 2024 11:34:37 +0000 Subject: w286 - Task 1 & 2 --- challenge-286/perlboy1967/perl/ch1.pl | 36 ++++++++++++++++++++++++++++++ challenge-286/perlboy1967/perl/ch2.pl | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 challenge-286/perlboy1967/perl/ch1.pl create mode 100755 challenge-286/perlboy1967/perl/ch2.pl diff --git a/challenge-286/perlboy1967/perl/ch1.pl b/challenge-286/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..c593627d8d --- /dev/null +++ b/challenge-286/perlboy1967/perl/ch1.pl @@ -0,0 +1,36 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 286 +- L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Self Spammer +Submitted by: David Ferrone + +Write a program which outputs one word of its own script / source code at random. +A word is anything between whitespace, including symbols. + +=cut + +use v5.32; +use feature qw(signatures); +no warnings qw(experimental::signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +use File::Slurp; + +sub selfSpammer { + my @w = grep /\S/, split /\s/, read_file(__FILE__); + $w[rand @w]; +} + +srand(0); + +is(selfSpammer,'Dijke'); + +done_testing; diff --git a/challenge-286/perlboy1967/perl/ch2.pl b/challenge-286/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..43a9e828c9 --- /dev/null +++ b/challenge-286/perlboy1967/perl/ch2.pl @@ -0,0 +1,42 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 286 +- L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Order Game +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints, whose length is a power of 2. + +Write a script to play the order game (min and max) and return the last element. + +=cut + +use v5.32; +use feature qw(signatures); +no warnings qw(experimental::signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +use DDP; +use List::AllUtils qw(min max natatime); + +sub orderGame (@ints) { + while (scalar @ints > 2) { + my @i; + push(@i,min(splice(@ints,0,2)),max(splice(@ints,0,2))) while (@ints); + @ints = @i; + } + min(@ints); +} + +is(1,orderGame(2,1,4,5,6,3,0,2)); +is(0,orderGame(0,5,3,2)); +is(2,orderGame(9,2,1,4,5,6,0,7,3,1,3,5,7,9,0,8)); + +done_testing; -- cgit