diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-12 16:50:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-12 16:50:32 +0100 |
| commit | b819ece18f323c2bc8a5ef46816b56fe8142132c (patch) | |
| tree | 6d3415cbdcee81ea1b42abbd25d11d072f2977a5 /challenge-282 | |
| parent | 4740d41bc092fbced507d5ecc1b2f8988a82619f (diff) | |
| parent | 1d567567864d4faddd976eeb385f2f642d4668b8 (diff) | |
| download | perlweeklychallenge-club-b819ece18f323c2bc8a5ef46816b56fe8142132c.tar.gz perlweeklychallenge-club-b819ece18f323c2bc8a5ef46816b56fe8142132c.tar.bz2 perlweeklychallenge-club-b819ece18f323c2bc8a5ef46816b56fe8142132c.zip | |
Merge pull request #10597 from oWnOIzRi/week282
add solutions week 282 in python
Diffstat (limited to 'challenge-282')
| -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) |
