diff options
| author | MatthiasMuth <99873492+MatthiasMuth@users.noreply.github.com> | 2023-05-21 21:59:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-21 21:59:46 +0200 |
| commit | dba8fa4ad8ac05cd7f92b1b8df63857d1d4b59c4 (patch) | |
| tree | deefa7edb1683505d41b95954b0dfc5b51a1a6e1 | |
| parent | f6448daebe7cffd67e484af066ebd21ad8e614b4 (diff) | |
| parent | 87c0bedd3ccc6c2459b62fc91429676507504d15 (diff) | |
| download | perlweeklychallenge-club-dba8fa4ad8ac05cd7f92b1b8df63857d1d4b59c4.tar.gz perlweeklychallenge-club-dba8fa4ad8ac05cd7f92b1b8df63857d1d4b59c4.tar.bz2 perlweeklychallenge-club-dba8fa4ad8ac05cd7f92b1b8df63857d1d4b59c4.zip | |
Merge branch 'manwar:master' into muthm-217
55 files changed, 4412 insertions, 2952 deletions
diff --git a/challenge-071/lubos-kolouch/perl/ch-1.pl b/challenge-071/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5499ab959b --- /dev/null +++ b/challenge-071/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub find_peaks { + my @arr = @_; + my $n = scalar @arr; + + my @peaks; + push @peaks, $arr[0] if $arr[0] > $arr[1]; + + for my $i ( 1 .. $n - 2 ) { + push @peaks, $arr[$i] if $arr[$i] > $arr[ $i - 1 ] and $arr[$i] > $arr[ $i + 1 ]; + } + + push @peaks, $arr[-1] if $arr[-1] > $arr[-2]; + + return @peaks; +} + +# Tests +print join( ", ", find_peaks( 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ) ), "\n"; # 48, 45, 21 +print join( ", ", find_peaks( 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ) ), "\n"; # 47, 32, 39, 36 diff --git a/challenge-071/lubos-kolouch/perl/ch-2.pl b/challenge-071/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1f8970ad3b --- /dev/null +++ b/challenge-071/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +use strict; +use warnings; + +package Node; + +sub new { + my ( $class, $data ) = @_; + my $self = { + data => $data, + next => undef + }; + return bless $self, $class; +} + +package LinkedList; + +sub new { + my ($class) = @_; + my $self = { head => undef }; + return bless $self, $class; +} + +sub append { + my ( $self, $data ) = @_; + my $new_node = Node->new($data); + if ( !$self->{head} ) { + $self->{head} = $new_node; + } + else { + my $cur = $self->{head}; + $cur = $cur->{next} while $cur->{next}; + $cur->{next} = $new_node; + } +} + +sub remove_from_end { + my ( $self, $n ) = @_; + my $size = 0; + my $cur = $self->{head}; + while ($cur) { + $size++; + $cur = $cur->{next}; + } + if ( $n >= $size ) { + $self->{head} = $self->{head}->{next}; + } + else { + $cur = $self->{head}; + for ( 1 .. $size - $n - 1 ) { + $cur = $cur->{next}; + } + $cur->{next} = $cur->{next}->{next} if $cur->{next}; + } +} + +sub print { + my ($self) = @_; + my @values; + my $cur = $self->{head}; + while ($cur) { + push @values, $cur->{data}; + $cur = $cur->{next}; + } + print join( " -> ", @values ), "\n"; +} + +# Tests +my $ll = LinkedList->new(); +for my $i ( 1 .. 5 ) { + $ll->append($i); +} + +for my $i ( 1 .. 6 ) { + $ll->remove_from_end($i); + $ll->print(); +} diff --git a/challenge-071/lubos-kolouch/python/ch-1.py b/challenge-071/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7a92d52f8b --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_peaks(arr: List[int]) -> List[int]: + n = len(arr) + # Initialize peaks list with the first element if it's bigger than the second + peaks = [arr[0]] if arr[0] > arr[1] else [] + + # Iterate over the array checking for peaks + for i in range(1, n-1): + if arr[i] > arr[i-1] and arr[i] > arr[i+1]: + peaks.append(arr[i]) + + # Add the last element if it's bigger than the penultimate + if arr[-1] > arr[-2]: + peaks.append(arr[-1]) + + return peaks + + +# Tests +print(find_peaks([18, 45, 38, 25, 10, 7, 21, 6, 28, 48])) # [48, 45, 21] +print(find_peaks([47, 11, 32, 8, 1, 9, 39, 14, 36, 23])) # [47, 32, 39, 36] diff --git a/challenge-071/lubos-kolouch/python/ch-2.py b/challenge-071/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5662d38226 --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-2.py @@ -0,0 +1,52 @@ +class Node: + def __init__(self, data=None): + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, data): + if not self.head: + self.head = Node(data) + else: + cur = self.head + while cur.next: + cur = cur.next + cur.next = Node(data) + + def remove_from_end(self, n): + if self.head is None: + return + size = 0 + cur = self.head + while cur: + size += 1 + cur = cur.next + if n >= size: + self.head = self.head.next if self.head.next else None + else: + cur = self.head + for _ in range(size - n - 1): + cur = cur.next + cur.next = cur.next.next if cur.next and cur.next.next else None + + def __str__(self): + values = [] + cur = self.head + while cur: + values.append(str(cur.data)) + cur = cur.next + return ' -> '.join(values) + + +# Tests +ll = LinkedList() +for i in range(1, 6): + ll.append(i) + +for i in range(1, 7): + ll.remove_from_end(i) + print(ll) diff --git a/challenge-215/simon-dueck/README b/challenge-215/simon-dueck/README index e69de29bb2..1356b2f16f 100644 --- a/challenge-215/simon-dueck/README +++ b/challenge-215/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck
\ No newline at end of file diff --git a/challenge-215/simon-dueck/fsharp/ch-1.fsx b/challenge-215/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..1367346b2f --- /dev/null +++ b/challenge-215/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,27 @@ +(*
+ You are given a list of words (alphabetic characters only) of same size.
+
+ Write a script to remove all words not sorted alphabetically and print
+ the number of words in the list that are not alphabetically sorted.
+*)
+
+let word_is_not_sorted (word: string): bool =
+ let rec loop (letters: char list): bool =
+ match letters with
+ | x::y::xs when x < y -> loop (y::xs)
+ | [x] -> false
+ | _ -> true
+ in loop (Seq.toList word)
+
+let count_unsorted (words: string list): int =
+ List.filter word_is_not_sorted words |> List.length
+
+
+let words_a = ["abc"; "xyz"; "tsu"]
+printfn $"{count_unsorted words_a}"
+
+let words_b = ["rat"; "cab"; "dad"]
+printfn $"{count_unsorted words_b}"
+
+let words_c = ["x"; "y"; "z"]
+printfn $"{count_unsorted words_c}"
diff --git a/challenge-215/simon-dueck/fsharp/ch-2.fsx b/challenge-215/simon-dueck/fsharp/ch-2.fsx new file mode 100644 index 0000000000..4c3ca66711 --- /dev/null +++ b/challenge-215/simon-dueck/fsharp/ch-2.fsx @@ -0,0 +1,26 @@ +(*
+ You are given a list of numbers having just 0 and 1. You are also given placement count (>=1).
+
+ Write a script to find out if it is possible to replace 0 with 1 in the given list.
+ The only condition is that you can only replace when there is no 1 on either side.
+ Print 1 if it is possible otherwise 0.
+*)
+
+let rec replace_zero (arr: int list) (count: int): int =
+ match arr with
+ | _ when count = 0 -> 1 // all are placed
+ | 0::0::xs -> replace_zero xs (count - 1) // can replace; skip next index as it will not be replaceable
+ | [0] -> replace_zero [] (count - 1) // end of the list is zero and replacable
+ | 1::0::xs -> replace_zero xs count // current and next index are not replaceable
+ | _::xs -> replace_zero xs count // current index is not replaceable but next potentially is or is a one
+ | _ -> 0 // end of list with count not reaching zero
+
+let numbers_a = [1; 0; 0; 0; 1]
+let numbers_b = [1; 0; 0; 0; 1]
+let numbers_c = [1; 0; 0; 0; 0; 0; 0; 0; 1]
+let numbers_d = [1; 0; 0; 0; 1; 0; 0; 1]
+
+printfn $"{replace_zero numbers_a 1}"
+printfn $"{replace_zero numbers_b 2}"
+printfn $"{replace_zero numbers_c 3}"
+printfn $"{replace_zero numbers_d 2}"
\ No newline at end of file diff --git a/challenge-216/simon-dueck/README b/challenge-216/simon-dueck/README index e69de29bb2..1356b2f16f 100644 --- a/challenge-216/simon-dueck/README +++ b/challenge-216/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck
\ No newline at end of file diff --git a/challenge-216/simon-dueck/fsharp/ch-1.fsx b/challenge-216/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..6a777fa28d --- /dev/null +++ b/challenge-216/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,36 @@ +(*
+ You are given a list of words and a random registration number.
+ Write a script to find all the words in the given list that has every letter in the given registration number.
+*)
+
+let word_contains_letter (word: string) (l: char): bool =
+ if l >= 'a' && l <= 'z' then
+ List.contains (char ((int l) - 32)) (Seq.toList word) // to uppercase
+ elif l >= 'A' && l <= 'Z' then
+ List.contains l (Seq.toList word)
+ else true // l is not a letter
+
+let rec word_contains_all_letters (word: string) (l: char list): bool =
+ match l with
+ | x::xs when word_contains_letter word x -> word_contains_all_letters word xs
+ | [] -> true
+ | _ -> false
+
+let contains_all (words: string list) (plate: string): string list =
+ let rec loop (words:string list) (plate: char list) =
+ match words with
+ | word::xs when word_contains_all_letters (word.ToUpper()) plate -> word :: (loop xs plate)
+ | _::xs -> loop xs plate
+ | [] -> []
+ loop words (plate |> Seq.toList)
+
+let words_a = ["abc"; "abcd"; "bcd"]
+let plate_a = "AB1 2CD"
+let words_b = ["job"; "james"; "bjorg"]
+let plate_b = "007 JB"
+let words_c = ["crack"; "road"; "rac"]
+let plate_c = "C7 RA2"
+
+printfn "%A %s -> %A" words_a plate_a (contains_all words_a plate_a)
+printfn "%A %s -> %A" words_b plate_b (contains_all words_b plate_b)
+printfn "%A %s -> %A" words_c plate_c (contains_all words_c plate_c)
diff --git a/challenge-217/athanasius/perl/ch-1.pl b/challenge-217/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..28ba086c1b --- /dev/null +++ b/challenge-217/athanasius/perl/ch-1.pl @@ -0,0 +1,261 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 217 +========================= + +TASK #1 +------- +*Sorted Matrix* + +Submitted by: Mohammad S Anwar + +You are given a n x n matrix where n >= 2. + +Write a script to find 3rd smallest element in the sorted matrix. + +Example 1 + + Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) + Output: 1 + + The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. + The 3rd smallest of the sorted list is 1. + +Example 2 + + Input: @matrix = ([2, 1], [4, 5]) + Output: 4 + + The sorted list of the given matrix: 1, 2, 4, 5. + The 3rd smallest of the sorted list is 4. + +Example 3 + + Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) + Output: 0 + + The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. + The 3rd smallest of the sorted list is 0. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2023 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. If $VERBOSE is set to a true value (the default), the output is followed by + an explanation of the result. + +Assumption +---------- +Matrix elements are integers. + +=cut +#=============================================================================== + +use strict; +use warnings; +use Const::Fast; +use Regexp::Common qw( number ); +use Test::More; + +const my $VERBOSE => 1; +const my $USAGE => +"Usage: + perl $0 <matrix> + perl $0 + + <matrix> String representation of an n x n integer matrix where n >= 2 +"; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 217, Task #1: Sorted Matrix (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $args = scalar @ARGV; + + if ($args == 0) + { + run_tests(); + } + elsif ($args == 1) + { + my $matrix = parse_matrix_string( $ARGV[ 0 ] ); + + printf "Input: \@matrix = %s\n", format_matrix( $matrix ); + + my @elements; + my $third_smallest = find_third_smallest( $matrix, \@elements ); + + printf "Output: %d\n", $third_smallest; + + if ($VERBOSE) + { + printf "\nThe sorted list of the given matrix: %s\n", + join ', ', @elements; + + my $padding = length( $elements[ 0 ] ) + + length( $elements[ 1 ] ) + 4; + + printf "The third-smallest element: %s%s\n", + ' ' x $padding, '^' x length $third_smallest; + } + } + else + { + error( "Expected 0 or 1 command-line arguments, found $args" ); + } +} + +#------------------------------------------------------------------------------- +sub find_third_smallest +#------------------------------------------------------------------------------- +{ + my ($matrix, $elements_ref) = @_; + my @elements; + + push @elements, @$_ for @$matrix; + + @elements = sort { $a <=> $b } @elements; + + scalar @elements >= 3 or error( 'Matrix too small' ); + + @$elements_ref = @elements if $elements_ref; + + return $elements[ 2 ]; +} + +#------------------------------------------------------------------------------- +sub parse_matrix_string +#------------------------------------------------------------------------------- +{ + my ($matrix) = @_; + + $matrix =~ / ^ \s* \( (.*?) \s* \) \s* $ /x + or error( 'Invalid input string' ); + + my $body = $1; + my @matrix; + + while ($body =~ / \[ (.+?) \] /gx) + { + my $row = $1; + my @elems = split / , \s* /x, $row; + + for my $elem (@elems) + { + $elem =~ s/ ^ \s+ //x; + $elem =~ s/ \s+ $ //x; + $elem =~ m/ ^ $RE{num}{int} $ /x + or error( qq["$elem" is not a valid integer] ); + } + + push @matrix, [ @elems ]; + + } + + validate_matrix( \@matrix ); + + return \@matrix; +} + +#------------------------------------------------------------------------------- +sub validate_matrix +#------------------------------------------------------------------------------- +{ + my ($matrix) = @_; + + my $rows = scalar @$matrix; + my $n = scalar @{ $matrix->[ 0 ] }; + + $rows >= 2 or error( 'Too few rows in matrix' ); + $rows == $n or error( 'Matrix is not square' ); + + for my $i (1 .. $#$matrix) + { + my $m = scalar @{ $matrix->[ $i ] }; + my $j = $i + 1; + + $m == $n or error( "In matrix row $j: expected $n elements, found $m" ); + } +} + +#------------------------------------------------------------------------------- +sub format_matrix +#------------------------------------------------------------------------------- +{ + my ($matrix) = @_; + my $matrix_str = '('; + my @row_strs; + + for my $elems (@$matrix) + { + push @row_strs, '[' . join( ', ', @$elems ) . ']'; + } + + $matrix_str .= join( ', ', @row_strs ) . ')'; + + return $matrix_str; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = <DATA>) + { + chomp $line; + + my ($test_name, $matrix_str, $expected) = split / \| /x, $line; + + for ($test_name, $matrix_str, $expected) # Trim whitespace + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my @matrix = parse_matrix_string( $matrix_str ); + my $third_smallest = find_third_smallest( @matrix ); + + is $third_smallest, $expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1|([3, 1, 2], [5, 2, 4], [ 0, 1, 3])| 1 +Example 2|([2, 1], [4, 5]) | 4 +Example 3|([1, 0, 3], [0, 0, 0], [ 1, 2, 1])| 0 +Negatives|([0, -1, -2], [0, -1, -3], [-3, 1, 2])|-2 diff --git a/challenge-217/athanasius/raku/ch-1.raku b/challenge-217/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..43e9d9e547 --- /dev/null +++ b/challenge-217/athanasius/raku/ch-1.raku @@ -0,0 +1,255 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 217 +========================= + +TASK #1 +------- +*Sorted Matrix* + +Submitted by: Mohammad S Anwar + +You are given a n x n matrix where n >= 2. + +Write a script to find 3rd smallest element in the sorted matrix. + +Example 1 + + Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) + Output: 1 + + The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. + The 3rd smallest of the sorted list is 1. + +Example 2 + + Input: @matrix = ([2, 1], [4, 5]) + Output: 4 + + The sorted list of the given matrix: 1, 2, 4, 5. + The 3rd smallest of the sorted list is 4. + +Example 3 + + Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) + Output: 0 + + The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. + The 3rd smallest of the sorted list is 0. +< |
