aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2024-06-24 15:01:02 +0100
committerSteven <steven1170@zoho.eu>2024-06-24 15:01:02 +0100
commitd557fb075504f51ce68bd5972a29fc8f56932c38 (patch)
treed8942c1e65054f7d448dd09ad3f387fdb684c9e4
parente706d2548a3ce7ff5d3ae480224ee3ee6c5577de (diff)
downloadperlweeklychallenge-club-d557fb075504f51ce68bd5972a29fc8f56932c38.tar.gz
perlweeklychallenge-club-d557fb075504f51ce68bd5972a29fc8f56932c38.tar.bz2
perlweeklychallenge-club-d557fb075504f51ce68bd5972a29fc8f56932c38.zip
add solutions week 275 in python
-rw-r--r--challenge-275/steven-wilson/python/ch-1.py22
-rw-r--r--challenge-275/steven-wilson/python/ch-2.py37
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-275/steven-wilson/python/ch-1.py b/challenge-275/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..1d4026cdc2
--- /dev/null
+++ b/challenge-275/steven-wilson/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+
+def typed_fully(sentence, *keys):
+ ''' Given a sentence and list of broken keys, return number of words can be
+ typed fully.
+ >>> typed_fully("Perl Weekly Challenge", 'l', 'a')
+ 0
+ >>> typed_fully("Perl and Raku", 'a')
+ 1
+ >>> typed_fully("Well done Team PWC", 'l', 'o')
+ 2
+ >>> typed_fully("The joys of polyglottism", 'T')
+ 2
+ '''
+ return sum(1 for word in sentence.split() if not any(key.casefold() in word.casefold() for key in keys))
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-275/steven-wilson/python/ch-2.py b/challenge-275/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..d215aadbe9
--- /dev/null
+++ b/challenge-275/steven-wilson/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+
+def replace_digits(string):
+ ''' Given an alphanumeric string, where each character is either a letter
+ or a digit, replace each digit in the given string with the value of the
+ previous letter plus (digit) places.
+ >>> replace_digits('a1c1e1')
+ 'abcdef'
+ >>> replace_digits('a1b2c3d4')
+ 'abbdcfdh'
+ >>> replace_digits('b2b')
+ 'bdb'
+ >>> replace_digits('a16z')
+ 'abgz'
+ '''
+ if not string[0].isalpha():
+ raise ValueError('First character of string should be letter.')
+
+ if not string.isalnum():
+ raise ValueError('String should be alphanumeric')
+
+ previous_letter = None
+ result = []
+ for c in string:
+ if c.isdigit():
+ result.append(chr(ord(previous_letter) + int(c)))
+ else:
+ result.append(c)
+ previous_letter = c
+ return "".join(result)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)