From 7c197d26437000c27a381b041de5bcade51e8111 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 22 Sep 2025 11:18:54 +0200 Subject: Add ch-1 in Perl --- challenge-340/spadacciniweb/perl/ch-1.pl | 82 ++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 challenge-340/spadacciniweb/perl/ch-1.pl diff --git a/challenge-340/spadacciniweb/perl/ch-1.pl b/challenge-340/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..36296dbebd --- /dev/null +++ b/challenge-340/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!/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; +} -- cgit From c415df80f8ee986bce4bbcb1533306841ad3cd7f Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 22 Sep 2025 11:42:03 +0200 Subject: Add ch-2 and cleanup ch-1 in Perl --- challenge-340/spadacciniweb/perl/ch-1.pl | 1 - challenge-340/spadacciniweb/perl/ch-2.pl | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 challenge-340/spadacciniweb/perl/ch-2.pl diff --git a/challenge-340/spadacciniweb/perl/ch-1.pl b/challenge-340/spadacciniweb/perl/ch-1.pl index 36296dbebd..c98cbdf9fa 100644 --- a/challenge-340/spadacciniweb/perl/ch-1.pl +++ b/challenge-340/spadacciniweb/perl/ch-1.pl @@ -75,7 +75,6 @@ sub get_noduplicate { 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..e22e5c7e24 --- /dev/null +++ b/challenge-340/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/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; + } + } + } + + printf "'%s' -> %s\n", $str, $sorted + ? 'true' + : 'false'; +} -- cgit From fbf2a1b7a246de38ca0a2e74d3bb6982d2984120 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 22 Sep 2025 12:45:35 +0200 Subject: Add ch-1 in Python --- challenge-340/spadacciniweb/python/ch-1.py | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 challenge-340/spadacciniweb/python/ch-1.py 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) -- cgit From 73b7940b487e99e182d406a38bad8362cfa194dd Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 22 Sep 2025 13:04:29 +0200 Subject: Add ch-2 in Python --- challenge-340/spadacciniweb/python/ch-2.py | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 challenge-340/spadacciniweb/python/ch-2.py diff --git a/challenge-340/spadacciniweb/python/ch-2.py b/challenge-340/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..2aa0256c51 --- /dev/null +++ b/challenge-340/spadacciniweb/python/ch-2.py @@ -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 + + +# printf "'%s' -> %s\n", $str, $sorted +# ? 'true' +import re + +def ascending_numbers(str): + numbers = 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 + + 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 ) -- cgit From 4002bf0b92e24443ddc2099bed2608bc7af74011 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 22 Sep 2025 13:24:56 +0200 Subject: Add ch-1 in Ruby --- challenge-340/spadacciniweb/ruby/ch-1.rb | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 challenge-340/spadacciniweb/ruby/ch-1.rb 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) + -- cgit From d69299bd49ea4de04cfeae86a5d20d39dee0c785 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 22 Sep 2025 14:12:56 +0200 Subject: Cleanup some ch-2 --- challenge-340/spadacciniweb/perl/ch-2.pl | 1 + challenge-340/spadacciniweb/python/ch-2.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/challenge-340/spadacciniweb/perl/ch-2.pl b/challenge-340/spadacciniweb/perl/ch-2.pl index e22e5c7e24..3a302e6197 100644 --- a/challenge-340/spadacciniweb/perl/ch-2.pl +++ b/challenge-340/spadacciniweb/perl/ch-2.pl @@ -60,6 +60,7 @@ sub ascending_numbers { $sorted = 0; last; } + $curr = $_ } } diff --git a/challenge-340/spadacciniweb/python/ch-2.py b/challenge-340/spadacciniweb/python/ch-2.py index 2aa0256c51..061f8199e2 100644 --- a/challenge-340/spadacciniweb/python/ch-2.py +++ b/challenge-340/spadacciniweb/python/ch-2.py @@ -32,7 +32,7 @@ import re def ascending_numbers(str): - numbers = re.findall(r'\d+', str) + numbers = list(map(int, re.findall(r'\d+', str) )) sorted = True if (len(numbers) > 0): @@ -41,6 +41,7 @@ def ascending_numbers(str): if (curr >= i): sorted = False break + curr = i print("'%s' -> %s" % ( str, sorted ) ) -- cgit From cd6ceb5b39df5b963dba47c245b98d6c594a8131 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Wed, 24 Sep 2025 10:51:54 +0200 Subject: Add ch-2 in Ruby --- challenge-340/spadacciniweb/ruby/ch-2.rb | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 challenge-340/spadacciniweb/ruby/ch-2.rb 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 ) -- cgit