diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-24 22:31:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 22:31:07 +0100 |
| commit | 17667dadffe84537f2b15b6f2ef6b429c587cf8b (patch) | |
| tree | 6074e79e9c40fe0b26e612004abb68ede9373d56 | |
| parent | b57e8c4dc46ffcbf7d6d5839cb3bcbcef815b9ad (diff) | |
| parent | d557fb075504f51ce68bd5972a29fc8f56932c38 (diff) | |
| download | perlweeklychallenge-club-17667dadffe84537f2b15b6f2ef6b429c587cf8b.tar.gz perlweeklychallenge-club-17667dadffe84537f2b15b6f2ef6b429c587cf8b.tar.bz2 perlweeklychallenge-club-17667dadffe84537f2b15b6f2ef6b429c587cf8b.zip | |
Merge pull request #10317 from oWnOIzRi/week275
add solutions week 275 in python
| -rw-r--r-- | challenge-275/steven-wilson/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-275/steven-wilson/python/ch-2.py | 37 |
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) |
