diff options
| author | Steven <steven1170@zoho.eu> | 2024-08-12 16:31:02 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-08-12 16:31:02 +0100 |
| commit | 1d567567864d4faddd976eeb385f2f642d4668b8 (patch) | |
| tree | ffc2622974ca61e704e32422624b99c9c2a6d551 | |
| parent | d00deb4fc0f9c8f599bd80cf1b2e7ac56fa2d065 (diff) | |
| download | perlweeklychallenge-club-1d567567864d4faddd976eeb385f2f642d4668b8.tar.gz perlweeklychallenge-club-1d567567864d4faddd976eeb385f2f642d4668b8.tar.bz2 perlweeklychallenge-club-1d567567864d4faddd976eeb385f2f642d4668b8.zip | |
add solutions week 282 in python
| -rw-r--r-- | challenge-282/steven-wilson/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-282/steven-wilson/python/ch-2.py | 30 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-282/steven-wilson/python/ch-1.py b/challenge-282/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..dd8a772db2 --- /dev/null +++ b/challenge-282/steven-wilson/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from itertools import groupby + + +def good_integer(integer): + """ Given a positive integer, having 3 or more digits, return the Good + Integer in the given integer or -1 if none found. + + A good integer is exactly three consecutive matching digits. + + >>> good_integer(12344456) + '444' + >>> good_integer(1233334) + -1 + >>> good_integer(10020003) + '000' + """ + integer_string = str(integer) + + if len(integer_string) < 3: + raise ValueError('Integer provided must have at least 3 digits') + + for _, item in groupby(integer_string): + group = ''.join(item) + if len(group) == 3: + return group + return -1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-282/steven-wilson/python/ch-2.py b/challenge-282/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..199cffc157 --- /dev/null +++ b/challenge-282/steven-wilson/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from itertools import groupby + + +def changing_keys(string): + """ Given an alphabetic string as typed by user,return the number of times + user had to change the key to type the given string. Changing key is + defined as using a key different from the last used key. The shift and caps + lock keys won’t be counted. + >>> changing_keys('pPeERrLl') + 3 + >>> changing_keys('rRr') + 0 + >>> changing_keys('GoO') + 1 + """ + if len(string) == 0: + return 0 + + if not all(c.isalpha() for c in string): + raise ValueError('User should type an aphabetic string') + + return len([item for _, item in groupby(string.casefold())]) - 1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
