aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-08 21:24:09 +0000
committerGitHub <noreply@github.com>2023-03-08 21:24:09 +0000
commitbbd3eedb1d1a2179450a2af48054b874c6719cbe (patch)
tree9e61bbaad409a9746addaf9f9ba3e347d6c2d0c2
parent5c6863b58f6dce5acf6539c9c5e0cd33f0b860c4 (diff)
parentfe7a96b4ba5dc964a27bb8bb683f15dc619c21b6 (diff)
downloadperlweeklychallenge-club-bbd3eedb1d1a2179450a2af48054b874c6719cbe.tar.gz
perlweeklychallenge-club-bbd3eedb1d1a2179450a2af48054b874c6719cbe.tar.bz2
perlweeklychallenge-club-bbd3eedb1d1a2179450a2af48054b874c6719cbe.zip
Merge pull request #7695 from spadacciniweb/PWC-207
PWC-207 - Perl, Python, Ruby, Go
-rw-r--r--challenge-206/spadacciniweb/python/ch-1.py2
-rw-r--r--challenge-207/spadacciniweb/go/ch-1.go51
-rw-r--r--challenge-207/spadacciniweb/go/ch-2.go59
-rw-r--r--challenge-207/spadacciniweb/perl/ch-1.pl35
-rw-r--r--challenge-207/spadacciniweb/perl/ch-2.pl37
-rw-r--r--challenge-207/spadacciniweb/python/ch-1.py30
-rw-r--r--challenge-207/spadacciniweb/python/ch-2.py33
-rw-r--r--challenge-207/spadacciniweb/ruby/ch-1.rb34
-rw-r--r--challenge-207/spadacciniweb/ruby/ch-2.rb39
9 files changed, 319 insertions, 1 deletions
diff --git a/challenge-206/spadacciniweb/python/ch-1.py b/challenge-206/spadacciniweb/python/ch-1.py
index 14d7e34598..ca0893c5cc 100644
--- a/challenge-206/spadacciniweb/python/ch-1.py
+++ b/challenge-206/spadacciniweb/python/ch-1.py
@@ -39,7 +39,7 @@ if __name__ == "__main__":
len(list(filter(lambda x: re.search(r'\d\d:\d\d', x), input))) != len(input)
or
len(list(filter(lambda x: x > '24:00', input))) != 0 ):
- sys.exit("Input error\n")
+ sys.exit("Input error")
minutes = [get_minutes(x) for x in input]
comb = combinations(range(len(input)), 2)
diff --git a/challenge-207/spadacciniweb/go/ch-1.go b/challenge-207/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..4e33648b72
--- /dev/null
+++ b/challenge-207/spadacciniweb/go/ch-1.go
@@ -0,0 +1,51 @@
+/*
+Task 1: Keyboard Word
+Submitted by: Mohammad S Anwar
+
+You are given an array of words.
+Write a script to print all the words in the given array that can be types using alphabet on only one row of the keyboard.
+
+Let us assume the keys are arranged as below:
+Row 1: qwertyuiop
+Row 2: asdfghjkl
+Row 3: zxcvbnm
+
+Example 1
+Input: @words = ("Hello","Alaska","Dad","Peace")
+Output: ("Alaska","Dad")
+
+Example 2
+Input: @array = ("OMG","Bye")
+Output: ()
+*/
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "regexp"
+ "strings"
+)
+
+func main() {
+ arrStr := os.Args[1:]
+
+ aclass := [3]string{"qwertyuiop","asdfghjkl","zxcvbnm"}
+ output := make([]string, 0)
+ for i := 0; i <= len(arrStr)-1; i++ {
+ word := arrStr[i]
+ for j := 0; j <= len(aclass)-1; j++ {
+ match, err := regexp.MatchString(`^[`+aclass[j]+`]+$`, strings.ToLower(word))
+ if err == nil {
+ if match {
+ output = append(output, word)
+ break
+ }
+ } else {
+ fmt.Println("Error:", err)
+ }
+ }
+ }
+ fmt.Println(output)
+}
diff --git a/challenge-207/spadacciniweb/go/ch-2.go b/challenge-207/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..9a6781f199
--- /dev/null
+++ b/challenge-207/spadacciniweb/go/ch-2.go
@@ -0,0 +1,59 @@
+/*
+Task 2: H-Index
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers containing citations a researcher has received for each paper.
+Write a script to compute the researcher’s H-Index. For more information please checkout the wikipedia page.
+
+ The H-Index is the largest number h such that h articles have at least h citations each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the author’s h-index is 3, because the author has three publications with 3 or more citations. However, the author does not have four publications with 4 or more citations.
+
+
+Example 1
+Input: @citations = (10,8,5,4,3)
+Output: 4
+
+Because the 4th publication has 4 citations and the 5th has only 3.
+
+Example 2
+Input: @citations = (25,8,5,3,3)
+Output: 3
+
+The H-Index is 3 because the fourth paper has only 3 citations.
+*/
+
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "sort"
+ "strconv"
+)
+
+func main() {
+ arrStr := os.Args[1:]
+ if (len(arrStr) < 1) {
+ log.Fatal("input error")
+ }
+
+ arrInt := make([]int, 0)
+ for i := 0; i <= len(arrStr)-1; i++ {
+ value, err := strconv.Atoi(arrStr[i])
+ if (err != nil) {
+ log.Fatal(err)
+ }
+ arrInt = append(arrInt, value)
+ }
+
+ sort.Sort(sort.Reverse(sort.IntSlice(arrInt)))
+
+ hIndex := make([]int, 0)
+ for i := 0; i <= len(arrInt)-1; i++ {
+ if arrInt[i] >= i+1 {
+ hIndex = append(hIndex, arrInt[i])
+ }
+ }
+
+ fmt.Printf("Output: %d\n",len(hIndex))
+}
diff --git a/challenge-207/spadacciniweb/perl/ch-1.pl b/challenge-207/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..4c91e2ed67
--- /dev/null
+++ b/challenge-207/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+# Task 1: Keyboard Word
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of words.
+# Write a script to print all the words in the given array that can be types using alphabet on only one row of the keyboard.
+#
+# Let us assume the keys are arranged as below:
+# Row 1: qwertyuiop
+# Row 2: asdfghjkl
+# Row 3: zxcvbnm
+#
+# Example 1
+# Input: @words = ("Hello","Alaska","Dad","Peace")
+# Output: ("Alaska","Dad")
+#
+# Example 2
+# Input: @array = ("OMG","Bye")
+# Output: ()
+
+use strict;
+use warnings;
+
+my @class = qw/qwertyuiop asdfghjkl zxcvbnm/;
+
+my @output;
+
+foreach my $word (@ARGV) {
+ push @output, sprintf '"%s"', $word
+ if scalar( map { $word =~ /^[$_]+$/i ? 1 : () }
+ @class );
+}
+
+printf "Output: (%s)\n", join ', ', @output;
diff --git a/challenge-207/spadacciniweb/perl/ch-2.pl b/challenge-207/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..a9f11c4310
--- /dev/null
+++ b/challenge-207/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+# Task 2: H-Index
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers containing citations a researcher has received for each paper.
+# Write a script to compute the researcher’s H-Index. For more information please checkout the wikipedia page.
+#
+# The H-Index is the largest number h such that h articles have at least h citations each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the author’s h-index is 3, because the author has three publications with 3 or more citations. However, the author does not have four publications with 4 or more citations.
+#
+#
+# Example 1
+# Input: @citations = (10,8,5,4,3)
+# Output: 4
+#
+# Because the 4th publication has 4 citations and the 5th has only 3.
+#
+# Example 2
+# Input: @citations = (25,8,5,3,3)
+# Output: 3
+#
+# The H-Index is 3 because the fourth paper has only 3 citations.
+
+use strict;
+use warnings;
+
+my @input = @ARGV;
+die "Input error\n"
+ if scalar @input < 1
+ or
+ (scalar map { $_ =~ /\D/ ? 1 : () }
+ @input) != 0;
+
+@input = sort { $b <=> $a } map {int $_}
+ @input;
+printf "Output: %d\n", scalar map { $input[$_] >= $_+1 ? 1 : () }
+ 0..$#input;
diff --git a/challenge-207/spadacciniweb/python/ch-1.py b/challenge-207/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..6beba936e1
--- /dev/null
+++ b/challenge-207/spadacciniweb/python/ch-1.py
@@ -0,0 +1,30 @@
+# Task 1: Keyboard Word
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of words.
+# Write a script to print all the words in the given array that can be types using alphabet on only one row of the keyboard.
+#
+# Let us assume the keys are arranged as below:
+# Row 1: qwertyuiop
+# Row 2: asdfghjkl
+# Row 3: zxcvbnm
+#
+# Example 1
+# Input: @words = ("Hello","Alaska","Dad","Peace")
+# Output: ("Alaska","Dad")
+#
+# Example 2
+# Input: @array = ("OMG","Bye")
+# Output: ()
+
+import re
+import sys
+
+if __name__ == "__main__":
+ aclass = {'qwertyuiop', 'asdfghjkl', 'zxcvbnm'}
+
+ output = set()
+ for word in sys.argv[1:]:
+ if len(list(filter(lambda x: re.search(r'^['+x+']+$', word.lower()), aclass))):
+ output.add(word)
+ print("Output: ({:s})".format(', '.join(output)))
diff --git a/challenge-207/spadacciniweb/python/ch-2.py b/challenge-207/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..8f83d5ff81
--- /dev/null
+++ b/challenge-207/spadacciniweb/python/ch-2.py
@@ -0,0 +1,33 @@
+# Task 2: H-Index
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers containing citations a researcher has received for each paper.
+# Write a script to compute the researcher’s H-Index. For more information please checkout the wikipedia page.
+#
+# The H-Index is the largest number h such that h articles have at least h citations each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the author’s h-index is 3, because the author has three publications with 3 or more citations. However, the author does not have four publications with 4 or more citations.
+#
+#
+# Example 1
+# Input: @citations = (10,8,5,4,3)
+# Output: 4
+#
+# Because the 4th publication has 4 citations and the 5th has only 3.
+#
+# Example 2
+# Input: @citations = (25,8,5,3,3)
+# Output: 3
+#
+# The H-Index is 3 because the fourth paper has only 3 citations.
+
+import re
+import sys
+
+if __name__ == "__main__":
+ input = sys.argv[1:]
+ if (len(input) < 1
+ or
+ len(list(filter(lambda x: re.search(r'\D', x), input))) > 0):
+ sys.exit("Input error")
+
+ input = sorted(list(map(int, input)), reverse=True)
+ print("Output: {:d}".format(len([i for i in range(len(input)) if input[i] >= i+1])))
diff --git a/challenge-207/spadacciniweb/ruby/ch-1.rb b/challenge-207/spadacciniweb/ruby/ch-1.rb
new file mode 100644
index 0000000000..c2409d1c8c
--- /dev/null
+++ b/challenge-207/spadacciniweb/ruby/ch-1.rb
@@ -0,0 +1,34 @@
+# Task 1: Keyboard Word
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of words.
+# Write a script to print all the words in the given array that can be types using alphabet on only one row of the keyboard.
+#
+# Let us assume the keys are arranged as below:
+# Row 1: qwertyuiop
+# Row 2: asdfghjkl
+# Row 3: zxcvbnm
+#
+# Example 1
+# Input: @words = ("Hello","Alaska","Dad","Peace")
+# Output: ("Alaska","Dad")
+#
+# Example 2
+# Input: @array = ("OMG","Bye")
+# Output: ()
+
+def main(input)
+ a_class = ['qwertyuiop','asdfghjkl','zxcvbnm']
+ output = []
+ input.each {|word|
+ a_class.each {|re|
+ if word.match?(/^[#{re}]+$/i)
+ output.append(word)
+ break
+ end
+ }
+ }
+ printf "Output: (%s)\n", output.join(', ')
+end
+
+main ARGV
diff --git a/challenge-207/spadacciniweb/ruby/ch-2.rb b/challenge-207/spadacciniweb/ruby/ch-2.rb
new file mode 100644
index 0000000000..e0c3eeec9f
--- /dev/null
+++ b/challenge-207/spadacciniweb/ruby/ch-2.rb
@@ -0,0 +1,39 @@
+# Task 2: H-Index
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers containing citations a researcher has received for each paper.
+# Write a script to compute the researcher’s H-Index. For more information please checkout the wikipedia page.
+#
+# The H-Index is the largest number h such that h articles have at least h citations each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the author’s h-index is 3, because the author has three publications with 3 or more citations. However, the author does not have four publications with 4 or more citations.
+#
+#
+# Example 1
+# Input: @citations = (10,8,5,4,3)
+# Output: 4
+#
+# Because the 4th publication has 4 citations and the 5th has only 3.
+#
+# Example 2
+# Input: @citations = (25,8,5,3,3)
+# Output: 3
+#
+# The H-Index is 3 because the fourth paper has only 3 citations.
+
+def main(input)
+ if input.length < 1 \
+ or \
+ input.select{ |i| i[/\D/] }.length > 0
+ then
+ puts "Input error"
+ exit(0)
+ end
+ output = []
+ input.map(&:to_i).sort.reverse.each_with_index { |element, index|
+ if element >= index+1
+ output.append(element)
+ end
+ }
+ printf "Output: %d\n", output.length
+end
+
+main ARGV