diff options
| -rw-r--r-- | challenge-273/steven-wilson/python/ch-1.py | 28 | ||||
| -rw-r--r-- | challenge-273/steven-wilson/python/ch-2.py | 25 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-273/steven-wilson/python/ch-1.py b/challenge-273/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..ef93272112 --- /dev/null +++ b/challenge-273/steven-wilson/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import math + + +def percentage_of_character(string, character): + ''' Given a string and a character, return the percentage, nearest whole, + of given character in the given string. + >>> percentage_of_character("perl", "e") + 25 + >>> percentage_of_character("java", "a") + 50 + >>> percentage_of_character("python", "m") + 0 + >>> percentage_of_character("ada", "a") + 67 + >>> percentage_of_character("ballerina", "l") + 22 + >>> percentage_of_character("analitik", "k") + 13 + ''' + return math.floor((string.count(character) / len(string) * 100) + 0.5) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-273/steven-wilson/python/ch-2.py b/challenge-273/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..c8e3c675b4 --- /dev/null +++ b/challenge-273/steven-wilson/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + + +def b_after_a(string): + ''' Given a string, return true if there is at least one b, and no a + appears after the first b. + >>> b_after_a("aabb") + True + >>> b_after_a("abab") + False + >>> b_after_a("aaa") + False + >>> b_after_a("bbb") + True + ''' + first_b_index = string.find("b") + if first_b_index != -1 and "a" not in string[first_b_index:]: + return True + return False + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
