diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-13 13:23:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-13 13:23:17 +0100 |
| commit | 4fa3f69176091cafd39d2fd070c25fcc4d2a11b7 (patch) | |
| tree | 6f4442bb5866b5d789ca7d13d150f2eb87d3de40 | |
| parent | 27bd6ee96e9a37c1ffa7bc269060062795bbaae4 (diff) | |
| parent | 362f90d8f2015fdd286da5e67c61807f5859b555 (diff) | |
| download | perlweeklychallenge-club-4fa3f69176091cafd39d2fd070c25fcc4d2a11b7.tar.gz perlweeklychallenge-club-4fa3f69176091cafd39d2fd070c25fcc4d2a11b7.tar.bz2 perlweeklychallenge-club-4fa3f69176091cafd39d2fd070c25fcc4d2a11b7.zip | |
Merge pull request #12021 from pme/challenge-321
challenge-321
| -rwxr-xr-x | challenge-321/peter-meszaros/perl/ch-1.pl | 77 | ||||
| -rwxr-xr-x | challenge-321/peter-meszaros/perl/ch-2.pl | 66 | ||||
| -rwxr-xr-x | challenge-321/peter-meszaros/tcl/ch-1.tcl | 78 | ||||
| -rwxr-xr-x | challenge-321/peter-meszaros/tcl/ch-2.tcl | 65 |
4 files changed, 286 insertions, 0 deletions
diff --git a/challenge-321/peter-meszaros/perl/ch-1.pl b/challenge-321/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..c1dc73f1f6 --- /dev/null +++ b/challenge-321/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Distinct Average + +Submitted by: Mohammad Sajid Anwar + +You are given an array of numbers with even length. + +Write a script to return the count of distinct average. The average is +calculate by removing the minimum and the maximum, then average of the two. + +=head2 Example 1 + + Input: @nums = (1, 2, 4, 3, 5, 6) + Output: 1 + + Step 1: Min = 1, Max = 6, Avg = 3.5 + Step 2: Min = 2, Max = 5, Avg = 3.5 + Step 3: Min = 3, Max = 4, Avg = 3.5 + + The count of distinct average is 1. + +=head2 Example 2 + + Input: @nums = (0, 2, 4, 8, 3, 5) + Output: 2 + + Step 1: Min = 0, Max = 8, Avg = 4 + Step 2: Min = 2, Max = 5, Avg = 3.5 + Step 3: Min = 3, Max = 4, Avg = 3.5 + + The count of distinct average is 2. + +=head2 Example 3 + + Input: @nums = (7, 3, 1, 0, 5, 9) + Output: 2 + + Step 1: Min = 0, Max = 9, Avg = 4.5 + Step 2: Min = 1, Max = 7, Avg = 4 + Step 3: Min = 3, Max = 5, Avg = 4 + + The count of distinct average is 2. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 2, 4, 3, 5, 6], 1, "Example 1"], + [[0, 2, 4, 8, 3, 5], 2, "Example 2"], + [[7, 3, 1, 0, 5, 9], 2, "Example 3"], +]; + +sub distinct_average +{ + my $nums = shift; + + my @nums_sorted = sort { $a <=> $b } @$nums; + my %avgs; + + while(@nums_sorted > 1) { + my $avg = (shift(@nums_sorted) + pop(@nums_sorted)) / 2; + $avgs{$avg}++ + } + return scalar keys %avgs; +} + +for (@$cases) { + is(distinct_average($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-321/peter-meszaros/perl/ch-2.pl b/challenge-321/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d2e5a905f3 --- /dev/null +++ b/challenge-321/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Backspace Compare + +Submitted by: Mohammad Sajid Anwar + +You are given two strings containing zero or more #. + +Write a script to return true if the two given strings are same by treating # +as backspace. + +=head2 Example 1 + + Input: $str1 = "ab#c" + $str2 = "ad#c" + Output: true + + For first string, we remove "b" as it is followed by "#". + For second string, we remove "d" as it is followed by "#". + In the end both strings became the same. + +=head2 Example 2 + + Input: $str1 = "ab##" + $str2 = "a#b#" + Output: true + +=head2 Example 3 + + Input: $str1 = "a#b" + $str2 = "c" + Output: false + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [["ab#c", "ad#c"], 1, "Example 1"], + [["ab##", "a#b#"], 1, "Example 2"], + [["a#b", "c"], 0, "Example 3"], +]; + +sub backspace_compare +{ + my $str1 = $_[0]->[0]; + my $str2 = $_[0]->[1]; + my $regex = qr/[^#]#/; + + while ($str1 =~ s/$regex//) { + } + while ($str2 =~ s/$regex//) { + } + + return $str1 eq $str2 ? 1 : 0; +} + +for (@$cases) { + is(backspace_compare($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-321/peter-meszaros/tcl/ch-1.tcl b/challenge-321/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..6b0e474c4e --- /dev/null +++ b/challenge-321/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,78 @@ +#!/usr/bin/env tclsh +# +# Task 1: Distinct Average +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of numbers with even length. +# +# Write a script to return the count of distinct average. The average is +# calculate by removing the minimum and the maximum, then average of the two. +# +# Example 1 +# +# Input: @nums = (1, 2, 4, 3, 5, 6) +# Output: 1 +# +# Step 1: Min = 1, Max = 6, Avg = 3.5 +# Step 2: Min = 2, Max = 5, Avg = 3.5 +# Step 3: Min = 3, Max = 4, Avg = 3.5 +# +# The count of distinct average is 1. +# +# Example 2 +# +# Input: @nums = (0, 2, 4, 8, 3, 5) +# Output: 2 +# +# Step 1: Min = 0, Max = 8, Avg = 4 +# Step 2: Min = 2, Max = 5, Avg = 3.5 +# Step 3: Min = 3, Max = 4, Avg = 3.5 +# +# The count of distinct average is 2. +# +# Example 3 +# +# Input: @nums = (7, 3, 1, 0, 5, 9) +# Output: 2 +# +# Step 1: Min = 0, Max = 9, Avg = 4.5 +# Step 2: Min = 1, Max = 7, Avg = 4 +# Step 3: Min = 3, Max = 5, Avg = 4 +# +# The count of distinct average is 2. +# + +package require tcltest + +set cases { + {{1 2 4 3 5 6} 1 {Example 1}} + {{0 2 4 8 3 5} 2 {Example 2}} + {{7 3 1 0 5 9} 2 {Example 3}} +} + +proc distinct_average {nums} { + set nums_sorted [lsort -integer -unique $nums] + set avgs {} + + while {[llength $nums_sorted] > 1} { + set min [lindex $nums_sorted 0] + set max [lindex $nums_sorted end] + set nums_sorted [lreplace $nums_sorted 0 0] + set nums_sorted [lreplace $nums_sorted end end] + + set avg [expr ($min + $max) / 2.0] + dict set avgs $avg 1 + } + return [llength [dict keys $avgs]] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + distinct_average [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-321/peter-meszaros/tcl/ch-2.tcl b/challenge-321/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..21fbc59a8f --- /dev/null +++ b/challenge-321/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,65 @@ +#!/usr/bin/env tclsh +# +# Task 2: Backspace Compare +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given two strings containing zero or more #. +# +# Write a script to return true if the two given strings are same by treating # +# as backspace. +# +# Example 1 +# +# Input: $str1 = "ab#c" +# $str2 = "ad#c" +# Output: true +# +# For first string, we remove "b" as it is followed by "#". +# For second string, we remove "d" as it is followed by "#". +# In the end both strings became the same. +# +# Example 2 +# +# Input: $str1 = "ab##" +# $str2 = "a#b#" +# Output: true +# +# Example 3 +# +# Input: $str1 = "a#b" +# $str2 = "c" +# Output: false +# + +package require tcltest + +set cases { + {{ab#c ad#c} 1 {Example 1}} + {{ab## a#b#} 1 {Example 2}} + {{a#b c} 0 {Example 3}} +} + +proc backspace_compare {strs} { + set str1 [lindex $strs 0] + set str2 [lindex $strs 1] + + set regex {[^#]#} + + while {[regsub $regex $str1 {} str1]} { + } + while {[regsub $regex $str2 {} str2]} { + } + + return [expr {$str1 eq $str2}] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + backspace_compare [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
