diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-26 10:27:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-26 10:27:06 +0100 |
| commit | 333da2d0d782d7c49904a79fc62390268fc50dda (patch) | |
| tree | 8831f3b453fad7fb0dac4ffbde42f1ee6c650c53 | |
| parent | c51aa68f0342fc920e291f211a882fd9be05130b (diff) | |
| parent | cd6ceb5b39df5b963dba47c245b98d6c594a8131 (diff) | |
| download | perlweeklychallenge-club-333da2d0d782d7c49904a79fc62390268fc50dda.tar.gz perlweeklychallenge-club-333da2d0d782d7c49904a79fc62390268fc50dda.tar.bz2 perlweeklychallenge-club-333da2d0d782d7c49904a79fc62390268fc50dda.zip | |
Merge pull request #12737 from spadacciniweb/PWC-340
PWC 340
| -rw-r--r-- | challenge-340/spadacciniweb/perl/ch-1.pl | 81 | ||||
| -rw-r--r-- | challenge-340/spadacciniweb/perl/ch-2.pl | 70 | ||||
| -rw-r--r-- | challenge-340/spadacciniweb/python/ch-1.py | 74 | ||||
| -rw-r--r-- | challenge-340/spadacciniweb/python/ch-2.py | 62 | ||||
| -rw-r--r-- | challenge-340/spadacciniweb/ruby/ch-1.rb | 74 | ||||
| -rw-r--r-- | challenge-340/spadacciniweb/ruby/ch-2.rb | 61 |
6 files changed, 422 insertions, 0 deletions
diff --git a/challenge-340/spadacciniweb/perl/ch-1.pl b/challenge-340/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..c98cbdf9fa --- /dev/null +++ b/challenge-340/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl + +# Task 1: Duplicate Removals +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, consisting of lowercase English letters. +# +# Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can. +# +# A duplicate removal consists of choosing two adjacent and equal letters and removing them. +# +# Example 1 +# Input: $str = 'abbaca' +# Output: 'ca' +# +# Step 1: Remove 'bb' => 'aaca' +# Step 2: Remove 'aa' => 'ca' +# +# Example 2 +# Input: $str = 'azxxzy' +# Output: 'ay' +# +# Step 1: Remove 'xx' => 'azzy' +# Step 2: Remove 'zz' => 'ay' +# +# Example 3 +# Input: $str = 'aaaaaaaa' +# Output: '' +# +# Step 1: Remove 'aa' => 'aaaaaa' +# Step 2: Remove 'aa' => 'aaaa' +# Step 3: Remove 'aa' => 'aa' +# Step 4: Remove 'aa' => '' +# +# Example 4 +# Input: $str = 'aabccba' +# Output: 'a' +# +# Step 1: Remove 'aa' => 'bccba' +# Step 2: Remove 'cc' => 'bba' +# Step 3: Remove 'bb' => 'a' +# +# Example 5 +# Input: $str = 'abcddcba' +# Output: '' +# +# Step 1: Remove 'dd' => 'abccba' +# Step 2: Remove 'cc' => 'abba' +# Step 3: Remove 'bb' => 'aa' +# Step 4: Remove 'aa' => '' + +use strict; +use warnings; + +my $str = 'abbaca'; +get_noduplicate($str); + +$str = 'azxxzy'; +get_noduplicate($str); + +$str = 'aaaaaaaa'; +get_noduplicate($str); + +$str = 'aabccba'; +get_noduplicate($str); + +$str = 'abcddcba'; +get_noduplicate($str); + +exit 0; + +sub get_noduplicate { + my $str_orig = shift; + my $str = $str_orig; + + do { + $str =~ s/(\w)\1//; + } while $str =~ /(\w)\1/; + + printf "'%s' -> '%s'\n", $str_orig, $str; +} diff --git a/challenge-340/spadacciniweb/perl/ch-2.pl b/challenge-340/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..3a302e6197 --- /dev/null +++ b/challenge-340/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +# Task 2: Ascending Numbers +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, is a list of tokens separated by a single space. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters. +# Write a script to check if all the numbers in the given string are strictly increasing from left to right. +# +# Example 1 +# Input: $str = "The cat has 3 kittens 7 toys 10 beds" +# Output: true +# +# Numbers 3, 7, 10 - strictly increasing. +# +# Example 2 +# Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas' +# Output: false +# +# Example 3 +# Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months' +# Output: true +# +# Example 4 +# Input: $str = 'Bob has 10 cars 10 bikes' +# Output: false +# +# Example 5 +# Input: $str = 'Zero is 0 one is 1 two is 2' +# Output: true + +use strict; +use warnings; + +my $str = "The cat has 3 kittens 7 toys 10 beds"; +ascending_numbers( $str ); + +$str = 'Alice bought 5 apples 2 oranges 9 bananas'; +ascending_numbers( $str ); + +$str = 'I ran 1 mile 2 days 3 weeks 4 months'; +ascending_numbers( $str ); + +$str = 'Bob has 10 cars 10 bikes'; +ascending_numbers( $str ); + +$str = 'Zero is 0 one is 1 two is 2'; +ascending_numbers( $str ); + +exit 0; + +sub ascending_numbers { + my $str = shift; + + my @numbers = ($str =~ /\d+/g); + my $sorted = 1; + if (scalar @numbers > 0) { + my $curr = shift @numbers; + foreach $_ (@numbers) { + if ($curr >= $_) { + $sorted = 0; + last; + } + $curr = $_ + } + } + + printf "'%s' -> %s\n", $str, $sorted + ? 'true' + : 'false'; +} diff --git a/challenge-340/spadacciniweb/python/ch-1.py b/challenge-340/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..a6bcad20be --- /dev/null +++ b/challenge-340/spadacciniweb/python/ch-1.py @@ -0,0 +1,74 @@ +# Task 1: Duplicate Removals +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, consisting of lowercase English letters. +# +# Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can. +# +# A duplicate removal consists of choosing two adjacent and equal letters and removing them. +# +# Example 1 +# Input: $str = 'abbaca' +# Output: 'ca' +# +# Step 1: Remove 'bb' => 'aaca' +# Step 2: Remove 'aa' => 'ca' +# +# Example 2 +# Input: $str = 'azxxzy' +# Output: 'ay' +# +# Step 1: Remove 'xx' => 'azzy' +# Step 2: Remove 'zz' => 'ay' +# +# Example 3 +# Input: $str = 'aaaaaaaa' +# Output: '' +# +# Step 1: Remove 'aa' => 'aaaaaa' +# Step 2: Remove 'aa' => 'aaaa' +# Step 3: Remove 'aa' => 'aa' +# Step 4: Remove 'aa' => '' +# +# Example 4 +# Input: $str = 'aabccba' +# Output: 'a' +# +# Step 1: Remove 'aa' => 'bccba' +# Step 2: Remove 'cc' => 'bba' +# Step 3: Remove 'bb' => 'a' +# +# Example 5 +# Input: $str = 'abcddcba' +# Output: '' +# +# Step 1: Remove 'dd' => 'abccba' +# Step 2: Remove 'cc' => 'abba' +# Step 3: Remove 'bb' => 'aa' +# Step 4: Remove 'aa' => '' + +import re + +def get_noduplicate(str): + str_orig = str + + while re.search(r'([a-z])\1', str): + str = re.sub(r'([a-z])\1', r'', str) + + print("'%s' -> '%s'" % ( str_orig, str ) ) + +if __name__ == "__main__": + str = 'abbaca' + get_noduplicate(str) + + str = 'azxxzy' + get_noduplicate(str) + + str = 'aaaaaaaa' + get_noduplicate(str) + + str = 'aabccba' + get_noduplicate(str) + + str = 'abcddcba' + get_noduplicate(str) diff --git a/challenge-340/spadacciniweb/python/ch-2.py b/challenge-340/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..061f8199e2 --- /dev/null +++ b/challenge-340/spadacciniweb/python/ch-2.py @@ -0,0 +1,62 @@ +# Task 2: Ascending Numbers +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, is a list of tokens separated by a single space. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters. +# Write a script to check if all the numbers in the given string are strictly increasing from left to right. +# +# Example 1 +# Input: $str = "The cat has 3 kittens 7 toys 10 beds" +# Output: true +# +# Numbers 3, 7, 10 - strictly increasing. +# +# Example 2 +# Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas' +# Output: false +# +# Example 3 +# Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months' +# Output: true +# +# Example 4 +# Input: $str = 'Bob has 10 cars 10 bikes' +# Output: false +# +# Example 5 +# Input: $str = 'Zero is 0 one is 1 two is 2' +# Output: true + + +# printf "'%s' -> %s\n", $str, $sorted +# ? 'true' +import re + +def ascending_numbers(str): + numbers = list(map(int, re.findall(r'\d+', str) )) + + sorted = True + if (len(numbers) > 0): + curr = numbers.pop(0) + for i in numbers: + if (curr >= i): + sorted = False + break + curr = i + + print("'%s' -> %s" % ( str, sorted ) ) + +if __name__ == "__main__": + str = "The cat has 3 kittens 7 toys 10 beds" + ascending_numbers( str ) + + str = 'Alice bought 5 apples 2 oranges 9 bananas' + ascending_numbers( str ) + + str = 'I ran 1 mile 2 days 3 weeks 4 months' + ascending_numbers( str ) + + str = 'Bob has 10 cars 10 bikes' + ascending_numbers( str ) + + str = 'Zero is 0 one is 1 two is 2' + ascending_numbers( str ) diff --git a/challenge-340/spadacciniweb/ruby/ch-1.rb b/challenge-340/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..e290f176d7 --- /dev/null +++ b/challenge-340/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,74 @@ +# Task 1: Duplicate Removals +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, consisting of lowercase English letters. +# +# Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can. +# +# A duplicate removal consists of choosing two adjacent and equal letters and removing them. +# +# Example 1 +# Input: $str = 'abbaca' +# Output: 'ca' +# +# Step 1: Remove 'bb' => 'aaca' +# Step 2: Remove 'aa' => 'ca' +# +# Example 2 +# Input: $str = 'azxxzy' +# Output: 'ay' +# +# Step 1: Remove 'xx' => 'azzy' +# Step 2: Remove 'zz' => 'ay' +# +# Example 3 +# Input: $str = 'aaaaaaaa' +# Output: '' +# +# Step 1: Remove 'aa' => 'aaaaaa' +# Step 2: Remove 'aa' => 'aaaa' +# Step 3: Remove 'aa' => 'aa' +# Step 4: Remove 'aa' => '' +# +# Example 4 +# Input: $str = 'aabccba' +# Output: 'a' +# +# Step 1: Remove 'aa' => 'bccba' +# Step 2: Remove 'cc' => 'bba' +# Step 3: Remove 'bb' => 'a' +# +# Example 5 +# Input: $str = 'abcddcba' +# Output: '' +# +# Step 1: Remove 'dd' => 'abccba' +# Step 2: Remove 'cc' => 'abba' +# Step 3: Remove 'bb' => 'aa' +# Step 4: Remove 'aa' => '' + +def get_noduplicate(str) + str_orig = str.dup + + while str =~ /(\w)\1/ + str.gsub!(/(\w)\1/, '') + end + + printf "'%s' -> '%s'\n", str_orig, str; +end + +str = 'abbaca' +get_noduplicate(str) + +str = 'azxxzy' +get_noduplicate(str) + +str = 'aaaaaaaa' +get_noduplicate(str) + +str = 'aabccba' +get_noduplicate(str) + +str = 'abcddcba' +get_noduplicate(str) + diff --git a/challenge-340/spadacciniweb/ruby/ch-2.rb b/challenge-340/spadacciniweb/ruby/ch-2.rb new file mode 100644 index 0000000000..faa6c18a62 --- /dev/null +++ b/challenge-340/spadacciniweb/ruby/ch-2.rb @@ -0,0 +1,61 @@ +# Task 2: Ascending Numbers +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, is a list of tokens separated by a single space. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters. +# Write a script to check if all the numbers in the given string are strictly increasing from left to right. +# +# Example 1 +# Input: $str = "The cat has 3 kittens 7 toys 10 beds" +# Output: true +# +# Numbers 3, 7, 10 - strictly increasing. +# +# Example 2 +# Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas' +# Output: false +# +# Example 3 +# Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months' +# Output: true +# +# Example 4 +# Input: $str = 'Bob has 10 cars 10 bikes' +# Output: false +# +# Example 5 +# Input: $str = 'Zero is 0 one is 1 two is 2' +# Output: true + +def ascending_numbers(str) + numbers = str.scan(/\d+/).map(&:to_i) + sorted = true + if (numbers.size > 0) + curr = numbers.shift + numbers.each { |i| + if (curr >= i) + print(curr, ' ', i, ' ') + sorted = false + break + end + curr = i + } + end + + printf "'%s' -> %s\n", str, sorted + +end + +str = "The cat has 3 kittens 7 toys 10 beds" +ascending_numbers( str ) + +str = 'Alice bought 5 apples 2 oranges 9 bananas' +ascending_numbers( str ) + +str = 'I ran 1 mile 2 days 3 weeks 4 months' +ascending_numbers( str ) + +str = 'Bob has 10 cars 10 bikes' +ascending_numbers( str ) + +str = 'Zero is 0 one is 1 two is 2' +ascending_numbers( str ) |
