diff options
| -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) |
