diff options
| author | Steven <steven1170@zoho.eu> | 2024-07-30 15:59:33 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-07-30 15:59:33 +0100 |
| commit | e709ea80940fda86b17b6f2f7817bdc6373b2565 (patch) | |
| tree | bc5e8b600fea08e1faa93c1dd66b0958bd68f0e6 | |
| parent | 4850143bf7a755fffe1ae7216fd55eb5661495c9 (diff) | |
| download | perlweeklychallenge-club-e709ea80940fda86b17b6f2f7817bdc6373b2565.tar.gz perlweeklychallenge-club-e709ea80940fda86b17b6f2f7817bdc6373b2565.tar.bz2 perlweeklychallenge-club-e709ea80940fda86b17b6f2f7817bdc6373b2565.zip | |
add solutions week 280 in python
| -rw-r--r-- | challenge-280/steven-wilson/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-280/steven-wilson/python/ch-2.py | 29 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-280/steven-wilson/python/ch-1.py b/challenge-280/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..dc4c9d8dab --- /dev/null +++ b/challenge-280/steven-wilson/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from collections import Counter +from string import ascii_lowercase + + +def twice_appearance(string): + """ Given a string containing lowercase English letters only, return the + first letter that appears twice. + + >>> twice_appearance("acbddbca") + 'd' + >>> twice_appearance("abccd") + 'c' + >>> twice_appearance("abcdabbb") + 'a' + >>> twice_appearance("abcdefgh") + + """ + if any(c not in ascii_lowercase for c in string): + raise ValueError("String should contain only lowercase letters") + + counter = Counter() + for c in string: + counter[c] += 1 + if counter[c] == 2: + return c + return None + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-280/steven-wilson/python/ch-2.py b/challenge-280/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..8a833439c0 --- /dev/null +++ b/challenge-280/steven-wilson/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + + +def count_asterisks(string): + """ Given a string, where every two consecutive vertical bars are grouped + into a pair, return the number of asterisks, *, excluding any between each + pair of vertical bars. + + >>> count_asterisks("p|*e*rl|w**e|*ekly|") + 2 + >>> count_asterisks("perl") + 0 + >>> count_asterisks("th|ewe|e**|k|l***ych|alleng|e") + 5 + """ + between = False + asterisks = 0 + for c in string: + if c == "|": + between = not between + if not between and c == "*": + asterisks += 1 + return asterisks + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
