diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-09-29 12:25:38 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-09-29 12:25:38 +0200 |
| commit | cd07f0044d6780da5c7ca84de1262fba34a0c2a7 (patch) | |
| tree | 2a2559f688e1db44fd46e821106250d00580c56d | |
| parent | 61be3b4f081b822e9ee3c86589a0e5c59663b2bc (diff) | |
| download | perlweeklychallenge-club-cd07f0044d6780da5c7ca84de1262fba34a0c2a7.tar.gz perlweeklychallenge-club-cd07f0044d6780da5c7ca84de1262fba34a0c2a7.tar.bz2 perlweeklychallenge-club-cd07f0044d6780da5c7ca84de1262fba34a0c2a7.zip | |
Add ch-1 and ch-2 in Python
| -rw-r--r-- | challenge-341/spadacciniweb/python/ch-1.py | 65 | ||||
| -rw-r--r-- | challenge-341/spadacciniweb/python/ch-2.py | 62 |
2 files changed, 127 insertions, 0 deletions
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 ) + |
