aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2025-09-29 13:09:27 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2025-09-29 13:09:27 +0200
commit48ac9bfec6c18250a4b47a33062e77528ca91b6c (patch)
tree7bd790e892c1adcf978b297996c286b962fc274e
parentcd67dcfb85d89114cfac5aceda02b23b9dda6f8c (diff)
downloadperlweeklychallenge-club-48ac9bfec6c18250a4b47a33062e77528ca91b6c.tar.gz
perlweeklychallenge-club-48ac9bfec6c18250a4b47a33062e77528ca91b6c.tar.bz2
perlweeklychallenge-club-48ac9bfec6c18250a4b47a33062e77528ca91b6c.zip
Add ch-1 in Ruby
-rw-r--r--challenge-341/spadacciniweb/ruby/ch-1.rb66
1 files changed, 66 insertions, 0 deletions
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)