aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-11 20:58:53 +0100
committerGitHub <noreply@github.com>2023-04-11 20:58:53 +0100
commit3ff31cdd319438d45c44ea89e7f6f014ca0e97cc (patch)
treec730d424ea905ea61a59cf6c1968ad1371fda2f3
parent63be90d4133a954543a79193b7e4dc8f07cd427e (diff)
parentbb2cf0104d8ae09e93b8ab7621b8c400640311e3 (diff)
downloadperlweeklychallenge-club-3ff31cdd319438d45c44ea89e7f6f014ca0e97cc.tar.gz
perlweeklychallenge-club-3ff31cdd319438d45c44ea89e7f6f014ca0e97cc.tar.bz2
perlweeklychallenge-club-3ff31cdd319438d45c44ea89e7f6f014ca0e97cc.zip
Merge pull request #7891 from spadacciniweb/PWC-212
PWC-212 (and PWC-211 fix)
-rw-r--r--challenge-211/spadacciniweb/go/ch-1.go13
-rw-r--r--challenge-211/spadacciniweb/perl/ch-1.pl30
-rw-r--r--challenge-211/spadacciniweb/python/ch-1.py10
-rw-r--r--challenge-211/spadacciniweb/ruby/ch-1.rb9
-rw-r--r--challenge-212/spadacciniweb/go/ch-1.go49
-rw-r--r--challenge-212/spadacciniweb/perl/ch-1.pl48
-rw-r--r--challenge-212/spadacciniweb/python/ch-1.py32
-rw-r--r--challenge-212/spadacciniweb/ruby/ch-1.rb44
8 files changed, 202 insertions, 33 deletions
diff --git a/challenge-211/spadacciniweb/go/ch-1.go b/challenge-211/spadacciniweb/go/ch-1.go
index 97d6d47354..b19067b0e7 100644
--- a/challenge-211/spadacciniweb/go/ch-1.go
+++ b/challenge-211/spadacciniweb/go/ch-1.go
@@ -30,14 +30,11 @@ import (
func check_toeplitz(m [][]int) bool {
rows := len(m)
cols := len(m[0])
- var dimens int = rows
- if (cols < rows) {
- dimens = cols
- }
- val := m[0][0]
- for i := 0; i < dimens; i++ {
- if m[i][i] != val {
- return false
+ for i := 1; i < rows; i++ {
+ for j := 1; j < cols; j++ {
+ if m[i][j] != m[i-1][j-1] {
+ return false
+ }
}
}
return true
diff --git a/challenge-211/spadacciniweb/perl/ch-1.pl b/challenge-211/spadacciniweb/perl/ch-1.pl
index 78e264778b..23550611f8 100644
--- a/challenge-211/spadacciniweb/perl/ch-1.pl
+++ b/challenge-211/spadacciniweb/perl/ch-1.pl
@@ -26,28 +26,26 @@ use warnings;
use feature qw/say/;
sub check_toeplitz {
- my $matrix = shift;
- my $rows = @$matrix;
- my $cols = scalar @{$matrix->[0]};
- my $min = ($rows < $cols)
- ? $rows
- : $cols;
- my $value = $matrix->[0]->[0];
- foreach my $i (0..$min-1) {
- return 'false'
- unless $matrix->[$i]->[$i] == $value;
+ my @matrix = @_;
+ my $rows = scalar @matrix;
+ my $cols = scalar @{$matrix[0]};
+ foreach my $i (1..($rows-1)) {
+ foreach my $j (1..($cols-1)) {
+ return 'false'
+ unless $matrix[$i][$j] == $matrix[$i-1][$j-1];
+ }
}
return 'true';
}
-my $matrix = [ [4, 3, 2, 1],
+my @matrix = ( [4, 3, 2, 1],
[5, 4, 3, 2],
[6, 5, 4, 3],
- ];
-say check_toeplitz($matrix);
+ );
+say check_toeplitz(@matrix);
-$matrix = [ [1, 2, 3],
+@matrix = ( [1, 2, 3],
[3, 2, 1],
- ];
-say check_toeplitz($matrix);
+ );
+say check_toeplitz(@matrix);
diff --git a/challenge-211/spadacciniweb/python/ch-1.py b/challenge-211/spadacciniweb/python/ch-1.py
index 00f663b9f1..985e105c76 100644
--- a/challenge-211/spadacciniweb/python/ch-1.py
+++ b/challenge-211/spadacciniweb/python/ch-1.py
@@ -23,11 +23,11 @@ import numpy
def check_toeplitz(matrix):
m = numpy.array(matrix)
- dim = min(m.shape)
- value = m[0][0]
- for i in range(dim):
- if m[i][i] != value:
- return 'false'
+ rows, cols = m.shape
+ for i in range(1,rows):
+ for j in range(1,cols):
+ if m[i][j] != m[i-1][j-1]:
+ return 'false'
return 'true'
if __name__ == "__main__":
diff --git a/challenge-211/spadacciniweb/ruby/ch-1.rb b/challenge-211/spadacciniweb/ruby/ch-1.rb
index 03b84d9ff3..c24d762b7a 100644
--- a/challenge-211/spadacciniweb/ruby/ch-1.rb
+++ b/challenge-211/spadacciniweb/ruby/ch-1.rb
@@ -20,10 +20,11 @@
# Output: false
def check_toeplitz(m)
- rc = [m.length, m[0].length].min - 1
- val = m[0][0]
- (0..rc).each {|e|
- return 'false' if m[e][e] != val
+ rows, cols = m.length, m[0].length
+ (1...rows).each { |i|
+ (1...cols).each { |j|
+ return 'false' if m[i][j] != m[i-1][j-1]
+ }
}
return 'true'
end
diff --git a/challenge-212/spadacciniweb/go/ch-1.go b/challenge-212/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..b5ee438b1e
--- /dev/null
+++ b/challenge-212/spadacciniweb/go/ch-1.go
@@ -0,0 +1,49 @@
+/*
+Task 1: Jumping Letters
+Submitted by: Mohammad S Anwar
+
+You are given a word having alphabetic characters only, and a list of positive integers of the same length
+
+Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.
+
+Example 1
+Input: $word = 'Perl' and @jump = (2,22,19,9)
+Output: Raku
+'P' jumps 2 place forward and becomes 'R'.
+'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a')
+'r' jumps 19 place forward and becomes 'k'.
+'l' jumps 9 place forward and becomes 'u'.
+
+Example 2
+Input: $word = 'Raku' and @jump = (24,4,7,17)
+Output: 'Perl'
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func tr_letter(c byte, jump int) string {
+ ord := int(c)
+ //return rune(ord)
+ if (ord >= 97) {
+ return string( ( ord - 97 + jump ) % 26 + 97 )
+ } else {
+ return string( ( ord - 65 + jump ) % 26 + 65 )
+ }
+}
+
+func tr_word(word string, jump []int) string {
+ new_word := ""
+ for i := 0; i <= len(word)-1; i++ {
+ new_word = new_word + tr_letter(byte(word[i]), jump[i])
+ }
+ return new_word
+}
+
+func main() {
+ fmt.Println(tr_word("Perl", []int{2,22,19,9}))
+ fmt.Println(tr_word("Raku", []int{24,4,7,17}))
+}
diff --git a/challenge-212/spadacciniweb/perl/ch-1.pl b/challenge-212/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..237d362ecb
--- /dev/null
+++ b/challenge-212/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+# Task 1: Jumping Letters
+# Submitted by: Mohammad S Anwar
+#
+# You are given a word having alphabetic characters only, and a list of positive integers of the same length
+#
+# Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.
+#
+# Example 1
+# Input: $word = 'Perl' and @jump = (2,22,19,9)
+# Output: Raku
+# 'P' jumps 2 place forward and becomes 'R'.
+# 'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a')
+# 'r' jumps 19 place forward and becomes 'k'.
+# 'l' jumps 9 place forward and becomes 'u'.
+
+# Example 2
+# Input: $word = 'Raku' and @jump = (24,4,7,17)
+# Output: 'Perl'
+
+use strict;
+use warnings;
+use feature qw/say/;
+
+sub tr_letter {
+ my $ord = ord shift;
+ return ($ord >= 97)
+ ? chr ( ( $ord - 97 + shift ) % 26 + 97 )
+ : chr ( ( $ord - 65 + shift ) % 26 + 65 );
+}
+
+sub tr_word {
+ my $word = shift;
+ my $jump = shift;
+ return join '',
+ map { tr_letter [split '', $word]->[$_], $jump->[$_] }
+ 0..(length $word)-1;
+}
+
+my $word = 'Perl';
+my @jump = (2,22,19,9);
+
+say tr_word($word, \@jump);
+
+$word = 'Raku';
+@jump = (24,4,7,17);
+say tr_word($word, \@jump);
diff --git a/challenge-212/spadacciniweb/python/ch-1.py b/challenge-212/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..517aa06136
--- /dev/null
+++ b/challenge-212/spadacciniweb/python/ch-1.py
@@ -0,0 +1,32 @@
+# Task 1: Jumping Letters
+# Submitted by: Mohammad S Anwar
+#
+# You are given a word having alphabetic characters only, and a list of positive integers of the same length
+#
+# Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.
+#
+# Example 1
+# Input: $word = 'Perl' and @jump = (2,22,19,9)
+# Output: Raku
+# 'P' jumps 2 place forward and becomes 'R'.
+# 'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a')
+# 'r' jumps 19 place forward and becomes 'k'.
+# 'l' jumps 9 place forward and becomes 'u'.
+
+# Example 2
+# Input: $word = 'Raku' and @jump = (24,4,7,17)
+# Output: 'Perl'
+
+tr_letter = lambda x, y: chr((ord(x) - 97 + y) % 26 + 97) if (ord(x) >= 97) else chr((ord(x) - 65 + y) % 26 + 65)
+
+def tr_word(word, jump):
+ return ''.join(list(map(tr_letter, word, jump)))
+
+if __name__ == "__main__":
+ word = 'Perl'
+ jump = [2,22,19,9]
+ print(tr_word(word, jump))
+
+ word = 'Raku';
+ jump = [24,4,7,17]
+ print(tr_word(word, jump))
diff --git a/challenge-212/spadacciniweb/ruby/ch-1.rb b/challenge-212/spadacciniweb/ruby/ch-1.rb
new file mode 100644
index 0000000000..ae3683628f
--- /dev/null
+++ b/challenge-212/spadacciniweb/ruby/ch-1.rb
@@ -0,0 +1,44 @@
+# Task 1: Jumping Letters
+# Submitted by: Mohammad S Anwar
+#
+# You are given a word having alphabetic characters only, and a list of positive integers of the same length
+#
+# Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.
+#
+# Example 1
+# Input: $word = 'Perl' and @jump = (2,22,19,9)
+# Output: Raku
+# 'P' jumps 2 place forward and becomes 'R'.
+# 'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a')
+# 'r' jumps 19 place forward and becomes 'k'.
+# 'l' jumps 9 place forward and becomes 'u'.
+
+# Example 2
+# Input: $word = 'Raku' and @jump = (24,4,7,17)
+# Output: 'Perl'
+
+def tr_letter(char,jump)
+ order = char.ord()
+ if (order >= 97)
+ then
+ return ( ( order - 97 + jump ) % 26 + 97 ).chr()
+ else
+ return ( ( order - 65 + jump ) % 26 + 65 ).chr()
+ end
+end
+
+def tr_word(word, jump)
+ new_word = ''
+ (0...word.length).each { |i|
+ new_word += tr_letter word[i], jump[i]
+ }
+ return new_word
+end
+
+word = 'Perl'
+jump = [2,22,19,9]
+puts tr_word word, jump
+
+word = 'Raku'
+jump = [24,4,7,17]
+puts tr_word word, jump