aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-02 15:39:51 +0100
committerGitHub <noreply@github.com>2025-10-02 15:39:51 +0100
commita096137b9ff18eeb6e55f8945dcedb1d0db5b1ef (patch)
tree70a420ac328d6eccebab2b0a20d01c0698966974
parent7a93bd31f3f5c8a033e38d3b7be616b632de4399 (diff)
parent8b5e21e973d05854e4fec739dda8d2d7ce79df61 (diff)
downloadperlweeklychallenge-club-a096137b9ff18eeb6e55f8945dcedb1d0db5b1ef.tar.gz
perlweeklychallenge-club-a096137b9ff18eeb6e55f8945dcedb1d0db5b1ef.tar.bz2
perlweeklychallenge-club-a096137b9ff18eeb6e55f8945dcedb1d0db5b1ef.zip
Merge pull request #12776 from spadacciniweb/PWC-341
PWC 341
-rw-r--r--challenge-341/spadacciniweb/go/ch-1.go95
-rw-r--r--challenge-341/spadacciniweb/go/ch-2.go93
-rw-r--r--challenge-341/spadacciniweb/perl/ch-1.pl72
-rw-r--r--challenge-341/spadacciniweb/perl/ch-2.pl68
-rw-r--r--challenge-341/spadacciniweb/python/ch-1.py65
-rw-r--r--challenge-341/spadacciniweb/python/ch-2.py62
-rw-r--r--challenge-341/spadacciniweb/ruby/ch-1.rb66
-rw-r--r--challenge-341/spadacciniweb/ruby/ch-2.rb63
8 files changed, 584 insertions, 0 deletions
diff --git a/challenge-341/spadacciniweb/go/ch-1.go b/challenge-341/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..f037ec6eea
--- /dev/null
+++ b/challenge-341/spadacciniweb/go/ch-1.go
@@ -0,0 +1,95 @@
+/*
+Task 1: Broken Keyboard
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string containing English letters only and also you are given broken keys.
+Write a script to return the total words in the given sentence can be typed completely.
+
+Example 1
+Input: $str = 'Hello World', @keys = ('d')
+Output: 1
+
+With broken key 'd', we can only type the word 'Hello'.
+
+Example 2
+Input: $str = 'apple banana cherry', @keys = ('a', 'e')
+Output: 0
+
+Example 3
+Input: $str = 'Coding is fun', @keys = ()
+Output: 3
+
+No keys broken.
+
+Example 4
+Input: $str = 'The Weekly Challenge', @keys = ('a','b')
+Output: 3
+
+Example 5
+Input: $str = 'Perl and Python', @keys = ('p')
+Output: 1
+*/
+
+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+func checkKeysInString(s string, bKeys []byte) bool {
+ isInto := false
+ for _, c := range[]byte(s) {
+ for _, k := range bKeys {
+ if c == k {
+ isInto = true
+ }
+ }
+ }
+ return isInto
+}
+
+func convert_chars_to_bytes(keys []string) []byte {
+ var bKeys []byte
+ for _, x := range keys {
+ bArr := []byte(strings.ToLower(x))
+ bKeys = append(bKeys, bArr[0])
+ }
+ return bKeys
+}
+
+
+func broken_keyboard(str string, keys []string) {
+ strs := strings.Split(str, " ")
+ bKeys := convert_chars_to_bytes( keys )
+
+ var okStrings []string
+ for _, s := range strs {
+ if !checkKeysInString(s, bKeys) {
+ okStrings = append(okStrings, s)
+ }
+ }
+ fmt.Printf("'%s' ('%s') -> %d\n", str, strings.Join(keys, "','"), len(okStrings))
+}
+
+func main() {
+ str := strings.ToLower("Hello World")
+ keys := []string{"d"}
+ broken_keyboard(str, keys)
+
+ str = "banana cherry"
+ keys = []string{"a", "e"}
+ broken_keyboard(str, keys)
+
+ str = "Coding is fun"
+ keys = []string{}
+ broken_keyboard(str, keys)
+
+ str = "The Weekly Challenge"
+ keys = []string{"a","b"}
+ broken_keyboard(str, keys)
+
+ str = "Perl and Python"
+ keys = []string{"p"}
+ broken_keyboard(str, keys)
+}
diff --git a/challenge-341/spadacciniweb/go/ch-2.go b/challenge-341/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..b34c62ec20
--- /dev/null
+++ b/challenge-341/spadacciniweb/go/ch-2.go
@@ -0,0 +1,93 @@
+/*
+Task 2: Reverse Prefix
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str and a character in the given string, $char.
+Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string.
+
+Example 1
+Input: $str = "programming", $char = "g"
+Output: "gorpmming"
+
+Reverse of prefix "prog" is "gorp".
+
+Example 2
+Input: $str = "hello", $char = "h"
+Output: "hello"
+
+Example 3
+Input: $str = "abcdefghij", $char = "h"
+Output: "hgfedcbaj"
+
+Example 4
+Input: $str = "reverse", $char = "s"
+Output: "srevere"
+
+Example 5
+Input: $str = "perl", $char = "r"
+Output: "repl"
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func reverse_array(arr []rune) []rune {
+ left := 0
+ right := len(arr) - 1
+ for left < right {
+ arr[left], arr[right] = arr[right], arr[left]
+ left++
+ right--
+ }
+ return arr
+}
+
+func reverse_prefix(str string, chars string) {
+ var str_new []rune
+ var c rune
+
+ for _, char := range chars {
+ c = char
+ break
+ }
+
+ for _, s := range str {
+ str_new = append(str_new, s)
+ if s == c {
+ reverse_array(str_new)
+ break
+ }
+ }
+
+ for _, s := range str[len(str_new):] {
+ str_new = append(str_new, s)
+ }
+
+ fmt.Printf("'%s' '%s' -> '%s'\n", string(str), string(chars), string(str_new))
+}
+
+
+func main() {
+ str := "programming"
+ char := "g"
+ reverse_prefix( str, char )
+
+ str = "hello"
+ char = "h"
+ reverse_prefix( str, char )
+
+ str = "abcdefghij"
+ char = "h"
+ reverse_prefix( str, char )
+
+ str = "reverse"
+ char = "s"
+ reverse_prefix( str, char )
+
+ str = "perl"
+ char = "r"
+ reverse_prefix( str, char )
+}
diff --git a/challenge-341/spadacciniweb/perl/ch-1.pl b/challenge-341/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..cf8b04d173
--- /dev/null
+++ b/challenge-341/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/env perl
+
+# Task 1: Broken Keyboard
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string containing English letters only and also you are given broken keys.
+# Write a script to return the total words in the given sentence can be typed completely.
+#
+# Example 1
+# Input: $str = 'Hello World', @keys = ('d')
+# Output: 1
+#
+# With broken key 'd', we can only type the word 'Hello'.
+#
+# Example 2
+# Input: $str = 'apple banana cherry', @keys = ('a', 'e')
+# Output: 0
+#
+# Example 3
+# Input: $str = 'Coding is fun', @keys = ()
+# Output: 3
+#
+# No keys broken.
+#
+# Example 4
+# Input: $str = 'The Weekly Challenge', @keys = ('a','b')
+# Output: 3
+#
+# Example 5
+# Input: $str = 'Perl and Python', @keys = ('p')
+# Output: 1
+
+use strict;
+use warnings;
+
+my $str = 'Hello World'; my @keys = ('d');
+broken_keyboard($str, \@keys);
+
+$str = 'apple banana cherry'; @keys = ('a', 'e');
+broken_keyboard($str, \@keys);
+#
+$str = 'Coding is fun'; @keys = ();
+broken_keyboard($str, \@keys);
+
+$str = 'The Weekly Challenge'; @keys = ('a','b');
+broken_keyboard($str, \@keys);
+
+$str = 'Perl and Python'; @keys = ('p');
+broken_keyboard($str, \@keys);
+
+exit 0;
+
+sub broken_keyboard {
+ my $str = shift;
+ my $keys = shift;
+
+ my %words = map { $_ => 0 }
+ split / /, $str;
+ foreach my $word (%words) {
+ next if $words{ $word };
+ foreach my $key (@$keys) {
+ if ($word =~ /$key/i) {
+ $words{ $word } += 1;
+ last;
+ }
+ }
+ }
+
+ printf "'%s' (%s) -> %d\n", $str,
+ (join ', ', map { sprintf "'$_'" } @$keys),
+ scalar grep { $_ == 0 } values %words;
+}
diff --git a/challenge-341/spadacciniweb/perl/ch-2.pl b/challenge-341/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..f0886bd5cb
--- /dev/null
+++ b/challenge-341/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/env perl
+
+# Task 2: Reverse Prefix
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str and a character in the given string, $char.
+# Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string.
+#
+# Example 1
+# Input: $str = "programming", $char = "g"
+# Output: "gorpmming"
+#
+# Reverse of prefix "prog" is "gorp".
+#
+# Example 2
+# Input: $str = "hello", $char = "h"
+# Output: "hello"
+#
+# Example 3
+# Input: $str = "abcdefghij", $char = "h"
+# Output: "hgfedcbaj"
+#
+# Example 4
+# Input: $str = "reverse", $char = "s"
+# Output: "srevere"
+#
+# Example 5
+# Input: $str = "perl", $char = "r"
+# Output: "repl"
+
+use strict;
+use warnings;
+
+my $str = "programming"; my $char = "g";
+reverse_prefix( $str, $char );
+
+$str = "hello", $char = "h";
+reverse_prefix( $str, $char );
+
+$str = "abcdefghij"; $char = "h";
+reverse_prefix( $str, $char );
+
+$str = "reverse"; $char = "s";
+reverse_prefix( $str, $char );
+
+$str = "perl"; $char = "r";
+reverse_prefix( $str, $char );
+
+exit 0;
+
+sub reverse_prefix {
+ my $str = shift;
+ my $char = shift;
+
+ my @str = split //, $str;
+ my $new_str = '';
+ foreach (1 .. length($str)) {
+ my $letter = shift @str;
+ $new_str .= $letter;
+ if ($letter eq $char) {
+ $new_str = reverse $new_str;
+ last;
+ }
+ }
+ $new_str .= join '', @str;
+
+ printf "'%s' '%s' -> '%s'\n", $str, $char, $new_str;
+}
diff --git a/challenge-341/spadacciniweb/python/ch-1.py b/challenge-341/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..cdfc3487ab
--- /dev/null
+++ b/challenge-341/spadacciniweb/python/ch-1.py
@@ -0,0 +1,65 @@
+# Task 1: Broken Keyboard
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string containing English letters only and also you are given broken keys.
+# Write a script to return the total words in the given sentence can be typed completely.
+#
+# Example 1
+# Input: $str = 'Hello World', @keys = ('d')
+# Output: 1
+#
+# With broken key 'd', we can only type the word 'Hello'.
+#
+# Example 2
+# Input: $str = 'apple banana cherry', @keys = ('a', 'e')
+# Output: 0
+#
+# Example 3
+# Input: $str = 'Coding is fun', @keys = ()
+# Output: 3
+#
+# No keys broken.
+#
+# Example 4
+# Input: $str = 'The Weekly Challenge', @keys = ('a','b')
+# Output: 3
+#
+# Example 5
+# Input: $str = 'Perl and Python', @keys = ('p')
+# Output: 1
+
+import re
+
+def broken_keyboard(str, keys):
+ words = dict.fromkeys( str.split(), 0)
+
+ for w in words.keys():
+ if words[w] > 0:
+ continue
+ for key in keys:
+ if re.search(key, w, re.IGNORECASE):
+ words[w] += 1
+ break
+
+ print("'%s' (%s) -> %s" % ( str, ', '.join( keys ), len( [w for w, val in words.items() if val == 0]) ) )
+
+if __name__ == "__main__":
+ str = 'Hello World'
+ keys = ('d')
+ broken_keyboard(str, keys)
+
+ str = 'apple banana cherry'
+ keys = ('a', 'e')
+ broken_keyboard(str, keys)
+#
+ str = 'Coding is fun'
+ keys = ()
+ broken_keyboard(str, keys)
+
+ str = 'The Weekly Challenge'
+ keys = ('a','b')
+ broken_keyboard(str, keys)
+
+ str = 'Perl and Python'
+ keys = ('p')
+ broken_keyboard(str, keys)
diff --git a/challenge-341/spadacciniweb/python/ch-2.py b/challenge-341/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..a0d3d07e67
--- /dev/null
+++ b/challenge-341/spadacciniweb/python/ch-2.py
@@ -0,0 +1,62 @@
+# Task 2: Reverse Prefix
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str and a character in the given string, $char.
+# Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string.
+#
+# Example 1
+# Input: $str = "programming", $char = "g"
+# Output: "gorpmming"
+#
+# Reverse of prefix "prog" is "gorp".
+#
+# Example 2
+# Input: $str = "hello", $char = "h"
+# Output: "hello"
+#
+# Example 3
+# Input: $str = "abcdefghij", $char = "h"
+# Output: "hgfedcbaj"
+#
+# Example 4
+# Input: $str = "reverse", $char = "s"
+# Output: "srevere"
+#
+# Example 5
+# Input: $str = "perl", $char = "r"
+# Output: "repl"
+
+def reverse_prefix(str, char):
+ strings = list(str)
+ new_str = ''
+ for n in (range(len(str))):
+ letter = strings.pop(0);
+ new_str += letter
+ if letter == char:
+ new_str = new_str[::-1]
+ break
+ new_str += ''.join( strings )
+
+ print("'%s' '%s' -> '%s'" % (str, char, new_str) )
+
+if __name__ == "__main__":
+ str = "programming"
+ char = "g"
+ reverse_prefix( str, char )
+
+ str = "hello"
+ char = "h"
+ reverse_prefix( str, char )
+
+ str = "abcdefghij"
+ char = "h"
+ reverse_prefix( str, char )
+
+ str = "reverse"
+ char = "s"
+ reverse_prefix( str, char )
+
+ str = "perl"
+ char = "r"
+ reverse_prefix( str, char )
+
diff --git a/challenge-341/spadacciniweb/ruby/ch-1.rb b/challenge-341/spadacciniweb/ruby/ch-1.rb
new file mode 100644
index 0000000000..44ced865e7
--- /dev/null
+++ b/challenge-341/spadacciniweb/ruby/ch-1.rb
@@ -0,0 +1,66 @@
+# Task 1: Broken Keyboard
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string containing English letters only and also you are given broken keys.
+# Write a script to return the total words in the given sentence can be typed completely.
+#
+# Example 1
+# Input: $str = 'Hello World', @keys = ('d')
+# Output: 1
+#
+# With broken key 'd', we can only type the word 'Hello'.
+#
+# Example 2
+# Input: $str = 'apple banana cherry', @keys = ('a', 'e')
+# Output: 0
+#
+# Example 3
+# Input: $str = 'Coding is fun', @keys = ()
+# Output: 3
+#
+# No keys broken.
+#
+# Example 4
+# Input: $str = 'The Weekly Challenge', @keys = ('a','b')
+# Output: 3
+#
+# Example 5
+# Input: $str = 'Perl and Python', @keys = ('p')
+# Output: 1
+
+def broken_keyboard(str, keys)
+ words = {}
+ str.split.each { |w| words[w] = 0 }
+ words.each_key do |w|
+ keys.each do |k|
+ if w =~ /#{k}/i
+ words[w] += 1
+ break
+ end
+ end
+ end
+
+ printf "'%s' (%s) -> %d\n", str,
+ keys.join(', '),
+ words.select { |_, v| v == 0 }.length
+end
+
+str = 'Hello World'
+keys = ['d']
+broken_keyboard(str, keys)
+
+str = 'apple banana cherry'
+keys = ['a', 'e']
+broken_keyboard(str, keys)
+#
+str = 'Coding is fun'
+keys = []
+broken_keyboard(str, keys)
+
+str = 'The Weekly Challenge'
+keys = ['a','b']
+broken_keyboard(str, keys)
+
+str = 'Perl and Python'
+keys = ['p']
+broken_keyboard(str, keys)
diff --git a/challenge-341/spadacciniweb/ruby/ch-2.rb b/challenge-341/spadacciniweb/ruby/ch-2.rb
new file mode 100644
index 0000000000..94ae6e1ffc
--- /dev/null
+++ b/challenge-341/spadacciniweb/ruby/ch-2.rb
@@ -0,0 +1,63 @@
+# Task 2: Reverse Prefix
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str and a character in the given string, $char.
+# Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string.
+#
+# Example 1
+# Input: $str = "programming", $char = "g"
+# Output: "gorpmming"
+#
+# Reverse of prefix "prog" is "gorp".
+#
+# Example 2
+# Input: $str = "hello", $char = "h"
+# Output: "hello"
+#
+# Example 3
+# Input: $str = "abcdefghij", $char = "h"
+# Output: "hgfedcbaj"
+#
+# Example 4
+# Input: $str = "reverse", $char = "s"
+# Output: "srevere"
+#
+# Example 5
+# Input: $str = "perl", $char = "r"
+# Output: "repl"
+
+def reverse_prefix(str, char)
+ chars = str.split('')
+ new_str = ''
+ (1..str.length).each do |_|
+ letter = chars.shift
+ new_str.concat(letter)
+ if letter == char
+ new_str = new_str.reverse
+ break
+ end
+ end
+ new_str.concat(chars.join())
+
+ printf "'%s' '%s' -> '%s'\n", str, char, new_str
+end
+
+str = "programming"
+char = "g"
+reverse_prefix(str, char)
+
+str = "hello"
+char = "h"
+reverse_prefix(str, char)
+
+str = "abcdefghij"
+char = "h"
+reverse_prefix(str, char)
+
+str = "reverse"
+char = "s"
+reverse_prefix(str, char)
+
+str = "perl"
+char = "r"
+reverse_prefix(str, char)