aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2023-03-15 13:39:19 +0100
committerMariano Spadaccini <spadacciniweb@gmail.com>2023-03-15 13:39:19 +0100
commit08e970df3bcaba7829f31f9de952c4a469966554 (patch)
treef6c9ace92eec393521474360762f7d905097f06b
parent0f4a809c0bead5e3fd8bc4616e1c011c9d6fb8a9 (diff)
downloadperlweeklychallenge-club-08e970df3bcaba7829f31f9de952c4a469966554.tar.gz
perlweeklychallenge-club-08e970df3bcaba7829f31f9de952c4a469966554.tar.bz2
perlweeklychallenge-club-08e970df3bcaba7829f31f9de952c4a469966554.zip
PWC-208: Perl, Python, Ruby, Go
-rw-r--r--challenge-208/spadacciniweb/go/ch-1.go70
-rw-r--r--challenge-208/spadacciniweb/go/ch-2.go70
-rw-r--r--challenge-208/spadacciniweb/perl/ch-1.pl74
-rw-r--r--challenge-208/spadacciniweb/perl/ch-2.pl53
-rw-r--r--challenge-208/spadacciniweb/python/ch-1.py56
-rw-r--r--challenge-208/spadacciniweb/python/ch-2.py40
-rw-r--r--challenge-208/spadacciniweb/ruby/ch-1.rb61
-rw-r--r--challenge-208/spadacciniweb/ruby/ch-2.rb46
8 files changed, 470 insertions, 0 deletions
diff --git a/challenge-208/spadacciniweb/go/ch-1.go b/challenge-208/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..65fc88e734
--- /dev/null
+++ b/challenge-208/spadacciniweb/go/ch-1.go
@@ -0,0 +1,70 @@
+/*
+# Task 1: Minimum Index Sum
+# Submitted by: Mohammad S Anwar
+#
+# You are given two arrays of strings.
+# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
+#
+# Example 1
+# Input: @list1 = ("Perl", "Raku", "Love")
+# @list2 = ("Raku", "Perl", "Hate")
+# Output: ("Perl", "Raku")
+#
+# There are two common strings "Perl" and "Raku".
+# Index sum of "Perl": 0 + 1 = 1
+# Index sum of "Raku": 1 + 0 = 1
+#
+# Example 2
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("D", "E", "F")
+# Output: ()
+#
+# No common string found, so no result.
+#
+# Example 3
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("C", "A", "B")
+# Output: ("A")
+#
+# There are three common strings "A", "B" and "C".
+# Index sum of "A": 0 + 1 = 1
+# Index sum of "B": 1 + 2 = 3
+# Index sum of "C": 2 + 0 = 2
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func get_minimum_index_sum(list1 []string, list2 []string) []string {
+ words := make([]string, 0)
+ var value int = -1
+
+ for i := 0; i <= len(list1)-1; i++ {
+ for j := 0; j <= len(list2)-1; j++ {
+ if value != -1 && value < i + j {
+ break
+ }
+ if list1[i] == list2[j] {
+ if value == -1 || value > i + j {
+ words = nil
+ value = i + j
+ }
+ words = append(words, list1[i])
+ }
+ }
+ }
+ return words
+}
+
+func main() {
+ fmt.Println(get_minimum_index_sum([]string {"Perl", "Raku", "Love"},
+ []string {"Raku", "Perl", "Hate"} ) )
+ fmt.Println(get_minimum_index_sum([]string {"A", "B", "C"},
+ []string {"D", "E", "F"} ) )
+ fmt.Println(get_minimum_index_sum([]string {"A", "B", "C"},
+ []string {"C", "A", "B"} ) )
+}
+
diff --git a/challenge-208/spadacciniweb/go/ch-2.go b/challenge-208/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..c393bff392
--- /dev/null
+++ b/challenge-208/spadacciniweb/go/ch-2.go
@@ -0,0 +1,70 @@
+/*
+# Task 2: Duplicate and Missing
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers in sequence with one missing and one duplicate.
+# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found.
+# For the sake of this task, let us assume the array contains no more than one duplicate and missing.
+#
+# Example 1:
+# Input: @nums = (1,2,2,4)
+# Output: (2,3)
+# Duplicate is 2 and Missing is 3.
+#
+# Example 2:
+# Input: @nums = (1,2,3,4)
+# Output: -1
+# No duplicate and missing found.
+#
+# Example 3:
+# Input: @nums = (1,2,3,3)
+# Output: (3,4)
+# Duplicate is 3 and Missing is 4.
+*/
+
+package main
+
+import (
+ "fmt"
+ "sort"
+)
+
+func skip_1_value(list1 []int, index int) []int {
+ skip_1_value := make([]int, 0)
+ for i, _ := range list1 {
+ if i == index {
+ continue
+ }
+ skip_1_value = append(skip_1_value, list1[i])
+ }
+ return skip_1_value
+}
+
+func duplicate_and_missing(list1 []int) []int {
+ dupl_and_miss := make([]int, 0)
+
+ for i, val := range list1 {
+ numbers := skip_1_value(list1, i)
+ pos := sort.SearchInts(numbers, val)
+ if pos < len(numbers) && numbers[pos] == val { // found
+ dupl_and_miss = append(dupl_and_miss, val)
+ break
+ }
+ }
+ for i := 0; i <= len(list1)-1; i++ {
+ val := list1[0] + i
+ pos := sort.SearchInts(list1, val)
+ if !(pos < len(list1) && list1[pos] == val) { // not found
+ dupl_and_miss = append(dupl_and_miss, val)
+ }
+ }
+ return dupl_and_miss
+}
+
+func main() {
+ fmt.Println(duplicate_and_missing([]int {1,2,2,4} ) )
+ fmt.Println(duplicate_and_missing([]int {1,2,3,4} ) )
+ fmt.Println(duplicate_and_missing([]int {1,2,3,3} ) )
+}
+
+
diff --git a/challenge-208/spadacciniweb/perl/ch-1.pl b/challenge-208/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..dcd2108187
--- /dev/null
+++ b/challenge-208/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+# Task 1: Minimum Index Sum
+# Submitted by: Mohammad S Anwar
+#
+# You are given two arrays of strings.
+# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
+#
+# Example 1
+# Input: @list1 = ("Perl", "Raku", "Love")
+# @list2 = ("Raku", "Perl", "Hate")
+# Output: ("Perl", "Raku")
+#
+# There are two common strings "Perl" and "Raku".
+# Index sum of "Perl": 0 + 1 = 1
+# Index sum of "Raku": 1 + 0 = 1
+#
+# Example 2
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("D", "E", "F")
+# Output: ()
+#
+# No common string found, so no result.
+#
+# Example 3
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("C", "A", "B")
+# Output: ("A")
+#
+# There are three common strings "A", "B" and "C".
+# Index sum of "A": 0 + 1 = 1
+# Index sum of "B": 1 + 2 = 3
+# Index sum of "C": 2 + 0 = 2
+
+use strict;
+use warnings;
+use feature 'say';
+
+say get_minimum_index_sum(["Perl", "Raku", "Love"],
+ ["Raku", "Perl", "Hate"]
+ );
+say get_minimum_index_sum(["A", "B", "C"],
+ ["D", "E", "F"]
+ );
+say get_minimum_index_sum(["A", "B", "C"],
+ ["C", "A", "B"]
+ );
+
+exit 0;
+
+sub get_minimum_index_sum {
+ my ($list1, $list2) = @_;
+
+ my @words;
+ my $value;
+ foreach my $i (0..scalar(@$list1)-1) {
+ foreach my $j (0..scalar(@$list2)-1) {
+ last
+ if defined($value) and $value < $i + $j;
+ if ($list1->[$i] eq $list2->[$j]) {
+ if (!defined($value)
+ or
+ $value > $i+$j
+ ) {
+ @words = ();
+ $value = $i+$j;
+ }
+ push @words, "\"$list1->[$i]\"";
+ last;
+ }
+ }
+ }
+ return sprintf "(%s)", join ', ', @words;
+}
diff --git a/challenge-208/spadacciniweb/perl/ch-2.pl b/challenge-208/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..5b8092c0b9
--- /dev/null
+++ b/challenge-208/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+# Task 2: Duplicate and Missing
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers in sequence with one missing and one duplicate.
+# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found.
+# For the sake of this task, let us assume the array contains no more than one duplicate and missing.
+#
+# Example 1:
+# Input: @nums = (1,2,2,4)
+# Output: (2,3)
+# Duplicate is 2 and Missing is 3.
+#
+# Example 2:
+# Input: @nums = (1,2,3,4)
+# Output: -1
+# No duplicate and missing found.
+#
+# Example 3:
+# Input: @nums = (1,2,3,3)
+# Output: (3,4)
+# Duplicate is 3 and Missing is 4.
+
+use strict;
+use warnings;
+use feature 'say';
+
+say duplicate_and_missing([1,2,2,4]);
+say duplicate_and_missing([1,2,3,4]);
+say duplicate_and_missing([1,2,3,3]);
+
+exit 0;
+
+sub duplicate_and_missing {
+ my $list = shift;
+
+ my @duplicate_or_missing;
+ foreach my $i (0..(scalar @$list)-1) {
+ if (scalar(grep /^$list->[$i]$/, @$list) > 1) {
+ push @duplicate_or_missing, $list->[$i];
+ last;
+ }
+ }
+ foreach my $i (0..(scalar @$list)-1) {
+ my $value = $i+$list->[0];
+ push @duplicate_or_missing, $value
+ unless grep /^$value$/, @$list;
+ }
+ return (@duplicate_or_missing)
+ ? sprintf "(%s)", join ', ', @duplicate_or_missing
+ : sprintf '-1';
+}
diff --git a/challenge-208/spadacciniweb/python/ch-1.py b/challenge-208/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..2bdf122cbe
--- /dev/null
+++ b/challenge-208/spadacciniweb/python/ch-1.py
@@ -0,0 +1,56 @@
+# Task 1: Minimum Index Sum
+# Submitted by: Mohammad S Anwar
+#
+# You are given two arrays of strings.
+# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
+#
+# Example 1
+# Input: @list1 = ("Perl", "Raku", "Love")
+# @list2 = ("Raku", "Perl", "Hate")
+# Output: ("Perl", "Raku")
+#
+# There are two common strings "Perl" and "Raku".
+# Index sum of "Perl": 0 + 1 = 1
+# Index sum of "Raku": 1 + 0 = 1
+#
+# Example 2
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("D", "E", "F")
+# Output: ()
+#
+# No common string found, so no result.
+#
+# Example 3
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("C", "A", "B")
+# Output: ("A")
+#
+# There are three common strings "A", "B" and "C".
+# Index sum of "A": 0 + 1 = 1
+# Index sum of "B": 1 + 2 = 3
+# Index sum of "C": 2 + 0 = 2
+
+def get_minimum_index_sum(list1, list2):
+ words = set()
+ value = None
+ for i in range(0, len(list1)):
+ for j in range(0, len(list2)):
+ if value != None and value < i + j:
+ break
+ if list1[i] == list2[j]:
+ if value == None or value > i+j:
+ words = set()
+ value = i + j
+ words.add('"'+list1[i]+'"')
+ break
+ print("({:s})".format(', '.join(words)))
+
+if __name__ == "__main__":
+ get_minimum_index_sum(["Perl", "Raku", "Love"],
+ ["Raku", "Perl", "Hate"]);
+ get_minimum_index_sum(["A", "B", "C"],
+ ["D", "E", "F"]
+ );
+ get_minimum_index_sum(["A", "B", "C"],
+ ["C", "A", "B"]
+ );
diff --git a/challenge-208/spadacciniweb/python/ch-2.py b/challenge-208/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..1b13deebdf
--- /dev/null
+++ b/challenge-208/spadacciniweb/python/ch-2.py
@@ -0,0 +1,40 @@
+# Task 2: Duplicate and Missing
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers in sequence with one missing and one duplicate.
+# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found.
+# For the sake of this task, let us assume the array contains no more than one duplicate and missing.
+#
+# Example 1:
+# Input: @nums = (1,2,2,4)
+# Output: (2,3)
+# Duplicate is 2 and Missing is 3.
+#
+# Example 2:
+# Input: @nums = (1,2,3,4)
+# Output: -1
+# No duplicate and missing found.
+#
+# Example 3:
+# Input: @nums = (1,2,3,3)
+# Output: (3,4)
+# Duplicate is 3 and Missing is 4.
+
+def duplicate_and_missing(list1):
+ duplicate_and_missing = set()
+ for i in range(0, len(list1)):
+ if len(list(filter(lambda x: list1[i] == x, list1))) > 1:
+ duplicate_and_missing.add(list1[i])
+ break
+ for i in range(list1[0], list1[0]+len(list1)):
+ if len(list(filter(lambda x: i == x, list1))) == 0:
+ duplicate_and_missing.add(i)
+ if len(list(duplicate_and_missing)) > 1:
+ print("({:s})".format(', '.join([str(x) for x in duplicate_and_missing])))
+ else:
+ print('-1')
+
+if __name__ == "__main__":
+ duplicate_and_missing([1,2,2,4])
+ duplicate_and_missing([1,2,3,4])
+ duplicate_and_missing([1,2,3,3])
diff --git a/challenge-208/spadacciniweb/ruby/ch-1.rb b/challenge-208/spadacciniweb/ruby/ch-1.rb
new file mode 100644
index 0000000000..2dfba6aad8
--- /dev/null
+++ b/challenge-208/spadacciniweb/ruby/ch-1.rb
@@ -0,0 +1,61 @@
+# Task 1: Minimum Index Sum
+# Submitted by: Mohammad S Anwar
+#
+# You are given two arrays of strings.
+# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
+#
+# Example 1
+# Input: @list1 = ("Perl", "Raku", "Love")
+# @list2 = ("Raku", "Perl", "Hate")
+# Output: ("Perl", "Raku")
+#
+# There are two common strings "Perl" and "Raku".
+# Index sum of "Perl": 0 + 1 = 1
+# Index sum of "Raku": 1 + 0 = 1
+#
+# Example 2
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("D", "E", "F")
+# Output: ()
+#
+# No common string found, so no result.
+#
+# Example 3
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("C", "A", "B")
+# Output: ("A")
+#
+# There are three common strings "A", "B" and "C".
+# Index sum of "A": 0 + 1 = 1
+# Index sum of "B": 1 + 2 = 3
+# Index sum of "C": 2 + 0 = 2
+
+def get_minimum_index_sum(list1, list2)
+ words = []
+ value = nil
+
+ (0..list1.length-1).each do |i|
+ (0..list2.length-1).each do |j|
+ break if value != nil and value < i + j
+ if list1[i] == list2[j]
+ if value == nil or value > i + j
+ words = []
+ value = i+j
+ end
+ words.append('"' + list1[i] + '"')
+ break
+ end
+ end
+ end
+ return sprintf "(%s)", words.join(', ')
+end
+
+puts get_minimum_index_sum(["Perl", "Raku", "Love"],
+ ["Raku", "Perl", "Hate"]
+ )
+puts get_minimum_index_sum(["A", "B", "C"],
+ ["D", "E", "F"]
+ )
+puts get_minimum_index_sum(["A", "B", "C"],
+ ["C", "A", "B"]
+ )
diff --git a/challenge-208/spadacciniweb/ruby/ch-2.rb b/challenge-208/spadacciniweb/ruby/ch-2.rb
new file mode 100644
index 0000000000..0f1d851ea1
--- /dev/null
+++ b/challenge-208/spadacciniweb/ruby/ch-2.rb
@@ -0,0 +1,46 @@
+# Task 2: Duplicate and Missing
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers in sequence with one missing and one duplicate.
+# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found.
+# For the sake of this task, let us assume the array contains no more than one duplicate and missing.
+#
+# Example 1:
+# Input: @nums = (1,2,2,4)
+# Output: (2,3)
+# Duplicate is 2 and Missing is 3.
+#
+# Example 2:
+# Input: @nums = (1,2,3,4)
+# Output: -1
+# No duplicate and missing found.
+#
+# Example 3:
+# Input: @nums = (1,2,3,3)
+# Output: (3,4)
+# Duplicate is 3 and Missing is 4.
+
+def duplicate_and_missing(list1)
+ dupl_and_miss = []
+ (0..list1.length-1).each do |i|
+ if list1.select { |w| w === list1[i] }.length > 1
+ dupl_and_miss.append list1[i]
+ break
+ end
+ end
+ (0..list1.length-1).each do |i|
+ if not(list1.include? list1[0] + i)
+ dupl_and_miss.append list1[0]+i
+ break
+ end
+ end
+ if dupl_and_miss.length > 1
+ return sprintf '(%s)', dupl_and_miss.join(',')
+ else
+ return -1
+ end
+end
+
+puts duplicate_and_missing([1,2,2,4])
+puts duplicate_and_missing([1,2,3,4])
+puts duplicate_and_missing([1,2,3,3])