diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-04-23 21:02:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-23 21:02:43 +0100 |
| commit | d68f8eef981daf43f4d692cbac871e274252883a (patch) | |
| tree | 95f8e78cf93d17547d0aa5589ea6d586bc59e49c | |
| parent | d3c56f7381f156b45dd87ae1f0ff4bc79a90011d (diff) | |
| parent | 94f36e94f9444e040864ea81144018f1ccc5dd2e (diff) | |
| download | perlweeklychallenge-club-d68f8eef981daf43f4d692cbac871e274252883a.tar.gz perlweeklychallenge-club-d68f8eef981daf43f4d692cbac871e274252883a.tar.bz2 perlweeklychallenge-club-d68f8eef981daf43f4d692cbac871e274252883a.zip | |
Merge pull request #11925 from jeanluc2020/jeanluc2020-318
Add solution 318
| -rw-r--r-- | challenge-318/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-318/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-318/jeanluc2020/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-318/jeanluc2020/perl/ch-2.pl | 79 |
4 files changed, 153 insertions, 0 deletions
diff --git a/challenge-318/jeanluc2020/blog-1.txt b/challenge-318/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..b6ef5a5a38 --- /dev/null +++ b/challenge-318/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-318-1.html diff --git a/challenge-318/jeanluc2020/blog-2.txt b/challenge-318/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..8fa4443541 --- /dev/null +++ b/challenge-318/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-318-2.html diff --git a/challenge-318/jeanluc2020/perl/ch-1.pl b/challenge-318/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..e7e44d89ec --- /dev/null +++ b/challenge-318/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-318/#TASK1 +# +# Task 1: Group Position +# ====================== +# +# You are given a string of lowercase letters. +# +# Write a script to find the position of all groups in the given string. Three +# or more consecutive letters form a group. Return "” if none found. +# +## Example 1 +## +## Input: $str = "abccccd" +## Output: "cccc" +# +# +## Example 2 +## +## Input: $str = "aaabcddddeefff" +## Output: "aaa", "dddd", "fff" +# +# +## Example 3 +## +## Input: $str = "abcdd" +## Output: "" +# +############################################################ +## +## discussion +## +############################################################ +# +# We need to remember the last character and any already existing +# substring of $str that consists of only the same character. +# If the current character matches the last one, we just add it to +# the temporary string. If it doesn't match, we reset $lastchar and +# the temporary string to the current character. In the end, we also +# capture the current $tmpstr and add it to the result list in case it's +# longer than or equal to 3 characters. + +use v5.36; + +group_position("abccccd"); +group_position("aaabcddddeefff"); +group_position("abcdd"); + +sub group_position($str) { + say "Input: \"$str\""; + my @result = (); + + my $tmpstr = ""; + my $lastchar = ""; + my @chars = split//, $str; + foreach my $char (@chars) { + if($char eq $lastchar) { + $tmpstr .= $char; + } else { + if(length($tmpstr) > 2) { + push @result, $tmpstr; + } + $tmpstr = $char; + $lastchar = $char; + } + } + if(length($tmpstr) > 2) { + push @result, $tmpstr; + } + + say "Output: \"" . join("\", \"", @result) . "\""; +} diff --git a/challenge-318/jeanluc2020/perl/ch-2.pl b/challenge-318/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..756b16cd7a --- /dev/null +++ b/challenge-318/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-318/#TASK2 +# +# Task 2: Reverse Equals +# ====================== +# +# You are given two arrays of integers, each containing the same elements as +# the other. +# +# Write a script to return true if one array can be made to equal the other by +# reversing exactly one contiguous subarray. +# +## Example 1 +## +## Input: @source = (3, 2, 1, 4) +## @target = (1, 2, 3, 4) +## Output: true +## +## Reverse elements: 0-2 +# +# +## Example 2 +## +## Input: @source = (1, 3, 4) +## @target = (4, 1, 3) +## Output: false +# +# +## Example 3 +## +## Input: @source = (2) +## @target = (2) +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# We let two variables walk the indices in the array, starting at +# 0 and going up to the index of the last element. Then we cut the +# source array into 3 parts: +# - a slice from the start to $i - 1 +# - a slice from $i to $j +# - a slice from $j + 1 until the index of the last element +# Then we put all of these parts together, reversing the second of these +# subarrays. If the temporary array is the same as @target we found +# a solution, so we can return true. If in the end, we didn't find a +# solution, we need to return false. +# + +use v5.36; + +reverse_equals( [3, 2, 1, 4], [1, 2, 3, 4] ); +reverse_equals( [1, 3, 4], [4, 1, 3] ); +reverse_equals( [2], [2] ); + +sub reverse_equals( $source, $target ) { + my @source = @$source; + my @target = @$target; + say "Input: (" . join(", ", @source) . "), (" . join(", ", @target) . ")"; + return say "Output: false" unless scalar(@source) == scalar(@target); + foreach my $i (0..$#source) { + foreach my $j ($i..$#source) { + my @tmp = @source[0..$i-1]; + push @tmp, reverse @source[$i..$j]; + push @tmp, @source[$j+1..$#source]; + my $matching = 1; + foreach my $k (0..$#tmp) { + if($tmp[$k] != $target[$k]) { + $matching = 0; + } + } + return say "Output: true" if $matching; + } + } + say "Output: false"; +} |
